Java·Î ±¸ÇöÇÏ´Â °£´ÜÇÑ Client & Server ÇÁ·Î±×·¥ |
|
|
 |
14³â Àü |
ÀÚ¹Ù¿¡¼ Á¦°øÇÏ´Â ¼ÒÄÏ Å¬·¡½º¸¦ ÀÌ¿ëÇÏ¸é ´ÙÀ½°ú °°ÀÌ °£´ÜÇÏ°Ô 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
|
|
̵̧ : 351 |
̵̧
¸ñ·Ï
|
|