62,620
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class WebServer {
protected void start() {
ServerSocket s;
System.out.println("WebServer starting up on port 80:");
System.out.println("press ctrl-c to exit");
try {
s=new ServerSocket(80);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error:"+e);
return;//终止程序
}
//---------------------------------------------------
System.out.println("Waiting for connection......");
for(;;){
try {
Socket remote=s.accept();//阻塞
System.out.println("Connected,sending data");
BufferedReader br=new BufferedReader(new InputStreamReader(remote.getInputStream()));
PrintWriter out=new PrintWriter(remote.getOutputStream());
String res="客户端发来的信息:";
System.out.println(res);
while(!res.equals("")){//只要没有读到空行就继续
res=br.readLine();
System.out.println(res);
}
//向客户端(浏览器)发送信息
out.println("HTTP/1.0 200 OK");
out.println("Content-type:text/html");
out.println("Server:Bot");
out.println("<h1>浏览器,你好!</h1>");
out.flush();//防止缓冲区还有内容没有送出
System.out.println("响应完毕!");
remote.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error:"+e);
}
}
}
public static void main(String[] args) {
new WebServer().start();
}
}
package test;
/*
* 使用socket的getOutputStream和getInputStream实现一个简单的echo客户端
* 用户在控制台键入的数据被返回发送给服务器
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class EchoClient {
public static void main(String[] args) {
String hostname = "localhost";
BufferedReader networkIn = null;
PrintWriter out = null;
try {
Socket s=new Socket(hostname,7);
//分别获取此socket连接的OutputStream和InputStream
networkIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader userIn=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(s.getOutputStream());
System.out.println("Connected to echo server,please enter data");
while(true){
String theLine=userIn.readLine();
if(theLine.equals("exit")){
break;
}
out.println(theLine);//把用户在控制台输入的数据写入socket的流中
out.flush();//网络缓冲的数据写入流中
System.out.println(networkIn.readLine());//从socket的流中读取数据并向控制台输出
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(networkIn!=null)
networkIn.close();
if(out!=null)out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
此程序链接到本地7号端口没成功,我是win8//把这几个去掉,浏览器就能展示了
//out.println("HTTP/1.0 200 OK");
//out.println("Content-type:text/html");
//out.println("Server:Bot");
out.println("<h1>浏览器,你好!</h1>");
out.flush();//防止缓冲区还有内容没有送出
System.out.println("响应完毕!");
不过有点奇怪的是,我用IE测试是响应两遍(发了两遍请求)就象1楼说的
但是我用火狐测试显示3遍
具体内容如下
WebServer starting up on port 80:
press ctrl-c to exit
Waiting for connection......
Connected,sending data
客户端发来的信息:
GET / HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET4.0C; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Shuame)
Accept-Encoding: gzip, deflate
Host: localhost
Connection: Keep-Alive
响应完毕!
Connected,sending data
客户端发来的信息:
GET /favicon.ico HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET4.0C; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Shuame)
Host: localhost
Connection: Keep-Alive
响应完毕!
Connected,sending data
客户端发来的信息:
GET / HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
响应完毕!