日期:2014-05-20  浏览次数:20729 次

急,急:请帮我看一下---为何这个程序不能实现?先谢谢了!
我写了一个Client端和Server端,想同时把几个Client端与Server端通过线程机制联结起来.但是加载第一个时没有问题,加载第二个时抛出一个异常java.net.ConnectException:   Connection   refused:   connect
at   java.net.PlainSocketImpl.socketConnect(Native   Method)
at   java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at   java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at   java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at   java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at   java.net.Socket.connect(Socket.java:519)
at   java.net.Socket.connect(Socket.java:469)
at   java.net.Socket. <init> (Socket.java:366)
at   java.net.Socket. <init> (Socket.java:179)
at   chatClient.connect(chatClient.java:37)
at   chatClient.launchFrame(chatClient.java:32)
at   chatClient.main(chatClient.java:75)


我的Client端程序是:
import   java.io.*;
import   java.net.*;
import   java.awt.*;
import   java.awt.event.*;

public   class   chatClient   extends   Frame   {

Socket   s   =   null;

DataOutputStream   dos   =   null;

TextField   texfid   =   new   TextField();

TextArea   taContent   =   new   TextArea();

public   void   launchFrame()   {
setLocation(300,   300);
this.setSize(400,   400);
add(texfid,   BorderLayout.SOUTH);
add(taContent,   BorderLayout.NORTH);
pack();
this.addWindowListener(new   WindowAdapter()   {
public   void   windowClosing(WindowEvent   e)   {
System.exit(0);
disconnect();

}

});
texfid.addActionListener(new   TFListener());
setVisible(true);
connect();
}

public   void   connect()   {
try   {
s   =   new   Socket( "127.0.1.1 ",   8888);
System.out.println( "connected ");
}   catch   (UnknownHostException   e)   {
e.printStackTrace();
}   catch   (IOException   e)   {
e.printStackTrace();
}

}

public   void   disconnect()   {
try   {
dos.close();
}   catch   (IOException   e)   {
e.printStackTrace();
}

}

private   class   TFListener   implements   ActionListener   {

public   void   actionPerformed(ActionEvent   e)   {
String   str   =   texfid.getText().trim();
taContent.setText(str);
texfid.setText( " ");
try   {
dos   =   new   DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
}   catch   (IOException   e1)   {
e1.printStackTrace();
}

}

}

public   static   void   main(String[]   args)   {
new   chatClient().launchFrame();
}

}


我的Server端程序是:
import   java.net.*;
import   java.io.*;

class   chatServer2   {
boolean   bConnected=false;
DataInputStream   dis=null;

public   static   void   main(String[]   args)   {
new   chatServer2().start();
}
public   void   start(){
ServerSocket   ss=null;