首页 > 开发 > Java > 正文

Java套接字编程(上)(3)

2024-07-13 09:54:50
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

serversocket类

  由于ssclient使用了流套接字,所以服务程序也要使用流套接字。这就要创建一个serversocket对象,serversocket有几个构造函数,最简单的是serversocket(int port),当使用serversocket(int port)创建一个serversocket对象,port参数传递端口号,这个端口就是服务器监听连接请求的端口,如果在这时出现错误将抛出ioexception异常对象,否则将创建serversocket对象并开始准备接收连接请求。

  接下来服务程序进入无限循环之中,无限循环从调用serversocket的accept()方法开始,在调用开始后accept()方法将导致调用线程阻塞直到连接建立。在建立连接后accept()返回一个最近创建的socket对象,该socket对象绑定了客户程序的ip地址或端口号。

  由于存在单个服务程序与多个客户程序通讯的可能,所以服务程序响应客户程序不应该花很多时间,否则客户程序在得到服务前有可能花很多时间来等待通讯的建立,然而服务程序和客户程序的会话有可能是很长的(这与电话类似),因此为加快对客户程序连接请求的响应,典型的方法是服务器主机运行一个后台线程,这个后台线程处理服务程序和客户程序的通讯。

  为了示范我们在上面谈到的慨念并完成ssclient程序,下面我们创建一个ssserver程序,程序将创建一个serversocket对象来监听端口10000的连接请求,如果成功服务程序将等待连接输入,开始一个线程处理连接,并响应来自客户程序的命令。下面就是这段程序的代码:

  listing 3: ssserver.java

// ssserver.java

import java.io.*;
import java.net.*;
import java.util.*;

class ssserver
{
 public static void main (string [] args) throws ioexception
 { 
  system.out.println ("server starting.../n");

  // create a server socket that listens for incoming connection
  // requests on port 10000.

  serversocket server = new serversocket (10000);

  while (true)
  {
   // listen for incoming connection requests from client
   // programs, establish a connection, and return a socket
   // object that represents this connection.

   socket s = server.accept ();

   system.out.println ("accepting connection.../n");

   // start a thread to handle the connection.

   new serverthread (s).start ();
  }
 }
}

class serverthread extends thread
{
 private socket s;

 serverthread (socket s)
 {
  this.s = s;
 }

 public void run ()
 {
  bufferedreader br = null;
  printwriter pw = null;

  try
  {
   // create an input stream reader that chains to the socket's
   // byte-oriented input stream. the input stream reader
   // 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);

   // create a calendar that makes it possible to obtain date
   // and time information.

   calendar c = calendar.getinstance ();

   // because the client program may send multiple commands, a
   // loop is required. keep looping until the client either
   // explicitly requests termination by sending a command
   // beginning with letters bye or implicitly requests
   // termination by closing its output stream.

   do
   {
    // obtain the client program's next command.

    string cmd = br.readline ();

    // exit if client program has closed its output stream.

    if (cmd == null)
     break;
  
    // convert command to uppercase, for ease of comparison.

    cmd = cmd.touppercase ();

    // if client program sends bye command, terminate.

    if (cmd.startswith ("bye"))
     break;

    // if client program sends date or time command, return
    // current date/time to the client program.

    if (cmd.startswith ("date") || cmd.startswith ("time"))
     pw.println (c.gettime ().tostring ());

    // if client program sends dom (day of month) command,
    // return current day of month to the client program.

    if (cmd.startswith ("dom"))
     pw.println ("" + c.get (calendar.day_of_month));

    // if client program sends dow (day of week) command,
    // return current weekday (as a string) to the client
    // program.

    if (cmd.startswith ("dow"))
     switch (c.get (calendar.day_of_week))
    {
     case calendar.sunday : pw.println ("sunday");
      break;

     case calendar.monday : pw.println ("monday");
      break;

     case calendar.tuesday : pw.println ("tuesday");
      break;

     case calendar.wednesday: pw.println ("wednesday");
      break;

     case calendar.thursday : pw.println ("thursday");
      break;

     case calendar.friday : pw.println ("friday");
      break;

     case calendar.saturday : pw.println ("saturday");
    }

    // if client program sends doy (day of year) command,
    // return current day of year to the client program.

    if (cmd.startswith ("doy"))
     pw.println ("" + c.get (calendar.day_of_year));

     // if client program sends pause command, sleep for three
     // seconds.
 
    if (cmd.startswith ("pause"))
    try
    {
     thread.sleep (3000);
    }
    catch (interruptedexception e)
    {
    }
   }
   while (true);
   {
   catch (ioexception e)
   {
    system.out.println (e.tostring ());
   }
   finally
   {
    system.out.println ("closing connection.../n");

    try
    {
     if (br != null)
      br.close ();

      if (pw != null)
       pw.close ();

      if (s != null)
       s.close ();
    }
    catch (ioexception e)
    {
    }
   }
  }
}

  运行这段程序将得到下面的输出:

server starting...
accepting connection...
closing connection...

  ssserver的源代码声明了一对类:ssserver 和serverthread;ssserver的main()方法创建了一个serversocket对象来监听端口10000上的连接请求,如果成功, ssserver进入一个无限循环中,交替调用serversocket的 accept() 方法来等待连接请求,同时启动后台线程处理连接(accept()返回的请求)。线程由serverthread继承的start()方法开始,并执行serverthread的run()方法中的代码。

  一旦run()方法运行,线程将创建bufferedreader, printwriter和 calendar对象并进入一个循环,这个循环由读(通过bufferedreader的 readline())来自客户程序的一行文本开始,文本(命令)存储在cmd引用的string对象中,如果客户程序过早的关闭输出流,会发生什么呢?答案是:cmd将得不到赋值。

  注意必须考虑到这种情况:在服务程序正在读输入流时,客户程序关闭了输出流,如果没有对这种情况进行处理,那么程序将产生异常。

  一旦编译了ssserver的源代码,通过输入java ssserver来运行程序,在开始运行ssserver后,就可以运行一个或多个ssclient程序。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表