[java] TCP/IP socket code(*.java, TCPIP 소켓 코드)

2022. 1. 25. 15:36Language/java

728x90
728x90

[java] 리눅스(linux) tcp/ip client socket(클라이언트 소켓), tcp/ip server socket(서버 소켓) 메시지(message) 송신, 수신(send/recv) 예제


-예제 실행하는 서버 정보

$ cat /etc/*release*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"

$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode

-linux jdk 설치 참고.


-java tcp/ip server socket 코드(serverSocket.java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class serverSocket {
        public static void main(String[] args) {
                try {
                        ServerSocket serverSocket = new ServerSocket(12345);
                        Socket socketUser = null;
                        socketUser = serverSocket.accept();
                        String msg = "serverSocketMsg";

                        InputStream input = socketUser.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                        System.out.println("recv msg : " + reader.readLine());

                        OutputStream out = socketUser.getOutputStream();
                        PrintWriter writer = new PrintWriter(out, true);
                        writer.println(msg);
                        System.out.println("send msg : " + msg);

                        out.close();
                        input.close();
                        serverSocket.close();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}

-java tcp/ip client socket 코드(clientSocket.java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class clientSocket {
        public static void main(String[] args) {
                try {
                        Socket socket = new Socket("127.0.0.1", 12345);
                        String msg = "clientSocketMsg";

                        OutputStream out = socket.getOutputStream();
                        PrintWriter writer = new PrintWriter(out, true);
                        writer.println(msg);
                        System.out.println("send msg : " + msg);

                        InputStream input = socket.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                        System.out.println("recv msg : " + reader.readLine());

                        out.close();
                        input.close();
                        socket.close();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}

-tcp/ip server socket 컴파일.

$ javac serverSocket.java

-tcp/ip client socket 컴파일.

$ javac clientSocket.java

-tcp/ip server socket 실행 결과.

$ java serverSocket 
recv msg : clientSocketMsg
send msg : serverSocketMsg

-tcp/ip client socket 실행 결과.

$ java clientSocket 
send msg : clientSocketMsg
recv msg : serverSocketMsg

 

728x90
728x90