ClientDemo
package com.itheima_04; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ClientDemo { public static void main(String[] args) throws IOException { Socket s=new Socket("192.168.1.30 ",10000); OutputStream os = s.getOutputStream(); os.write("hello,tcp,我来了".getBytes()); InputStream is=s.getInputStream(); byte [] bys=new byte[1024]; int len=is.read(bys); String data=new String(bys,0,len); System.out.println("客户端:"+data); s.close(); } }
ServerDemo
package com.itheima_04; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { ServerSocket ss=new ServerSocket(10000); Socket s=ss.accept(); InputStream is=s.getInputStream(); byte [] bys=new byte[1024]; int len=is.read(bys); String data=new String(bys,0,len); System.out.println("服务器:"+data); OutputStream os=s.getOutputStream(); os.write("数据已经收到".getBytes()); } }