扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Socket类
当客户程序需要与服务器程序通讯的时候,客户程序在客户机创建一个socket对象,Socket类有几个构造函数。两个常用的构造函数是 Socket(InetAddress addr, int port) 和 Socket(String host, int port),两个构造函数都创建了一个基于Socket的连接服务器端流套接字的流套接字。对于第一个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 ();
接下面我们将示范一个流套接字的客户程序,这个程序将创建一个Socket对象,Socket将访问运行在指定主机端口10000上的服务程序,如果访问成功客户程序将给服务程序发送一系列命令并打印服务程序的响应。List2使我们创建的程序SSClient的源代码:Listing 2: SSClient.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)
{
System.out.println (e.toString ());
}
finally
{
try
{
if (br != null)
br.close ();
if (pw != null)
pw.close ();
if (s != null)
s.close ();
} catch (IOException e)
{
}
}
}
}
运行这段程序将会得到下面的结果:
Tue Jan 29 18:11:51 CST 2002
TUESDAY
29
29
SSClient创建了一个Socket对象与运行在主机端口10000的服务程序联系,主机的IP地址由host变量确定。SSClient将获得Socket的输入输出流,围绕BufferedReader的输入流和PrintWriter的输出流对字符串进行读写操作就变得非常容易,SSClient个服务程序发出各种date/time命令并得到响应,每个响应均被打印,一旦最后一个响应被打印,将执行Try/Catch/Finally结构的Finally子串,Finally子串将在关闭Socket之前关闭BufferedReader 和 PrintWriter。
在SSClient源代码编译完成后,可以输入java SSClient 来执行这段程序,如果有合适的程序运行在不同的主机上,采用主机名/IP地址为参数的输入方式,比如www.sina.com.cn是运行服务器程序的主机,那么输入方式就是java SSClientwww.sina.com.cn。
技巧
Socket类包含了许多有用的方法。比如getLocalAddress()将返回一个包含客户程序IP地址的InetAddress子类对象的引用;getLocalPort()将返回客户程序的端口号;getInetAddress()将返回一个包含服务器IP地址的InetAddress子类对象的引用;getPort()将返回服务程序的端口号。
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)
{
}
}
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者