본문 바로가기

IT/Java

자바 서버/클라이언트 소켓통신 예제

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Server

package lg.tmte.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	private ServerSocket serverSocket;
	private Socket socket;
	private final int PORT = 8080;
	
	DataInputStream dis;
	DataOutputStream dos;
	InputStream is;
	OutputStream os;
	
	Server(){
		dos = null;
		dis = null;
		os = null;
		is = null;
		socket = null;
	}
	
	public void startServer() {
		try {
			serverSocket = new ServerSocket(PORT);
			while(true) {
				socket = serverSocket.accept();
				is = socket.getInputStream();
				dis = new DataInputStream(is);
				
				String input = dis.readUTF();
				System.out.println(input);
				
				os = socket.getOutputStream();
				dos = new DataOutputStream(os);
				dos.writeUTF(input+"1");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dis.close();
				is.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		new Server().startServer();
	}
}

 

Client

package lg.tmte.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
	
	private final String SERVER_IP = "127.0.0.1";
	private final int PORT = 8080;
	Socket socket;
	
	public void startClient() {
		
		InputStream is = null;
		OutputStream os = null;
		DataInputStream dis = null;
		DataOutputStream dos = null;
		
		Scanner scan = new Scanner(System.in);
		try {
			
			while(true) {
				try {
					socket = new Socket(SERVER_IP,PORT);
					os = socket.getOutputStream();
					dos = new DataOutputStream(os);
					dos.writeUTF(scan.nextLine());
					
					is = socket.getInputStream();
					dis = new DataInputStream(is);
					
					System.out.println("server reply :"+dis.readUTF());
					
				}catch(Exception e) {
					e.printStackTrace();
				}finally {
					socket.close();
				}
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dos.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
//	public void sendText(DataOutputStream dos) throws IOException {
//		
//		Scanner scan = new Scanner(System.in);
//		while(true) {
//			dos.writeUTF(scan.nextLine());
//		}
//	}
	
	
	public static void main(String[] args) {
		new Client().startClient();
	}
}

'IT > Java' 카테고리의 다른 글

jetty server  (0) 2021.06.07
자바 ArrayList<Object> 정렬  (0) 2020.09.23
프로그래머스 DP 도둑  (0) 2020.08.11
프로그래머스 DFS 네트워크  (0) 2020.08.10
자바 EXE 파일 실행 및 데이터 전달  (0) 2020.07.14