Java复习9网路编程

Java 复习9网路编程 20131008

前言:

         Java语言在网络通信上面的开发要远远领先于其他编程语言,这是Java开发中最重要的应用,可以基于协议的编程,如Socket,URLConnection等等,之后面的Web开发就是Servlet开发还有各种框架等等,基本上都是基于网络编程的。在上大二的时候,自己学习过Java网络编程的知识,现在好像都忘了,再有就是JSP,Servlet等等这些编程的知识也都忘得差不多了,所以这一段时间回忆一下关于网络编程和Web编程的知识。

1.网络通信编程

         TCP/IP(Transport Control Protocol / Internet Protocol) 是Internet最基本的协议。IP协议规定了数据传输的是的基本单元格式,数据包的递交办法和路由选择。TCP协议是提供的是可靠地面向对象的数据流传输服务的规则和约定。简单的说IP协议是单向的,TCP模式下,对方发送给你一个数据包,你要发送一个确认数据包给对方,这样来确认数据传输的可靠性。

2.使用URL进行网络连接

         URL(Uniform Resource Loactor): 用于完整描述internet网页的和其他资源唯一标识,通常用的构造方法有:

         public URL(String spec); URL url = new URL(“http://yangtengfei.duapp.com/index.php”)

         public URL(URL context, String spec);

         public URL(String protocol, String host, String file);

         public URL(String protocol,String host, int port, String file);

         使用URL获取指定的URL的数据流:

URL url  = null;

try {

    url = new URL("http://yangtengfei.duapp.com/index.html");

} catch (MalformedURLException e) {

    e.printStackTrace();

}

System.out.println("protocol:"+url.getProtocol());

System.out.println("host:"+ url.getHost());

try {

    System.out.println("content:" + url.getContent().toString());

} catch (IOException e) {

    e.printStackTrace();

}

System.out.println("port:" + url.getPort());

System.out.println("file:" + url.getFile());

System.out.println("Ref: " + url.getRef());

System.out.println("UserInfo" + url.getUserInfo());

try {

    url.openConnection();

} catch (IOException e) {

    e.printStackTrace();

}

try {

    BufferedInputStream bis = new BufferedInputStream(url.openStream());

    byte []buf = new byte[100];

    int count = -1;

    while((count=bis.read(buf, 0, 100))  != -1){

       System.out.print(new String(buf,0,count));

    }

} catch (IOException e) {

    e.printStackTrace();

}

3.Socket编程

         Java定义了Socket和ServerSocket两个类socket表示客户端,ServerSocket表示服务器端。

         构造函数:

         Socket(InetAddress address, int port [, boolean stream]);

         Socket(String host,int port[, boolean stream]);

         Socket(SocketImpl impl);

         Socket(String host,int port,InetAddress localAddress, int localport);

         Socket(InetAddress address, int port, InetAddress localAddr, int localport);

         ServerSocket(int port);

         ServerSocket(int port, int backlog, InetAddress binAddr/*local address*/);

Server 端编程:

         ss.accept() 接受一个连接,返回一个socket,然后通过socket打开输入输出流发送数据。

Socket socket = null;

BufferedInputStream  bis = null;

BufferedOutputStream bos = null;

try {

    socket = ss.accept();

    bis = new BufferedInputStream(socket.getInputStream());

    bos = new BufferedOutputStream(socket.getOutputStream());

} catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

当然Server段不可能只有一个客户端连接,这就需要在没接受一个连接的时候,将accept连接返回的socket作为参数触底给一个线程,让一个线程根据socket获得输入输出流,并且收发数据。同时数据流的收发建议使用Buffer缓存机制。

         网络编程中少不了是数据流操作,同时还有字节流的操作,对于网络传输的数据基本上都是基于字节流的,一般不会使用字符流,所以我们在数据流操作的时候需要使用字节流而不是字节流。

         字节流的操作,参看之前整理的输入输出流的笔记。

追梦的飞飞

于广州中山大学 20131008

HomePage: http://yangtengfei.duapp.com

原文地址:https://www.cnblogs.com/hbhzsysutengfei/p/3409535.html