溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java的ServerSocket類(lèi)怎么用

發(fā)布時(shí)間:2022-01-14 19:56:28 來(lái)源:億速云 閱讀:162 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“Java的ServerSocket類(lèi)怎么用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Java的ServerSocket類(lèi)怎么用”吧!

ServerSocket類(lèi)

  由于SSClient使用了流套接字,所以服務(wù)程序也要使用流套接字。這就要?jiǎng)?chuàng)建一個(gè)ServerSocket對(duì)象,ServerSocket有幾個(gè)構(gòu)造函數(shù),最簡(jiǎn)單的是ServerSocket(int port),當(dāng)使用ServerSocket(int port)創(chuàng)建一個(gè)ServerSocket對(duì)象,port參數(shù)傳遞端口號(hào),這個(gè)端口就是服務(wù)器監(jiān)聽(tīng)連接請(qǐng)求的端口,如果在這時(shí)出現(xiàn)錯(cuò)誤將拋出IOException異常對(duì)象,否則將創(chuàng)建ServerSocket對(duì)象并開(kāi)始準(zhǔn)備接收連接請(qǐng)求。

  接下來(lái)服務(wù)程序進(jìn)入無(wú)限循環(huán)之中,無(wú)限循環(huán)從調(diào)用ServerSocket的accept()方法開(kāi)始,在調(diào)用開(kāi)始后accept()方法將導(dǎo)致調(diào)用線程阻塞直到連接建立。在建立連接后accept()返回一個(gè)最近創(chuàng)建的Socket對(duì)象,該Socket對(duì)象綁定了客戶程序的IP地址或端口號(hào)。

  由于存在單個(gè)服務(wù)程序與多個(gè)客戶程序通訊的可能,所以服務(wù)程序響應(yīng)客戶程序不應(yīng)該花很多時(shí)間,否則客戶程序在得到服務(wù)前有可能花很多時(shí)間來(lái)等待通訊的建立,然而服務(wù)程序和客戶程序的會(huì)話有可能是很長(zhǎng)的(這與電話類(lèi)似),因此為加快對(duì)客戶程序連接請(qǐng)求的響應(yīng),典型的方法是服務(wù)器主機(jī)運(yùn)行一個(gè)后臺(tái)線程,這個(gè)后臺(tái)線程處理服務(wù)程序和客戶程序的通訊。

  為了示范我們?cè)谏厦嬲劦降目畈⑼瓿蒘SClient程序,下面我們創(chuàng)建一個(gè)SSServer程序,程序?qū)?chuàng)建一個(gè)ServerSocket對(duì)象來(lái)監(jiān)聽(tīng)端口10000的連接請(qǐng)求,如果成功服務(wù)程序?qū)⒌却B接輸入,開(kāi)始一個(gè)線程處理連接,并響應(yīng)來(lái)自客戶程序的命令。下面就是這段程序的代碼:

  Listing 3: SSServer.JavaXML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

// 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)
    {
    }
   }
  }
}


  運(yùn)行這段程序?qū)⒌玫较旅娴妮敵觯?/p>

Server starting...
Accepting Connection...
Closing Connection...


  SSServer的源代碼聲明了一對(duì)類(lèi):SSServer 和ServerThread;SSServer的main()方法創(chuàng)建了一個(gè)ServerSocket對(duì)象來(lái)監(jiān)聽(tīng)端口10000上的連接請(qǐng)求,如果成功, SSServer進(jìn)入一個(gè)無(wú)限循環(huán)中,交替調(diào)用ServerSocket的 accept() 方法來(lái)等待連接請(qǐng)求,同時(shí)啟動(dòng)后臺(tái)線程處理連接(accept()返回的請(qǐng)求)。線程由ServerThread繼承的start()方法開(kāi)始,并執(zhí)行ServerThread的run()方法中的代碼。

  一旦run()方法運(yùn)行,線程將創(chuàng)建BufferedReader, PrintWriter和 Calendar對(duì)象并進(jìn)入一個(gè)循環(huán),這個(gè)循環(huán)由讀(通過(guò)BufferedReader的 readLine())來(lái)自客戶程序的一行文本開(kāi)始,文本(命令)存儲(chǔ)在cmd引用的string對(duì)象中,如果客戶程序過(guò)早的關(guān)閉輸出流,會(huì)發(fā)生什么呢?答案是:cmd將得不到賦值。

  注意必須考慮到這種情況:在服務(wù)程序正在讀輸入流時(shí),客戶程序關(guān)閉了輸出流,如果沒(méi)有對(duì)這種情況進(jìn)行處理,那么程序?qū)a(chǎn)生異常。

  一旦編譯了SSServer的源代碼,通過(guò)輸入Java SSServer來(lái)運(yùn)行程序,在開(kāi)始運(yùn)行SSServer后,就可以運(yùn)行一個(gè)或多個(gè)SSClient程序。

到此,相信大家對(duì)“Java的ServerSocket類(lèi)怎么用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI