import java.net.*;
import java.io.*;
public class Client {
public static void main (String[] args)throws Exception {
new TCPClient().connect();
}
}
class TCPClient{
private static final int port=8002 ;
public void connect () throws Exception{
Socket client =new Socket(InetAddress.getLocalHost(),port);
InputStream in=client.getInputStream();
byte [] buf=new byte [1024 ];
int len=in.read(buf);
System.out.println(new String(buf,0 ,len));
client.close();
}
}
import java.io.*;
import java.net.*;
public class Sever {
public static void main (String[] args)throws Exception {
new TcpServer().listen();
}
}
class TcpServer{
private static final int port=8002 ;
public void listen () throws Exception{
ServerSocket serverSocket=new ServerSocket (port);
Socket client=serverSocket.accept();
OutputStream out =client.getOutputStream();
System.out .println("开始与客户端交互数据" );
out .write(("Hello world" ).getBytes());
System.out .println("结束与客户端交互数据" );
out .close();
client.close();
}
}