这是我的服务器的源代码:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Server extends Frame implements Runnable{
TextArea txt=new TextArea();
ServerSocket server;
Socket client;
DataInputStream in;
PrintStream out;
ObjectOutputStream objout;
Thread serverThread;
String s1;
String s2;
String s=new String();
Vector vector_user=new Vector();
Vector vector_msg=new Vector();
public Server(){
super("服务器");
Panel p=new Panel();
p.setLayout(null);
txt.setBounds(30,50,300,300);
p.add(txt);
add(p);
setSize(360,400);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
server=new ServerSocket(4000);
serverThread=new Thread(this);
txt.append("线程已经初始化!");
txt.append("\n");
serverThread.start();
txt.append("线程已经开始!");
txt.append("\n");
}catch(Exception e){}
}
public void run(){
try{
while(true){
client=server.accept();
txt.append("已连接到客户机:"+client.getInetAddress().getHostName()+"\n\n");
in=new DataInputStream(client.getInputStream());
txt.append("输入已经初始化!");
txt.append("\n");
out=new PrintStream(client.getOutputStream());
txt.append("输出已经初始化!");
txt.append("\n");
txt.append("\n");
objout=new ObjectOutputStream(client.getOutputStream());
txt.append("ObjectOutputStream已经初始化!");
txt.append("\n");
txt.append("\n");
verify();
txt.append("verify已经调用!");
txt.append("\n");
}
}catch(Exception e){}
}
public void verify(){
while(true){
try{
String fromClientStr=null;
//
fromClientStr=in.readLine();
txt.append("已经读取来自客户端的信息!"+fromClientStr);
txt.append("\n");
//}
if(fromClientStr.equals("SEND")){
txt.append("客户端发来信息");
txt.append("\n");
s1=in.readLine();
txt.append("客户:"+s1);
txt.append("\n");
vector_user.addElement((String)s1);
txt.append("s1已经打包!");
txt.append("\n");
s2=in.readLine();
txt.append("信息内容:"+s2);
txt.append("\n");
s+=s1+":"+"\n"+s2;
txt.append("s="+s);
vector_msg.addElement((String)s);
txt.append("s2已经打包!");
txt.append("\n");
objout.writeObject(vector_user);
//out.println(s1);
txt.append("客户已经返回");
txt.append("\n");
objout.writeObject(vector_msg);
//out.println(s2);
txt.append("信息内容已经返回");
txt.append("\n");
txt.append("\n");
}
else if(fromClientStr.equals("TIMER")){
txt.append("客户发来TIMER信息");
txt.append("\n");
objout.writeObject(vector_user);
txt.append("TIMER已经返回");
txt.append("\n");
objout.writeObject(vector_msg);
txt.append("TIMER已经返回");
txt.append("\n");
txt.append("\n");
}
//}
}catch(Exception e){
System.out.println("I/O有误!");
System.out.println(e);
txt.append("I/O有误!");
//txt.append(e);
txt.append("\n");
}
}
}
public static void main(String Args[]){
new Server();
}
}
这是我的客户机的源代码:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Client extends Frame implements ActionListener{
TextArea txtMessages=new TextArea();
TextArea txtUserList=new TextArea();
TextField txtMsg=new TextField();
Button btnSend=new Button("发送");
Button btnExit=new Button("退出");
Socket client;
//DataInputStream in;
public ObjectInputStream objin;
PrintStream out;
String name;
String msg;
int messageCount=0;
public Client(){
String nm="zp";
name=new String(nm);
//super("客户端:"+name);
setTitle("客户端:"+name);
Panel p=new Panel();
p.setLayout(null);
txtMessages.setBounds(30,30,200,200);
p.add(txtMessages);
txtUserList.setBounds(250,30,70,200);
p.add(txtUserList);
txtMsg.setBounds(30,250,200,30);
p.add(txtMsg);
btnSend.setBounds(250,250,60,20);
btnSend.addActionListener(this);
p.add(btnSend);
btnExit.setBounds(330,250,60,20);
btnExit.addActionListener(this);
p.add(btnExit);
add(p);
setSize(400,500);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
client=new Socket(InetAddress.getLocalHost(),4000);
System.out.println("Socket已经初始化!");
//in=new DataInputStream(client.getInputStream());
objin=new ObjectInputStream(client.getInputStream());
System.out.println("输入已经初始化!");
out=new PrintStream(client.getOutputStream());
System.out.println("输出已经初始化!");
}catch(Exception ex){
System.out.println("Socket未初始化!");
System.out.println(ex);
}
}
Timer t=new Timer(5000,new TimerAction());
class TimerAction implements ActionListener{
Socket toServer;
ObjectInputStream objin;
PrintStream pout;
public void actionPerformed(ActionEvent e2){
try{
toServer=new Socket(InetAddress.getLocalHost(),4000);
System.out.println("TimerAction_Socket已经初始化!");
objin=new ObjectInputStream(toServer.getInputStream());
System.out.println("TimerAction_输入已经初始化!");
pout=new PrintStream(toServer.getOutputStream());
System.out.println("TimerAction_输出已经初始化!");
pout.println("TIMER");
System.out.println("TIMER已经发送!");
Vector vector_user=(Vector)objin.readObject();
System.out.println("TimerAction_收到消息!");
Vector vector_msg=(Vector)objin.readObject();
System.out.println("TimerAction_收到消息!");
int i=messageCount;
for(;i<vector_msg.capacity();i++){
txtMessages.append((String)vector_msg.elementAt(i));
txtMessages.append("\n");
}
messageCount=i;
for(int j=1;j<vector_user.capacity();j++){
txtUserList.append((String)vector_user.elementAt(j));
txtUserList.append("\n");
}
}catch(Exception ex){}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnSend){
try{
out.println("SEND");
System.out.println("SEND已经发送!");
out.println(name);
System.out.println("name已经发送!"+name);
out.println(txtMsg.getText());
System.out.println("msg已经发送!"+txtMsg.getText());
Vector vector_user=(Vector)objin.readObject();
System.out.println("接受到name!");
Vector vector_msg=(Vector)objin.readObject();
System.out.println("接受到msg!");
int i;//=messageCount;
for(i=0;i<vector_msg.size();i++){
txtMessages.append((String)vector_msg.elementAt(i));
txtMessages.append("\n");
messageCount++;
}
txtMessages.append(String.valueOf(messageCount));
txtMessages.append("\n");
//messageCount=i;
for(int j=0;j<vector_msg.size();j++){
txtUserList.setText("");
txtUserList.append((String)vector_user.elementAt(j));
txtUserList.append("\n");
}
txtMsg.setText("");
}catch(Exception ex){
System.out.println("I/O有误!");
System.out.println(ex);
}
//t.start();
}
else if(e.getSource()==btnExit){
System.exit(0);
}
}
public static void main(String Args[]){
new Client();
}
}
为什么我的客户机中只能显示第一条发出的信息?
我调用了javax.swing.Timer,想重复更新消息内容,为什么显示不出来?
而且还进入死循环????
天那,高手们,帮帮偶啦!!
3X!!!!!