將想法與焦點(diǎn)和您一起共享

Java網(wǎng)絡(luò )編程基礎(四) ServerSocket類(lèi)使用發(fā)布者:本站     時(shí)間:2020-05-06 16:05:46

由于SSClient使用了流套接字,所以服務(wù)程序也要使用流套接字。

這就要創(chuàng )建一個(gè)ServerSocket對象,ServerSocket有幾個(gè)構造函數,最簡(jiǎn)單的是ServerSocket(int port),當使用ServerSocket(int port)創(chuàng )建一個(gè)ServerSocket對象,port參數傳遞端口號,這個(gè)端口就是服務(wù)器監聽(tīng)連接請求的端口,如果在這時(shí)出現錯誤將拋出IOException異常對象,否則將創(chuàng )建ServerSocket對象并開(kāi)始準備接收連接請求。

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

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

為了示范我們在上面談到的慨念并完成SSClient程序,下面我們創(chuàng )建一個(gè)SSServer程序,程序將創(chuàng )建一個(gè)ServerSocket對象來(lái)監聽(tīng)端口10000的連接請求,如果成功服務(wù)程序將等待連接輸入,開(kāi)始一個(gè)線(xiàn)程處理連接,并響應來(lái)自客戶(hù)程序的命令。下面就是這段程序的代碼:

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 redivsents 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



選擇我們,優(yōu)質(zhì)服務(wù),不容錯過(guò)
1. 優(yōu)秀的網(wǎng)絡(luò )資源,強大的網(wǎng)站優(yōu)化技術(shù),穩定的網(wǎng)站和速度保證
2. 15年上海網(wǎng)站建設經(jīng)驗,優(yōu)秀的技術(shù)和設計水平,更放心
3. 全程省心服務(wù),不必擔心自己不懂網(wǎng)絡(luò ),更省心。
------------------------------------------------------------
24小時(shí)聯(lián)系電話(huà):021-58370032
最新国产精品第二页_色资源av中文无码先锋_中国xx爽69护士_日韩欧美亚洲每日更新在线观看