JAVA chapter18. IO 기반 입출력 및 네트워킹. 


18.7 TCP 네트워킹

18.7.1 ServerSocket과 Socket의 용도


18.7.2 ServerSocket 생성과 연결 수락


18.7.3 Socket 생성과 연결 요청


ClientExample.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec07.exam01_serversocket_socket;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
 
public class ClientExample {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket(); // Socket 생성
            System.out.println"[연결 요청]");
            socket.connect(new InetSocketAddress("localhost"5001)); // 연결 요청
            System.out.println"[연결 성공]");
        } catch(Exception e) {}
        
        if(!socket.isClosed()) { // 연결이 되어 있을 경우
            try {
                socket.close(); // 연결 끊기
            } catch (IOException e1) {}
        }    
    }
}
cs

ServerExample.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
package sec07.exam01_serversocket_socket;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerExample {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            // ServerSocket 생성
            serverSocket = new ServerSocket();        
            serverSocket.bind(new InetSocketAddress("localhost"5001));
            while(true) {
                System.out.println"[연결 기다림]");
                Socket socket = serverSocket.accept(); // 클라이언트 연결 수락
                InetSocketAddress isa = (InetSocketAddress) socket.getRemoteSocketAddress();
                System.out.println("[연결 수락함] " + isa.getHostName());
            }
        } catch(Exception e) {}
        
        if(!serverSocket.isClosed()) { // ServerSocket이 닫혀있지 않은 경우
            try {
                serverSocket.close(); // ServerSocket 닫기
            } catch (IOException e1) {}
        }
    }
}
cs

18.7.4 Socket 데이터 통신


ClientExample.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
package sec07.exam02_data_read_write;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerExample {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket();        
            serverSocket.bind(new InetSocketAddress("localhost"5001));
            while(true) {
                System.out.println"[연결 기다림]");
                Socket socket = serverSocket.accept();
                InetSocketAddress isa = (InetSocketAddress) socket.getRemoteSocketAddress();
                System.out.println("[연결 수락함] " + isa.getHostName());
                
                byte[] bytes = null;
                String message = null;
                
                InputStream is = socket.getInputStream();
                bytes = new byte[100];
                int readByteCount = is.read(bytes);
                message = new String(bytes, 0, readByteCount, "UTF-8");
                System.out.println("[데이터 받기 성공]: " + message);
                
                OutputStream os = socket.getOutputStream();
                message = "Hello Client";
                bytes = message.getBytes("UTF-8");
                os.write(bytes);
                os.flush();
                System.out.println"[데이터 보내기 성공]");
                
                is.close();
                os.close();
                socket.close();
            }
        } catch(Exception e) {}
        
        if(!serverSocket.isClosed()) {
            try {
                serverSocket.close();
            } catch (IOException e1) {}
        }
    }
}
 
cs

ServerExample.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
package sec07.exam02_data_read_write;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
 
public class ClientExample {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket();
            System.out.println"[연결 요청]");
            socket.connect(new InetSocketAddress("localhost"5001));
            System.out.println"[연결 성공]");
            
            byte[] bytes = null;
            String message = null;
            
            OutputStream os = socket.getOutputStream();
            message = "Hello Server";
            bytes = message.getBytes("UTF-8");
            os.write(bytes);
            os.flush();
            System.out.println"[데이터 보내기 성공]");
            
            InputStream is = socket.getInputStream();
            bytes = new byte[100];
            int readByteCount = is.read(bytes);
            message = new String(bytes, 0, readByteCount, "UTF-8");
            System.out.println("[데이터 받기 성공]: " + message);
            
            os.close();
            is.close();
        } catch(Exception e) {}
        
        if(!socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException e1) {}
        }
    }
}
cs


18.7.5 스레드 병렬 처리



18.7.6 채팅 서버 구현


18.7.7 채팅 클라이언트 구현

Posted by 너래쟁이
: