반응형
자바에서 제공하는 소켓 클래스를 이용하면 다음과 같이 간단하게 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
// 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
반응형
'웹프로그램' 카테고리의 다른 글
초간단 AJAX 샘플 (0) | 2009.06.09 |
---|---|
IIS에서 JSP or Java Servlet 돌리는 방법 (0) | 2009.06.09 |
JDK 다운로드 및 설치 방법 (0) | 2009.06.09 |
친절한 톰캣(Tomcat)의 기능 개요 및 설치 방법 (0) | 2009.06.09 |
JSP 페이지 이동 4가지 방법 및 특성 (0) | 2009.06.09 |
JPG 저장된 DB 이미지 JSP로 보여주기 (0) | 2009.06.09 |
JSP 폼메일 예제 (0) | 2009.06.09 |
JSP include 집중해부 (0) | 2009.06.09 |