| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 662 人关注过本帖
标题:请高手帮我看下我的聊天程序
取消只看楼主 加入收藏
zh_peng00100
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-11-12
收藏
 问题点数:0 回复次数:0 
请高手帮我看下我的聊天程序

这是我的服务器的源代码:

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!!!!!

搜索更多相关主题的帖子: import java 源代码 服务器 Vector 
2005-11-12 21:03
快速回复:请高手帮我看下我的聊天程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023084 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved