看看这个小程序哪出问题了(网络编程)

程猿薇茑
优质创作者: Java技术领域
领域专家: 后端开发技术领域
2014-12-25 04:36:50

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();
}

}


为什么控制台输出两遍信息
并且浏览器没显示服务器发来的信息?
...全文
369 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
skgary 2014-12-31
  • 打赏
  • 举报
回复
1024以下的端口少用。 3楼贴出来的第17行就是这个请求。 最后说一句,做这种应用,多看看相关的协议。
程猿薇茑 2014-12-26
  • 打赏
  • 举报
回复
有谁知道如何打开win8的7号端口?

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
shixitong 2014-12-26
  • 打赏
  • 举报
回复
//把这几个去掉,浏览器就能展示了
//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

响应完毕!
skgary 2014-12-26
  • 打赏
  • 举报
回复
1. 浏览器发了两次请求,第一次是拿内容,第二次是拿fav.ico。所以,你打印了两次 2. 你返回的回复不符合HTTP 协议的要求,所以浏览器显示不出结果 。 你自己看一下浏览器调试模式或开发模 式下net work这一页的东西,所有的header/body都必须符合规则
程猿薇茑 2014-12-26
  • 打赏
  • 举报
回复
引用 2 楼 skgary 的回复:
1. 浏览器发了两次请求,第一次是拿内容,第二次是拿fav.ico。所以,你打印了两次 2. 你返回的回复不符合HTTP 协议的要求,所以浏览器显示不出结果 。 你自己看一下浏览器调试模式或开发模 式下net work这一页的东西,所有的header/body都必须符合规则
fav.ico是神马啊,在哪里
程猿薇茑 2014-12-25
  • 打赏
  • 举报
回复
运行程序后在浏览器输入127.0.0.1查看效果

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧