这是我学习的时候写的一段代码:- public class TcpServer {
- public static void main(String[] args) throws IOException{
-
-
- ServerSocket ss=new ServerSocket(10004);
- Socket s=ss.accept();
- System.out.println(s.getLocalPort());
- System.out.println(s.getPort());
- System.out.println(s.getRemoteSocketAddress());
- InputStream in=s.getInputStream();
- byte[] buf=new byte[1024];
- int len=in.read(buf);
- System.out.println(new String(buf,0,len));
- OutputStream out=s.getOutputStream();
- out.write("hello!我已收到你的信息!".getBytes());
- ss.close();
- Boolean ff=null;
- }
- }
复制代码 虽然ServerSocket中没有etRemoteSocketAddress()方法,但是ServerSocket可以接收Socket对象,然后通过Socket对象获取客户端IP地址 |