}
// 启动服务器主程序
public static void main(String args[])
{
int port = 0;
if (args.length == 1)
{
try port = Integer.parseInt(args[0]);
catch(NumberFormatException e) port = 0;
}
new Server(port);
// End of the main
} // End of Server class
//以下定义了Connection 类,它是用来处理与客户机的所有通信的线程。
class Connection extends Thread
{
protected Socket client;
protected DataInputStream in;
protected PrintStream out;
// 初始化通信流并启动线程
public Connection(Socket client_socket)
{
client = client_socket;
try
{
in = new DataInputStream(client.getinputStream());
out = new PrintStream(client.getOutputStream());
}
catch(IOException e)
{
try client.close();
catch(IOException e2);
System.err.println("Exception while getting socket streram: " + e);
Return;
}
this.start;
} // End of Connection method
// 服务例程:读出一行文本;反转文本;返回文本。
public void run()
{
String line;
StringBuffer revline;
int len;
{
for(;;)
{
// Read a line
line = in.readline();
if(line == null) break;
// Reverse the line
len = line.length();
revline = new StringBuffer(len);
for(int i = len-1; i > =0; i--)
revline.insert(len-1-I;line.charAt(i));
// Write out the reverse line
out.println(revline);
}
catch(IOException e);
finally try client.close();
catch(IOException e2);
}
// End of run method
}
// End of Connection class
3、编写客户机类Java 程序
// Client.java
import java.io.*;
import java.net.*;
public class Client extends
{
public static final int Default_Port = 6543;
// 定义出错例程
public static final void usage()
{
System.out.println("Usage: Java Client []");
System.exit(0);
}
public static void main(String args[])
{
int port = Default_Port;
Socket s = null;
// 解析端口参数
if ((args.length != 1)&&(args.length != 2 )) usage();
if (args.length == 1)
port = Defau