publicclassMain{publicstaticvoidmain(String[] args){try{ServerSocket socket =newServerSocket(3333);System.out.println("套接字创建成功,等待连接");while(true){finalSocket con = socket.accept();System.out.println("有客户端接入");newThread(newRunnable(){publicvoidrun(){InputStream in;try{
in = con.getInputStream();byte[] data =newbyte[128];int len =0;
len = in.read(data);System.out.println("读到数据:"+newString(data,0,len));}catch(IOException e){// TODO Auto-generated catch block
e.printStackTrace();}}}).start();}}catch(IOException e){// TODO Auto-generated catch block
e.printStackTrace();}}}
客户端代码
publicclassMain{publicstaticvoidmain(String[] args){try{Socket client =newSocket("192.168.30.1",6666);OutputStream out = client.getOutputStream();Scanner sc =newScanner(System.in);String massage = sc.next();
out.write(massage.getBytes());InputStream in = client.getInputStream();byte[] data =newbyte[128];int len = in.read(data);System.out.println("得到服务端数据:"+newString(data,0,len));}catch(UnknownHostException e){// TODO Auto-generated catch block
e.printStackTrace();}catch(IOException e){// TODO Auto-generated catch block
e.printStackTrace();}}}
Socket服务端代码客户端代码服务端代码public class Main { public static void main(String[] args){ try { ServerSocket socket = new ServerSocket(3333); System.out.println("套接字创建成功,等待连接"); while(true){ final Socket con = socket.accept(); Syst