标签:
class TcpClient { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.1.1", 10002); OutputStream out = s.getOutputStream(); out.write("服务端,你好".getBytes()); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf, 0, len)); s.close(); } }
class TcpServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10002); Socket s = ss.accept(); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf, 0, len)); OutputStream out = s.getOutputStream(); out.write("哥们收到,你好".getBytes()); s.close(); } }
标签:
原文地址:http://www.cnblogs.com/xxr2015/p/4621238.html