package tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class Send { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Socket s = new Socket("192.168.2.143", 10003); OutputStream os = s.getOutputStream(); os.write("我是你的tcp小可爱".getBytes()); InputStream is = s.getInputStream(); byte buf[] = new byte[1024]; int len = is.read(buf); String text = new String(buf,0,len); System.out.println(text); s.close(); } } package tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub ServerSocket serverSocket = new ServerSocket(10003); Socket s = serverSocket.accept(); InputStream is = s.getInputStream(); byte buf[] = new byte[1024]; int len = is.read(buf); String text = new String(buf,0,len); System.out.println(s.getInetAddress().getHostAddress() + ":" + text); OutputStream os = s.getOutputStream(); os.write("收到了".getBytes()); s.close(); serverSocket.close(); } }