다연이네

[days28] 소켓을 이용해 서버와 클라이언트 연결 (기본) 본문

Java

[days28] 소켓을 이용해 서버와 클라이언트 연결 (기본)

 다연  2020. 11. 1. 16:25
반응형

서버

package days28;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Date;

public class TestServer {

	public static void main(String[] args) {

		int port= 5001;
		ServerSocket serverSocket = null;
		
		try {
			serverSocket = new ServerSocket();
			SocketAddress endpoint = new InetSocketAddress(port);
			serverSocket.bind(endpoint);
			
			while(true) {
				
				System.out.println("> 서버소켓 생성 후 클라이언트 접속 대기");
				//-----1
				
				
				Socket socket = serverSocket.accept();
				
				InetAddress clientIPAddress = socket.getInetAddress();
				//접속한 클라이언트의 ip주소를 가지고있는 객체
				String ip = clientIPAddress.getHostAddress(); //클라이언트의 ip주소
				System.out.printf(">[%s] 고객님 접속\n",ip);
				InputStream is = socket.getInputStream();
				OutputStream os = socket.getOutputStream();
				
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
				
				String message = br.readLine();
				//-----2
				
				System.out.printf(">[%s] - 수신한 메시지 : %s\n", ip, message);
			
				
				Date now = new Date();
				message = String.format("서버에서 수신한 시간 : %s + %s\n",
						now.toLocaleString(), message); //메아리 작업(클라이언트에서 받은 문자 제전송
				bw.write(message);
				bw.flush();
				//-----3
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

 

 

클라이언트

package days28;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class TestClient {

	public static void main(String[] args) {
		
		Socket socket = null;
		String host = "192.168.0.26";
		int port = 5001;
		
		socket = new Socket();
		SocketAddress endpoint = new InetSocketAddress(host, port);
		
		
		try {
			socket.connect(endpoint);
			//-----1
			
			InputStream is =  socket.getInputStream();
			OutputStream os = socket.getOutputStream();	
			
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			
			System.in.read(); //잠깐 멈춤 - 엔터치면
			System.in.skip(System.in.available());
			
			bw.write("배다연:-)\n");
			bw.flush();
			
			String message = br.readLine(); //클라이언트 블로킹
			//-----2
			
			System.out.printf(">메아리: %s\n", message);

		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(">엔터치면 연결 종료");
		try {
			System.in.read();
			System.in.skip(System.in.available());
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(!socket.isClosed()) {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}

}
반응형

'Java' 카테고리의 다른 글

[days28] InetAddress 클래스  (0) 2020.11.01
[days27] 스레드 그룹과 데몬스레드  (0) 2020.10.27
[days27] 동기화  (0) 2020.10.27
[days27] 스레드 강제종료  (0) 2020.10.27
[days26] 스레드 - 동기화의 필요성  (1) 2020.10.26
Comments