Java Network Programming(1) - Socket

Status
Publishing
Created By
Empty
Last Edited Time
7/13/2020, 3:33:00 PM
Description
Socket를 이용한 네트워크 통신
Tags
Java
네트워크
흐름
TCP 통신
C/C++
Java
Java 코드
Client
Socket socket = new Socket(); socket.connect(new InetSocketAddress(host, port)); /* send "REQ" */ byte[] writeBuf = {'R', 'E', 'Q'}; socket.getOutputStream().write(writeBuf, 0, writeBuf.length); /* recv byte */ byte[] readBuf = new byte[16]; socket.getInputStream().read(readBuf, 0, readBuf.length); socket.close();
Java
Server
ServerSocket serverSocket = new ServerSocket(6000); Socket socket = serverSocket.accept(); /* recv byte */ byte[] readBuf = new byte[16]; socket.getInputStream().read(readBuf, 0, readBuf.length); /* send "RES" */ byte[] writeBuf = {'R', 'E', 'S'}; socket.getOutputStream().write(writeBuf, 0, writeBuf.length); serverSocket.close();
Java