首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

急请帮小弟我看一下-为何这个程序不能实现?先多谢了

2011-12-26 
急,急:请帮我看一下---为何这个程序不能实现?先谢谢了!我写了一个Client端和Server端,想同时把几个Client

急,急:请帮我看一下---为何这个程序不能实现?先谢谢了!
我写了一个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;


try   {
      ss=new   ServerSocket(8888);
bConnected=true;
}catch(BindException   eb){
System.out.println( "port   in   use..... ");
System.out.println( "please   close   other   program   and   continue ");
}catch(IOException   ei){
ei.printStackTrace();
}
try   {
Socket   s   =   ss.accept();
client   c=new   client(s);
new   Thread(c).start();
}   catch   (IOException   e)   {
e.printStackTrace();
}finally{
try   {
ss.close();
}   catch   (IOException   e)   {
e.printStackTrace();
}
}
}
class   client   implements   Runnable{
private   Socket   s=   null;
private   DataInputStream   dis=null;
boolean   bConnected=false;

public   client(Socket   s){
this.s=s;
try   {
dis=new   DataInputStream(s.getInputStream());
}   catch   (IOException   e)   {
e.printStackTrace();
}
bConnected=true;

}
public   void   run()   {
try{
while(bConnected){
  dis=new   DataInputStream(s.getInputStream());
  String   str=dis.readUTF();
  System.out.println(str);
}
}catch(SocketException   ee){
System.out.println( "the   client   is   over! ");
}catch   (IOException   e){
e.printStackTrace();
}finally{
try   {
if(dis   !=null)   dis.close();
if(s   !=null)       s.close();
}   catch   (IOException   e)   {
e.printStackTrace();
}
          }
}
}
}





[解决办法]
try {
Socket s = ss.accept();
client c=new client(s);
new Thread(c).start();
}
--------------------
这里要循环accept()!!!
[解决办法]
拜托养成个好习惯,别把两个类写一起,你的程序需要改的地方太多了,我直接贴我的代码给你好了
服务端
import java.net.*;
import java.io.*;

public class SocketServer extends Thread{
public SocketServer() {
}
int servP =0;
ServerSocket serversocket ;
DataInputStream isr = null;
PrintWriter pw = null;
public SocketServer(int port){
this.servP = port;
System.out.println( "8956端口开始启动! ");
}
public void run(){
Socket so = null;
try {
if (servP != 0) {
serversocket = new ServerSocket(servP);
so = serversocket.accept();
while (true) {
// new Thread(new RunaDeal(so)).start();
isr = new DataInputStream(so.getInputStream());
String result = isr.readLine();
System.out.println(result);
pw = new PrintWriter(so.getOutputStream());
pw.write( "receive ok!from server ");
pw.flush();
pw.close();
so.close();
so = serversocket.accept();
}

}



} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new SocketServer(8956).start();
}

}

[解决办法]
ding
[解决办法]
ding

热点排行