import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Demo {
public static void main(String []args){
try {
Socket s = new Socket("www.sina.com.cn",80);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.println("GET /index.html HTTP/1.1");
out.println("Host: www.sina.com.cn");
out.println("contentType:text/html");
out.println();
String str = null;
while((str = reader.readLine())!=null){
System.out.println(str);
}
} catch (UnknownHostException e) {
} catch (IOException e) {
}
}
}
try代码段的第一行Socket s = new Socket("www.sina.com.cn",80);
为什么不首先创建一个端口ServerSocket server =new ServerSocket(8080);
然后调用服务器的accept方法完成与浏览器的对接 Socket s=server.accept();
【话唠】必须黑马、 2014/4/30 23:05:57
Socket s = new Socket("www.sina.com.cn",80);
这一步是不是隐式的完成了上面的动作么??
23:07:06
【话唠】必须黑马、 2014/4/30 23:07:06
那个端口8080 不对 应该是80 我就是表明那个意思 首先监听端口 然后调用方法与浏览器请求对接
|