mojo's Blog

명품 JAVA Programming 제 15장 Open Challenge 본문

Java

명품 JAVA Programming 제 15장 Open Challenge

_mojo_ 2021. 8. 23. 16:32

Server Code

 

package Part15;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.net.*;


public class ServerEx extends JFrame {
	private static BufferedReader in = null;
	private static BufferedWriter out = null;
	private static ServerSocket listener = null;
	private static Socket socket = null;
	private static Scanner scanner = new Scanner(System.in);
	
	private JTextField tf=new JTextField(25);
	private static JTextArea ta= new JTextArea(10, 25); 
	
	ServerEx(){
		setTitle("Server-Client Chatting");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(tf);
		c.add(new JScrollPane(ta));
		
		tf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JTextField t = (JTextField)e.getSource();
				if(socket == null) {
					t.setText("");
				}
				else {
					String outputMessage = t.getText();
					try {
						out.write(outputMessage+"\n");
						out.flush();
						ta.append("서버: " + outputMessage+"\n");
						t.setText("");	
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						System.out.println(e1.getMessage());
					}
				}
			}
		});
		
		setSize(300, 250);
		setVisible(true); 
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		
		new ServerEx();
		
		try {
			listener = new ServerSocket(9999);
			System.out.println("연결들 기다리고 있습니다......");
			socket = listener.accept();
			ta.setText("클라이언트로부터 연결 완료\n");
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			while(true) {
				String inputMessage = in.readLine();
				if(inputMessage.equalsIgnoreCase("bye")) {
					ta.setText(ta.getText() + "서버가 bye를 입력하여 연결 종료!\n");
					break;
				}
				ta.setText(ta.getText() + "   클라이언트 : "+inputMessage+"\n");
			}
		} catch(IOException e) {
			System.out.println(e.getMessage());
		}
		finally {
			try {
				scanner.close();
				socket.close();
				listener.close();
			} catch(IOException e) {
				System.out.println("클라이언트와 채팅 중 오류가 발생했습니다.");
			}
		}
	}
	
}

 

Client Code

 

package Part15;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.net.*;


public class ClientEx extends JFrame {
	private static BufferedReader in = null;
	private static BufferedWriter out = null;
	private static Socket socket = null;
	private static Scanner scanner = new Scanner(System.in);
	
	private JTextField tf=new JTextField(25);
	private static JTextArea ta= new JTextArea(10, 25); 
	
	ClientEx(){
		setTitle("Client-Server Chatting");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		c.add(tf);
		c.add(new JScrollPane(ta));
		
		tf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JTextField t = (JTextField)e.getSource();
				if(socket == null) {
					t.setText("");
				}
				else {
					String outputMessage = t.getText();
					try {
						out.write(outputMessage+"\n");
						out.flush();
						ta.append("서버: " + outputMessage+"\n");
						t.setText("");	
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						System.out.println(e1.getMessage());
					}
				}
			}
		});
		
		setSize(300, 250);
		setVisible(true); 
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		
		new ClientEx();
		
		try {
			socket = new Socket("localhost", 9999);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			ta.setText("서버에 연결 완료\n");
			while(true) {
				String inputMessage = in.readLine();
				if(inputMessage.equalsIgnoreCase("bye")) {
					ta.setText(ta.getText() + "서버가 bye를 입력하여 연결 종료!\n");
					break;
				}
				ta.setText(ta.getText() + "   클라이언트 : "+inputMessage+"\n");
			}
		} catch(IOException e) {
			System.out.println(e.getMessage());
		}
		finally {
			try {
				scanner.close();
				socket.close();
			} catch(IOException e) {
				System.out.println("서버와 채팅 중 오류가 발생했습니다.");
			}
		}
	}
	
}

 

 

 

'Java' 카테고리의 다른 글

데이터베이스  (0) 2021.08.24
명품 JAVA Programming 제 15장 실습문제 (1, 4, 7, 8 제외)  (0) 2021.08.23
수식 계산 Server-Client 만들기  (0) 2021.08.23
Server-Client Chatting Program 만들기  (0) 2021.08.23
Socket Programming  (0) 2021.08.23
Comments