본문 바로가기
Programming Language/Java

TCP/IP Socket java code

by 팡팡마트 2023. 11. 17.
728x90

1. Server.java

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

public class Server extends Thread {

        final static int SERVER_PORT = 1000;
        final static String MESSAGE_TO_SERVER = "Hello, Client";

        public static void main(String[] args) {

                ServerSocket serverSocket = null;

                try {
                        serverSocket = new ServerSocket(SERVER_PORT);


                } catch (IOException e) {
                        e.printStackTrace();
                }

                try {
                        while (true) {
                                System.out.println("socket 연결 대기");
                                Socket socket = serverSocket.accept();
                                System.out.println("host : "+socket.getInetAddress()+" | 통신 연결 성공");

                                /**     Server에서 보낸 값을 받기 위한 통로 */
                                InputStream is = socket.getInputStream();
                                /**     Server에서 Client로 보내기 위한 통로 */
                                OutputStream os = socket.getOutputStream();

                                byte[] data = new byte[16];
                                int n = is.read(data);
                                final String messageFromClient = new String(data,0,n);

                                System.out.println(messageFromClient);

                                os.write( MESSAGE_TO_SERVER.getBytes() );
                                os.flush();

                                is.close();
                                os.close();
                                socket.close();
                        }

                } catch (IOException e) {
                        e.printStackTrace();
                }

        }
}

class SocketRun implements Runnable {

        private Socket socket = null;

        SocketRun( Socket socket ){
                this.socket = socket;
        }

        @Override
        public void run() {

        }
}

 

2. Client.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

        final static String SERVER_IP = "[서버ip]";
        final static int SERVER_PORT = 1000;
        final static String MESSAGE_TO_SERVER = "Hi, Server";

        public static void main(String[] args) {

                Socket socket = null;

                try {
                        /** 소켓통신 시작 */
                        socket = new Socket(SERVER_IP,SERVER_PORT);
                        System.out.println("socket 연결");

                        /**     Client에서 Server로 보내기 위한 통로 */
                        OutputStream os = socket.getOutputStream();
                        /**     Server에서 보낸 값을 받기 위한 통로 */
                        InputStream is = socket.getInputStream();

                        os.write( MESSAGE_TO_SERVER.getBytes() );
                        os.flush();

                        byte[] data = new byte[16];
                        int n = is.read(data);
                        final String resultFromServer = new String(data,0,n);

                        System.out.println(resultFromServer);

                        socket.close();
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }
}

 

Server

 

Client

 

**이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

728x90

'Programming Language > Java' 카테고리의 다른 글

Spring boot start  (0) 2023.12.15
TCP/IP Socket java code + Keep-Alive 활성화  (1) 2023.11.17