티거의 개발 공간

자바 소켓통신 하는 코드는 아래와 같다.

 


 

[ Server.java ]

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.net.*;
import java.util.*;
import java.io.*;
 
class Server
{
 
    private static int port = 0;
 
    public static void main(String[] args) 
    {
        System.out.println("SERVER PROGRAM");
        Scanner scan = new Scanner(System.in);
        while(true){
            System.out.print("[SYSTEM] Type Port : ");
            String ports = scan.nextLine();
            try{
                port = Integer.parseInt(ports);
                if(port <= 0 || port > 9999){
                    System.out.println("[SYSTEM] Wrong Port!");
                    port = 0;
                } else break;
            } catch(Exception e){ System.out.println("[SYSTEM] Wrong Port!"); }
        }
        openServer();
    }
 
    private static void openServer(){
        try{
            ServerSocket server = new ServerSocket(port);
            System.out.println("[SYSTEM] Server Started ["+port+"]");
            System.out.println("[SYSTEM] Wait Client...");
            Socket socket = server.accept();
            System.out.println("[SYSTEM] Connect! ["+socket.getInetAddress()+"]");
        
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            OutputStream os = socket.getOutputStream();
            DataOutputStream dou = new DataOutputStream(os);
 
            while(true){
                String msg = dis.readUTF();
 
                if(msg.equalsIgnoreCase("exit")){
                    dou.writeUTF("exit");
                    System.out.println("[SYSTEM] Shutdown Server");
                    socket.close();
                    server.close();
                    break;
                } else {
                    System.out.println("[CLIENT] " + msg);
                    dou.writeUTF("Data Received : " + msg);
                }
            }
        } catch(Exception e){
            System.out.println("[SYSTEM] Failed to open server");
        }
    }
 
}
 
 
cs

 

 


 

 

 

[ Client.java ]

 

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
import java.net.*;
import java.util.*;
 
class Client 
{
 
    private static int port  = 0;
    private static String ip = "";
 
    public static void main(String[] args) 
    {
        System.out.println("CLIENT PROGRAM");
        Scanner scan = new Scanner(System.in);
        System.out.print("[SYSTEM] Type Ip: ");
        ip = scan.nextLine();
        while(true){
            System.out.print("[SYSTEM] Type Port : ");
            String ports = scan.nextLine();
            try{
                port = Integer.parseInt(ports);
                if(port <= 0 || port > 9999){
                    System.out.println("[SYSTEM] Wrong Port!");
                    port = 0;
                } else break;
            } catch(Exception e){ System.out.println("[SYSTEM] Wrong Port!"); }
        }
        
        Connect();
    }
 
 
    private static void Connect(){
        try{
            Socket socket = new Socket(ip, port);
            System.out.println("[SYSTEM] Server Connected ["+port+"]");
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);
 
            Scanner scan = new Scanner(System.in);
            while(true){
                System.out.print("[SYSTEM] Type Message : ");
                String msg = scan.nextLine();
                
                OutputStream os = socket.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeUTF(msg);
 
                String remsg = dis.readUTF();
    
                if(remsg.equalsIgnoreCase("exit")){
                    System.out.println("[SYSTEM] Shutdown Client");
                    socket.close();
                    break;
                } else
                System.out.println("[SERVER] " + remsg);
            }
 
        } catch(Exception e){
            System.out.println("[SYSTEM] You can not access the server");
        }
    }
}
 
 
cs

 

 


 

서버와 클라이언트 두개의 클래스가 있으며

서버 클래스를 먼저 실행시켜준 뒤 서버를 개방할 포트 입력 후

클라이언트 클래스를 실행시켜 접속할 아이피, 포트 입력하면

접속되어 소켓통신이 가능하다.

 

그리고 접속 포트와 서버 포트는 1~9999번으로 제한했지만,

실제로는 1~65565번까지 사용가능하다는거 같다.