import java.io.*;
import java.net.*;
class TcpClient{
public static void main(String []args)throws Exception{//声明异常
Socket s =new Socket("127.0.0.1",10003);
OutputStream out =s.getOutputStream();
out.write("TCP 我来了!".getBytes());
s.close();
}
}
public class TcpServer{
public static void main(String []args)throws Exception{//此处static写错,再声明异常就OK了
ServerSocket ss =new ServerSocket(10003);
Socket s=ss.accept();
String ip =s.getInetAddress().getHostAddress();
System.out.println("ip:"+ip+"connected");
InputStream in =s.getInputStream();
byte []buf =new byte[1024];
System.out.println(new String(buf,0,in.read(buf)));
s.close();
ss.close();
}
} |
|