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 sockets
// byte-oriented input stream. the input stream reader
// converts bytes read from the socket to characters. the
// conversion is based on the platforms 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 sockets 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 platforms 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 ssclient www.sina.com.cn。
技巧
socket类包含了许多有用的方法。比如getlocaladdress()将返回一个包含客户程序ip地址的inetaddress子类对象的引用;getlocalport()将返回客户程序的端口号;getinetaddress()将返回一个包含服务器ip地址的inetaddress子类对象的引用;getport()将返回服务程序的端口号。
