Java로 구현하는 간단한 Client & Server 프로그램 | |||
| |||
// Client Socket socket = new Socket("www.foo.com", 9000); OutputStream out = socket.getOutputStream(); out.write("Hello world".getBytes()); out.flush(); out.close(); // only if you want one request-response byte[] buffer = new byte[2048]; // just a sample size in = socket.getInputStream(); int bytesRead = in.read(buffer); in.close(); // only if you want one request-response System.out.println(new String(buffer, 0, bytesRead)); // Server (www.foo.com) ServerSocket server = new ServerSocket(9000); Socket socket = server.accept(); // Normally, the accept() is in a threading loop to keep processing InputStream in = socket.getInputStream(); byte[] buffer = new byte[2048]; // just a a sample size int bytesRead = in.read(buffer); in.close(); // only if you want one request-response OutputStream out = socket.getOutputStream(); out.write("I received the following from you: ".getBytes()); out.write(value, 0, bytesRead); out.flush(); out.close(); // only if you want one request-response Tags: socket 소켓 | |||
| |||
| |||
Login for comment |
SIMILAR POSTS 소켓 프로그래밍 기본 흐름 - Socket Programming Basic Flow |