需求:我想实现服务器有个时钟(节拍的意思,不是表阿)然后呢,时钟一直走 有客户端连接就在客户端显示服务器的时钟,支持多客户端。
问题:现在只要一开客户端,服务器端时钟就停了,而且客户端打印也有问题,请高手帮忙修改下,最好能指出问题,谢谢了//Server
import java.awt.*;
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.net.*; public class ServerTest implements Runnable
{
private boolean started; 
private ServerSocket ss; 
private DataInputStream dis; 
private DataOutputStream dos; 
private int i;    //计数器
private String sendStr;

private JFrame frame;
private JLabel label;
public final int INTERVAL=1000;

private JLabel label1;// 站位用

public void run()
{
//计数器实现
ActionListener taskPerformer=new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
sendStr=Integer.toString(i++);
label.setText(sendStr);
}
};
new Timer(INTERVAL,taskPerformer).start();
}
public ServerTest()
{
started=false;
ss=null;
i=0;
sendStr="";
dis=null;
dos=null;
InitialComponent();
Thread th=new Thread(this);
th.start();  
}

private void InitialComponent()
{
frame=new JFrame("Test");
frame.setSize(300,200);
frame.setLocation(300,200);
frame.setDefaultCloseOperation(3);

label1=new JLabel("                                                 ");
frame.add(label1,BorderLayout.WEST);

label=new JLabel("0");
frame.add(label,BorderLayout.CENTER);

frame.setVisible(true);
}

public void start() 

try 

ss = new ServerSocket(8888); 
started = true; 
System.out.println("Server Start!!!");

catch (BindException e)

System.out.println("端口使用中...."); 
System.out.println("请关掉相关程序并重新运行服务器!"); 
System.exit(0); 

catch (IOException e)

e.printStackTrace(); 


try 

while(started) 

Socket s = ss.accept(); 
ClientThread ct = new ClientThread(s); 
System.out.println("a client connected!"); 
new Thread(ct).start(); 


catch (IOException e) 
{
e.printStackTrace(); 

finally 

try 

ss.close(); 

catch (IOException e) 
{   
e.printStackTrace(); 




class ClientThread extends Thread//implements Runnable 
{  
private Socket s; 
private boolean bConnected; 

public ClientThread(Socket s) 
{
this.s = s; 
bConnected=false;  

InitialComponent();

ActionListener taskPerformer=new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
send(sendStr);
}
};
new Timer(INTERVAL,taskPerformer).start();
}

private void InitialComponent()
{
try 

dis = new DataInputStream(s.getInputStream()); 
dos = new DataOutputStream(s.getOutputStream());
bConnected = true; 
 

catch (IOException e) 

e.printStackTrace(); 



public void send(String send) 

try 
{
while(bConnected) 

dos.writeUTF(send); 
dos.flush(); 
this.sleep(1000);
}

catch (EOFException e) 

System.out.println("Client closed!"); 

catch (IOException e) 
{
//System.out.println("Client closed!");
//e.printStackTrace(); 

catch(Exception e)
{
}
finally 

try 

if(dis != null) dis.close(); 
if(dos != null) dos.close(); 
if(s != null) 

s.close();   
    } 

catch (IOException e1) 

e1.printStackTrace(); 




public void run() 
{
send(sendStr);



public static void main(String args[])
{
new ServerTest().start();
}
}//client
import java.io.*; 
import java.net.*; class ClientTest
{
private Socket s; 
private DataOutputStream dos; 
private DataInputStream dis; 
private boolean bConnected; 
Thread thread;

public ClientTest()
{
s=null;
dos=null;
dis=null;
bConnected=false;
thread = new Thread(new ClientThread());
}

public void Start()
{
Connect();
thread.start();
}

public void Connect() 

try 

s = new Socket("127.0.0.1", 8888); 
dos = new DataOutputStream(s.getOutputStream()); 
dis = new DataInputStream(s.getInputStream()); 
System.out.println("connected!"); 
bConnected = true; 

catch (UnknownHostException e) 

e.printStackTrace(); 

catch (IOException e) 

e.printStackTrace(); 



private class ClientThread implements Runnable 

public void run() 

try 

while(bConnected) 

String str = dis.readUTF(); 
System.out.println(str);


catch (SocketException e) 

System.out.println("退出了,bye!"); 

catch (EOFException e) 

System.out.println("退出了,bye - bye!"); 

catch (IOException e) 

e.printStackTrace(); 

finally 

try 

if(dis != null) dis.close(); 
if(dos != null) dos.close(); 
if(s != null) 

s.close(); 
//s = null; 
    } 

catch (IOException e1) 

e1.printStackTrace(); 





public static void main(String args[])
{
new ClientTest().Start();
}
}

解决方案 »

  1.   

    1楼主不应该定义一个start方法哦,这样容易混淆和混乱,个人感觉!2只要一开客户端,服务器端时钟就停了:是因为你的socket堵塞,或者IO关闭了,尝试用非堵塞IO-Socket
    3下面这个事件不知道是干什么用的哦...
    ActionListener taskPerformer=new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        send(sendStr);    
                    }
                };
    4private void InitialComponent这里你初始化了
     dis = new DataInputStream(s.getInputStream()); 
     dos = new DataOutputStream(s.getOutputStream());
    但是你在send方法里面关闭了这些流.send方法在run的线程中调用.
    所以以后的线程当然无法打开了.