import java.io.*;
import java.net.*;
class ServerDemo
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10100);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println("不要乱进网站,容易中毒");
s.close();
ss.close();
}
}
这里要时刻注意,当用浏览器连接服务器的时候,端口号的选择尤其重要(因为这个端口纠结了好久) |
|