4-07:如何检测和解决端口冲突问题
使用netstat命令查看当前正在被使用的端口号。
客户端程序:
package frame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
if (args.length<2)
{
System.out.println("需要指定2个参数,IP地址和端口号");
return;
}
Socket s = new Socket(args[0],Integer.parseInt(args[1]));
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
PrintWriter pw=new PrintWriter(ops,true);
BufferedReader br=new BufferedReader(new InputStreamReader(ips));
BufferedReader keyBoard=new BufferedReader(new InputStreamReader(System.in));
while(true){
String strWord=keyBoard.readLine();
pw.println(strWord);
if(strWord.equalsIgnoreCase("quit")){
break;
}
System.out.println(br.readLine());
}
br.close();
pw.close();
keyBoard.close();
s.close();
s=null;
}
}
TCP网络上传递对象:
对象类:
frame;
java.io.Serializable;
publicclassimplements
; String ; ; String ; Student( id, String name, age, String department) { . = id; . = name; . = age; . = department; } }package
import
java.io.ObjectOutputStream;import
java.net.ServerSocket;import
publicclass
main(String[] args) IOException {
new
new
new"zhangsan""shuxue"
}package
import
java.io.ObjectInputStream;import
publicclass
publicstaticvoidthrows
new"127.0.0.1"
new
System..println(<FONT face=""" color="#2a00ff">"学生<FONT face=""" color="#2a00ff">id:"id
out"学生<FONT face=""" color="#2a00ff">name:"name
out"学生<FONT face=""" color="#2a00ff">age:"age
out"学生<FONT face=""" color="#2a00ff">depatment:"department
}
4-08:
URL的基本组成:协议、主机名、端口号、资源名
例如:http://www.it315.org:8080/index.html
相对URL,例如:/a.html ./a.html ../../a.html a.html
RUL编码规则:
空格转换为加号(+)
0-9 a-z A-Z字符保持不变
其他字符,用字符集编码在内存中的十六进制格式表示
HTTP协议的会话过程
HTTP请求信息:
一个完整的请求信息包括:一个请求行、若干消息头、实体内容
HTTP响应信息:
一个完整的响应信息包括:一个状态行、若干消息头、实体内容
需要了解的几个HTTP消息头
1、 connection: Keep-Alive 和 close
2、 Accept-Language:可以指定多个,用逗号分隔
3、 Content-Length:实体内容的长度
4、 Range:指定服务器只需返回的部分内容及范围,格式
1】 Range:bytes=100-599
2】 Range:bytes=100-
3】 Range:bytes=-100
5、 Content-Range:指定服务器返回的部分实体内容的位置信息:例如
Content-Range:bytes:2543-4523
4-09:Url类
构造函数
public URL(String spec);
public URL(String protocol,String host,int port,String file);
public URL(String protocol,String host,int port,String file,URLStreamHandler handler);
public URL(URL context,String spec);
getProtocol、getHost、getPort、getFile等方法
openConnection方法返回URLConnection对象
工厂设计模式:
URL类的静态方法:
SetURLStreamHandlerFactory(URLStreamHandlerFactory fac).
URLStreamHandlerFactory方法:
CreateURLStreamHandlerFactory(String protocol)
工厂模式的工作原理
URLConnection 是HttpURLConnection的父类
一个HTTP连接可以被多个HttpURLConnection实例共享,调用HttpURLConnection的disconnect()可以关闭底层共享网络
4-10:编程实例
frame;
java.io.BufferedReader;import
java.io.InputStreamReader;import
java.net.URL;import
java.util.Map;import
publicclass
publicstaticvoidthrows
out"获取日文页面<FONT face=""" color="#2a00ff">"
"ja"
out"/n"
out"获取中文页面<FONT face=""" color="#2a00ff">"
"zh"
publicstaticvoidthrows
new"http://www.google.com"
"Accept-Language"
while
out":"
while
out":"
} InputStream is=googleConnection.getInputStream(); BufferedReader br= BufferedReader( InputStreamReader(is)); String strLine=; ((strLine=br.readLine())!=) { System..println(strLine); } br.close(); googleConnection.disconnect(); }
2459

被折叠的 条评论
为什么被折叠?



