网络编程2

 

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】  Rangebytes=100-599

2】  Rangebytes=100-

3】  Rangebytes=-100

5、  Content-Range:指定服务器返回的部分实体内容的位置信息:例如

Content-Rangebytes2543-4523

 

4-09Url

构造函数

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

 

getProtocolgetHostgetPortgetFile等方法

openConnection方法返回URLConnection对象

 

工厂设计模式:

URL类的静态方法:

SetURLStreamHandlerFactory(URLStreamHandlerFactory fac).

URLStreamHandlerFactory方法:

CreateURLStreamHandlerFactory(String protocol)

 

工厂模式的工作原理

 

URLConnection HttpURLConnection的父类

 

一个HTTP连接可以被多个HttpURLConnection实例共享,调用HttpURLConnectiondisconnect()可以关闭底层共享网络

 

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值