프로그래밍/ㆍC/C++/Java
[Java].class 소켓 통신 - 클라이언트 클래스
쪼재
2012. 2. 11. 00:46
import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class SocketClient { // 접속 시도하려는 IP, 포트번호 입력 public SocketClient(String host, int port) { try { Socket socket = new Socket(host, port); OutputStream os = socket.getOutputStream(); os.write("Hello".getBytes()); InputStream is = socket.getInputStream(); byte[] b = new byte[10240]; int readCount = is.read(b); System.out.println(new String(b, 0, readCount)); os.write("Hello".getBytes()); readCount = is.read(b); } catch (Exception e) { e.printStackTrace(); } } }