Java套接字编程基础

本文节选自:http://www.builder.com.cn/2008/0414/813590.shtml


什么是套接字(Socket)?

Network API是典型的用于基于TCP/IP网络Java程序与其他程序通讯,Network API依靠Socket进行通讯。

Socket可以看成在两个程序进行通讯连接中的一个端点,一个程序将一段信息写入Socket中,该Socket将这段信息发送给另外一个Socket中,如图1

  我们来分析一下图1,Host A上的程序A将一段信息写入Socket中,Socket的内容被Host A的网络管理软件访问,并将这段信息通过Host A的网络接口卡发送到Host B,Host B的网络接口卡接收到这段信息后,传送给Host B的网络管理软件,网络管理软件将这段信息保存在Host B的Socket中,然后程序B才能在Socket中阅读这段信息。

  基于TCP/IP网络中的每一个主机均被赋予了一个唯一的IP地址,IP地址是一个32位的无符号整数,由于没有转变成二进制,因此通常以小数点分隔,如:198.163.227.6,正如所见IP地址均由四个部分组成,每个部分的范围都是0-255,以表示8位地址。

  每一个基于TCP/IP网络通讯的程序都被赋予了唯一的端口和端口号,端口是一个信息缓冲区,用于保留Socket中的输入/输出信息,端口号是一个16位无符号整数,范围是0-65535,以区别主机上的每一个程序(端口号就像房屋中的房间号),低于256的短口号保留给标准应用程序,比如pop3的端口号就是110,每一个套接字都组合进了IP地址、端口、端口号,这样形成的整体就可以区别每一个套接字t。


Java的Socket类

  当客户程序需要与服务器程序通讯的时候,客户程序在客户机创建一个socket对象。两个常用的构造函数是 Socket(InetAddress addr, int port) 和 Socket(String host, int port)。对于第一个InetAddress子类对象通过addr参数获得服务器主机的IP地址,对于第二个函数host参数包被分配到InetAddress对象中,如果没有IP地址与host参数相一致,那么将抛出UnknownHostException异常对象。两个函数都通过参数port获得服务器的端口号。假设已经建立连接了,网络API将在客户端基于Socket的流套接字中捆绑客户程序的IP地址和任意一个端口号,否则两个函数都会抛出一个IOException对象。

  如果创建了一个Socket对象,那么它可能通过调用Socket的 getInputStream()方法从服务程序获得输入流读传送来的信息,也可能通过调用Socket的 getOutputStream()方法获得输出流来发送消息。在读写活动完成之后,客户程序调用close()方法关闭流和流套接字,下面的代码创建了一个服务程序主机地址为198.163.227.6,端口号为13的Socket对象,然后从这个新创建的Socket对象中读取输入流,然后再关闭流和Socket对象。

Socket s = new Socket ("198.163.227.6", 13);
InputStream is = s.getInputStream ();
// Read from the stream.
is.close ();
s.close ();

Java实例:


// SSClient.java
import java.io.*;
import java.net.*;
class SSClient
{
 public static void main (String [] args)
 {
    String host = "localhost";
    // If user specifies a command-line argument, that argument
    // represents the host name.
    if (args.length == 1) host = args [0];
    BufferedReader br = null;
    PrintWriter pw = null;

    Socket s = null;
    try{
      // Create a socket that attempts to connect to the server
      // program on the host at port 10000.
      s = new Socket (host, 10000);
      // Create an input stream reader that chains to the socket's
      // byte-oriented input stream. The input stream reade
      // converts bytes read from the socket to characters. The
      // conversion is based on the platform's default character
      // set.
      InputStreamReader isr;
      isr = new InputStreamReader (s.getInputStream ());

      // Create a buffered reader that chains to the input stream
      // reader. The buffered reader supplies a convenient method // for reading entire lines of text.
      br = new BufferedReader (isr);
      // Create a print writer that chains to the socket's byte-
      // oriented output stream. The print writer creates an
      // intermediate output stream writer that converts
      // characters sent to the socket to bytes. The conversion
      // is based on the platform's default character set.
      pw = new PrintWriter (s.getOutputStream (), true);
      // Send the DATE command to the server.
      pw.println ("DATE");
      // Obtain and print the current date/time.
      System.out.println (br.readLine ());

      // Send the PAUSE command to the server. This allows several
      // clients to start and verifies that the server is spawning
      // multiple threads. pw.println ("PAUSE");
      // Send the DOW command to the server.
      pw.println ("DOW");
      // Obtain and print the current day of week.
      System.out.println (br.readLine ());
      // Send the DOM command to the server.
       pw.println ("DOM");
      // Obtain and print the current day of month.
      System.out.println (br.readLine ());
      // Send the DOY command to the server.
      pw.println ("DOY");
      // Obtain and print the current day of year.
      System.out.println (br.readLine ());
    }
    catch (IOException e)
    {
      // Exception process... 
    }
    finally
    {
      try{
        if (br != null) br.close ();
        if (pw != null) pw.close ();
        if (s != null) s.close ();
      }
      catch (IOException e){
       // Exception process... 
      }
    }
  }
}

SSClient创建了一个Socket对象与运行在主机端口10000的服务程序联系,主机的IP地址由host变量确定。

SSClient将获得Socket的输入输出流,围绕BufferedReader的输入流和PrintWriter的输出流对字符串进行读写操作就变得非常容易,SSClient个服务程序发出各种date/time命令并得到响应,每个响应均被打印,一旦最后一个响应被打印,将执行Try/Catch/Finally结构的Finally子串,Finally子串将在关闭Socket之前关闭BufferedReader 和 PrintWriter。




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值