728x90

서버소켓 채널 생성과 다중 클라이언트 연결 수락 (서버)


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
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
 
public class ServerTest {
 
    public static void main(String[] args) {
        ServerSocketChannel serverSocketChannel = null;
        
        try {
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(true);
            serverSocketChannel.bind(new InetSocketAddress(5001));
            
            while(true) {
                System.out.println("연결 대기..");
                SocketChannel socketChannel = serverSocketChannel.accept();
                InetSocketAddress inetSockAddr = (InetSocketAddress) socketChannel.getRemoteAddress();
                System.out.println("연결 수락!" + inetSockAddr.getHostName());
                
                ByteBuffer byteBuffer = null;
                Charset charset = Charset.forName("UTF-8");
                
                byteBuffer = ByteBuffer.allocate(100);
                int byteCount = socketChannel.read(byteBuffer);
                byteBuffer.flip();
                String message = charset.decode(byteBuffer).toString();
                System.out.println"[데이터 수신 성공] : " + message);
                
                byteBuffer = charset.encode("Hello Client");
                socketChannel.write(byteBuffer);
                System.out.println"[데이터 전송 성공]");
            }
        }
        catch (Exception e) {
        }
        
        if (serverSocketChannel.isOpen()) {
            try {
                serverSocketChannel.close();
            } catch (Exception e) {
            }
        }
    }
}
cs


소켓 채널 생성과 연결 요청 (클라이언트)


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
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
 
public class ClientTest {
    public static void main(String[] args) {
        SocketChannel socketChannel = null;
        try {
            socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(true);
            System.out.println"[연결 요청]");
            socketChannel.connect(new InetSocketAddress("localhost"5001));
            System.out.println"[연결 성공]");
            
            ByteBuffer byteBuffer = null;
            Charset charset = Charset.forName("UTF-8");
            
            // client가 데이터(헬로서버) 보냄
            byteBuffer = charset.encode("Hello Server");
            socketChannel.write(byteBuffer);
            System.out.println"[데이터 전송 성공]");
            
            // client가 데이터 받음
            byteBuffer = ByteBuffer.allocate(100);
            int byteCount = socketChannel.read(byteBuffer);
            byteBuffer.flip();
            String message = charset.decode(byteBuffer).toString();
            System.out.println"[데이터 수신 성공] : " + message);
            
        } catch(Exception e) {}
        
        if(socketChannel.isOpen()) {
            try {
                socketChannel.close();
                System.out.println"[연결 닫음]");
            } catch (IOException e1) {}
        }    
    }
}
cs



'Study > Java' 카테고리의 다른 글

자바 NIO 셀렉터  (0) 2017.07.05
Java Network 프로그래밍 기초  (0) 2017.06.03
JAVA - NIO  (0) 2017.05.29
Java - OOP_인터페이스와 다형성  (0) 2017.03.20
Java - OOP_클래스와 상속, 생성자, 오버로딩/오버라이딩  (0) 2017.03.17

+ Recent posts