ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA 45] 자바 Socket 소켓
    Language/Java 2018. 4. 29. 20:47
    반응형
    ssss

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import java.net.*;
    import java.io.*;
     
    class Server{
        ServerSocket server_socket;
        Socket socket;
     
        int port = 2000;
     
        Server(){
            try{
                server_socket = new ServerSocket(port);
     
                while(true){
                    System.out.println(" [ " + port + " ] 번 포트에서 서버 대기중");
                    socket = server_socket.accept();
                    System.out.println("클라이언트 접속 성공 : " + socket.getInetAddress().getHostAddress());
                    socket.close();
                }
            }catch(Exception e){
                System.out.println("[ " + port + " ] 번 포트 이미 사용중");
            }
        }
     
        public static void main(String[] args){
            new Server();
        }
    }
    cs
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import java.net.*;
    import java.io.*;
     
    class Client{
        String ip = "61.81.98.125";
        int port = 2000;
        Socket socket;
     
        Client(){
            try{
                socket = new Socket(ip , port);
                System.out.println("서버와 연결 성공 socket : " + socket);
            }catch(UnknownHostException uhe){
            }catch(IOException ioe){}
        }
     
        public static void main(String[] args){
            new Client();
        }
    }
    cs

     

     

     

    연결만을 위한 Server와 Client

    public static void main(String args[]){
    	test();
    }

     

    반응형
Designed by Tistory.