import java.net.*;
import java.io.*;
class ServerDemo
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(11111);
Socket s = ss.accept();
/*
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufIn.readLine())!=null)
{
System.out.println(line);
}
为什么不行?? 客户端发来的请求数据头后面不是有空行吗?应该可以读到null,然后结束循环的,怎么用这个试了好几遍都不行
*/
System.out.println(s.getInetAddress().getHostAddress());
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();
//System.out.println("Hello World!");
}
}
|