急!考试题,做一个聊天室。但不知道为什么不用正常运行。请各位高手帮忙看一下错在哪

时间:2021-05-20 15:39:27
如题,源码如下。
服务器:
package Server;
import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
private boolean bListening=true;
private Vector vClients=new Vector(50,1);

class Client
{
DataOutputStream dosRemoteOut;
String sName="";
}

Server()
{
ServerSocket serverSock;
try
{
serverSock=new ServerSocket(5001);
System.out.println("服务器已启动..............");
}
catch(IOException e)
{
System.out.println(e.getMessage()+":服务器创建失败");
return;
}
while(bListening)
{
try
{
Socket socket=serverSock.accept();
System.out.println(socket.getInetAddress().getHostAddress()+"已连接......");
DataInputStream disRemoteIn=new DataInputStream(socket.getInputStream());
//Client client=new Client();
//client.dosRemoteOut=dosRemoteOut;
//vClients.addElement(client);
new ServerMonitor(socket,disRemoteIn,this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+"连接失败");
}
}
}

synchronized Vector GetClients()
{
return vClients;
}

synchronized void AddClient(Client client)
{
vClients.addElement(client);
}

synchronized void RemoveClient(Client client)
{
vClients.removeElement(client);
}

class ServerMonitor extends Thread
{
/*从读取客户端消息格式:
1:代表登陆信息,格式:"1:用户名:密码";
2:代表注册信息,格式:"2:用户名:密码";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:收消息用户名:消息";
发送至客户端消息格式:
1:代表登陆信息,格式:"1:成功"或"1:失败";
2:代表注册信息,格式:"2:成功"或"2:失败";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:消息";
5:代表用户列表信息,格式:"5:用户名1,用户名2,用户名3......,用户名n";*/
private Socket sock;
private DataInputStream disRemoteIn;
private Server server;
private boolean listening=true;
private DataOutputStream dosRemoteOut;
private String sMessage,sFromName,sPassword,sToName,sTag;
private StringTokenizer stkMessage;
private Client client=new Client();
ServerMonitor(Socket sock,DataInputStream disRemoteIn,Server server)
{
try
{
this.sock=sock;
this.disRemoteIn=disRemoteIn;
this.server=server;
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
sMessage=disRemoteIn.readLine();
stkMessage=new StringTokenizer(sMessage,":");
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接失败");
}
}

synchronized public void run()
{
try
{
while(listening)
{
sMessage=disRemoteIn.readUTF();
if(sMessage==null)
{
System.out.println("Message NULL");
}
stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
char c=sTag.charAt(1);
switch(c)
{
case '1':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("xy")&&sPassword.equals("xy"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="1:成功";
SendMessage(sMessage,"");
}
else
{
sMessage="1:失败";
SendMessage(sMessage,"");
listening=false;
}
break;
}
case '2':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("zg")&&sPassword.equals("zg"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="2:成功";
SendMessage(sMessage,"");
}
else
{
sMessage="2:失败";
SendMessage(sMessage,"");
listening=false;
}
break;
}
case '3':
{
sFromName=stkMessage.nextToken();
sMessage="3:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"");
break;
}
case '4':
{
sFromName=stkMessage.nextToken();
sToName=stkMessage.nextToken();
sMessage="4:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"4");
break;
}
case '5':
{
sMessage="5:";
Vector vClients=server.GetClients();
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
sMessage=sMessage+":"+client.sName;
}
SendMessage(sMessage,"");
break;
}
}
}
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接丢失");
}
finally
{
try
{
if(dosRemoteOut!=null)
{
server.RemoveClient(client);
dosRemoteOut.close();
dosRemoteOut=null;
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
disRemoteIn=null;
}
if(sock!=null)
{
sock.close();
sock=null;
}
}
catch(IOException ioe)
{

}
}
}
public void SendMessage(String sMessage,String sTag)
{
 Vector vClients=server.GetClients();
 Client client;
 String sName;
 DataOutputStream dosDataOut;
 
 if(!sTag.equals("4"))
 {
  for(Enumeration e=vClients.elements();e.hasMoreElements();)
  {
  client=(Client)(e.nextElement());
  dosDataOut=client.dosRemoteOut;
  if(!dosDataOut.equals(dosRemoteOut))
  {
try
{
dosDataOut.writeUTF(sMessage);
}
catch(IOException x)
{
System.out.println(x.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
  }
 }
}
else
{
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
dosDataOut=client.dosRemoteOut;
sName=client.sName;
if(sName.equals(sToName))
{
try
{
dosDataOut.writeUTF(sMessage);
}
catch(IOException ex)
{
System.out.println(ex.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
}
}
}
}
}
public static void main(String s[])
{
new Server();
}
}

9 个解决方案

#1


客户端:
package Client;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Client
{
JDialog d=new JDialog();
JPanel panel=new JPanel();
JTextField tfRegName=new JTextField(10);
JPasswordField pfRegPwd=new JPasswordField(10);
JLabel lRegName=new JLabel("昵称:");
JLabel lRegPwd=new JLabel("密码:");
JButton bEnter=new JButton("登陆");
JButton bRegister=new JButton("注册");

JFrame fMain=new JFrame("JAVA聊天室");
JPanel pMessage=new JPanel();
JPanel pUserList=new JPanel();
JPanel pTalk=new JPanel();
JPanel pMain=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel pTalkAndMsg=new JPanel();
JTextArea taMessage=new JTextArea(25,40);
JScrollPane spMessage;
JScrollPane spUserList;
JTextField tfTalk=new JTextField(30);
JLabel lb1=new JLabel("对");
JLabel lb2=new JLabel("说:");
JList lUserList=new JList();
JComboBox cbUserList=new JComboBox();
int v,h;
Socket sock;
DataOutputStream dosRemoteOut;
String sLocalName,sPassword,sMessage,sTag,sRemoteName;

public Client()
{
d.getContentPane().add(panel);
panel.setLayout(new GridLayout(3,3));
panel.add(lRegName);
panel.add(tfRegName);
panel.add(lRegPwd);
panel.add(pfRegPwd);
panel.add(bEnter);
panel.add(bRegister);
d.setTitle("登陆");
d.setSize(300,200);
d.setVisible(true);
bEnter.addActionListener(new Enter());
bRegister.addActionListener(new Register());
}
void Connect()
{
try
{
sock=new Socket("127.0.0.1",5001);
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
sMessage=sTag+":"+sLocalName+":"+sPassword;
System.out.println(sMessage);
dosRemoteOut.writeUTF(sMessage);
new Monitor(this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接服务器失败");
}
}
class MouseEvt extends MouseAdapter
{
public void mouseCliecked(MouseEvent me)
{
String sName;
boolean bTag=false;
sName=lUserList.getSelectedValue().toString();

for(int i=0;i<=cbUserList.getItemCount();i++)
{
if(cbUserList.getItemAt(i).toString().equals(sName))
{
bTag=true;
break;
}
}
if(!bTag)
{
cbUserList.addItem(sName);
}
}
}
void InitMain()
{
fMain.getContentPane().add(pMain);
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
taMessage.setEditable(false);
spMessage=new JScrollPane(taMessage,v,h);
spUserList=new JScrollPane(lUserList,v,h);
lUserList.setVisibleRowCount(70);
MouseEvt evt=new MouseEvt();
lUserList.addMouseListener(evt);
pMessage.setLayout(new BorderLayout());
pMessage.add(spMessage,BorderLayout.CENTER);
pUserList.setLayout(new BorderLayout());
pUserList.add(spUserList,BorderLayout.CENTER);

p1.setLayout(new BorderLayout());
p1.add(lb1,BorderLayout.WEST);

cbUserList.addItem("所有人");
p1.add(cbUserList,BorderLayout.CENTER);
p1.add(lb2,BorderLayout.EAST);
p2.setLayout(new BorderLayout());
p2.add(tfTalk,BorderLayout.CENTER);
pTalk.setLayout(new BorderLayout());
pTalk.add(p1,BorderLayout.WEST);
pTalk.add(p2,BorderLayout.CENTER);

pTalkAndMsg.setLayout(new BorderLayout());
pTalkAndMsg.add(pMessage,BorderLayout.CENTER);
pTalkAndMsg.add(pTalk,BorderLayout.SOUTH);

pMain.setLayout(new BorderLayout());
pMain.add(pTalkAndMsg,BorderLayout.CENTER);
pMain.add(pUserList,BorderLayout.WEST);
/*发送至服务器消息格式:
1:代表登陆信息,格式:"1:用户名:密码";
2:代表注册信息,格式:"2:用户名:密码";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:收消息用户名:消息";
从读取服务器消息格式:
1:代表登陆信息,格式:"1:成功"或"1:失败";
2:代表注册信息,格式:"2:成功"或"2:失败";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:消息";
5:代表用户列表信息,格式:"5:用户名1,用户名2,用户名3......,用户名n";*/
tfTalk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
sRemoteName=cbUserList.getSelectedItem().toString();
if(!sRemoteName.equals("所有人"))
{
sMessage="3:"+tfTalk.getText();
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"对所有人说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
else
{
sRemoteName=cbUserList.getSelectedItem().toString();
sMessage="4:"+sLocalName+":"+sRemoteName+":"+tfTalk.getText();;
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"悄悄的对"+sRemoteName+"说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
}
catch(IOException x)
{
System.out.println(x.getMessage()+":连接丢失");
}
}
});

fMain.setSize(800,600);
fMain.setVisible(true);
}
class Monitor extends Thread
{
private Client client;

public Monitor(Client client)
{
this.client=client;
}

public void run()
{
DataInputStream disRemoteIn;

try
{
disRemoteIn=new DataInputStream(client.sock.getInputStream());
while(true)
{
sMessage=disRemoteIn.readUTF();
StringTokenizer stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
char c=sTag.charAt(1);
switch(c)
{
case '1':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"密码不正确或没有该用户!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '2':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"已经有一个相同的用户名!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '3':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"对所有人说: "+sMessage+"\n");
break;
}
case '4':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"悄悄的对你说: "+sMessage+"\n");
break;
}
case '5':
{
String[] sUserList=new String[50];
sUserList[1]=sLocalName;
for(int count=2;stkMessage.hasMoreTokens();++count)
{
sUserList[count]=stkMessage.nextToken();
}
lUserList.setListData(sUserList);
}
}
}
}
catch(IOException e)
{

}
}
}

class Enter implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="1";
d.setVisible(false);
Connect();
}
}

class Register implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="2";
d.setVisible(false);
Connect();
}
}
public static void main(String s[])
{
new Client();
}
}

#2


急用,请各位高手帮帮忙,帮帮小弟。谢谢

#3


(1) JDialog d=new JDialog();换用JFrame
(2) frame.show()

#4


我测了一下,发现好象是客户端不能正确读取服务器反回的登陆或注册信息。老是停在线程RUN方法的sMessage=disRemoteIn.readUTF();地方.

#5


看楼上的饿!!!

#6


服务器端执行不到run方法

#7


server.java
package Server;
import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
private boolean bListening=true;
private Vector vClients=new Vector(50,1);
private String responseLine;

class Client
{
DataOutputStream dosRemoteOut;
String sName="";
}

Server()
{
ServerSocket serverSock;
try
{
serverSock=new ServerSocket(5001);
System.out.println("服务器已启动..............");
}
catch(IOException e)
{
System.out.println(e.getMessage()+":服务器创建失败");
return;
}
while(bListening)
{
try
{
Socket socket=serverSock.accept();
System.out.println(socket.getInetAddress().getHostAddress()+"已连接......");
DataInputStream disRemoteIn=new DataInputStream(socket.getInputStream());


//Client client=new Client();
//client.dosRemoteOut=dosRemoteOut;
//vClients.addElement(client);
new ServerMonitor(socket,disRemoteIn,this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+"连接失败");
}
}
}

synchronized Vector GetClients()
{
return vClients;
}

synchronized void AddClient(Client client)
{
vClients.addElement(client);
}

synchronized void RemoveClient(Client client)
{
vClients.removeElement(client);
}

class ServerMonitor extends Thread
{
/*从读取客户端消息格式:
1:代表登陆信息,格式:"1:用户名:密码";
2:代表注册信息,格式:"2:用户名:密码";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:收消息用户名:消息";
发送至客户端消息格式:
1:代表登陆信息,格式:"1:成功"或"1:失败";
2:代表注册信息,格式:"2:成功"或"2:失败";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:消息";
5:代表用户列表信息,格式:"5:用户名1,用户名2,用户名3......,用户名n";*/
private Socket sock;
private DataInputStream disRemoteIn;
private Server server;
private boolean listening=true;
private DataOutputStream dosRemoteOut;
private String sMessage,sFromName,sPassword,sToName,sTag;
private StringTokenizer stkMessage;
private Client client=new Client();
ServerMonitor(Socket sock,DataInputStream disRemoteIn,Server server)
{

try
{
//String str=disRemoteIn.readUTF();
//System.out.println("Client accept: "+str);

this.sock=sock;
this.disRemoteIn=disRemoteIn;
this.server=server;
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
//sMessage = disRemoteIn.readUTF();
//System.out.println("Client accept: "+sMessage);

/*
BufferedReader d = new BufferedReader(new InputStreamReader(sock.getInputStream()));
responseLine = d.readLine();
System.out.println(responseLine);
sMessage=d.readLine();

while ((responseLine = d.readLine()) != null){
// 把每一行显示出来
System.out.println(responseLine);
if ( responseLine.indexOf("") != -1 )
break;
}*/
//stkMessage=new StringTokenizer(sMessage,":");
//System.out.println("Client accept: "+stkMessage);

}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接失败");
}
}

synchronized public void run()
{

try
{

while(listening)
{
//System.out.println("receive is ok");
sMessage=disRemoteIn.readUTF();
//System.out.println(sMessage);
if(sMessage==null)
{
System.out.println("Message NULL");
}
stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
//System.out.println(sTag);
char c=sTag.charAt(0);

switch(c)
{
case '1':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("xy")&&sPassword.equals("xy"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="1:成功";
System.out.println(sMessage);
SendMessage(sMessage,"");
}
else
{
sMessage="1:失败";
//SendMessage(sMessage,"");
dosRemoteOut.writeUTF(sMessage);
listening=false;
}
break;
}
case '2':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("zg")&&sPassword.equals("zg"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="2:成功";
SendMessage(sMessage,"");
}
else
{
sMessage="2:失败";
SendMessage(sMessage,"");
listening=false;
}
break;
}
case '3':
{
sFromName=stkMessage.nextToken();
sMessage="3:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"");
break;
}
case '4':
{
sFromName=stkMessage.nextToken();
sToName=stkMessage.nextToken();
sMessage="4:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"4");
break;
}
case '5':
{
sMessage="5:";
Vector vClients=server.GetClients();
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
sMessage=sMessage+":"+client.sName;
}
SendMessage(sMessage,"");
break;
}
}
}
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接丢失");
}
finally
{
try
{
if(dosRemoteOut!=null)
{
server.RemoveClient(client);
dosRemoteOut.close();
dosRemoteOut=null;
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
disRemoteIn=null;
}
if(sock!=null)
{
sock.close();
sock=null;
}
}
catch(IOException ioe)
{

}
}
}
public void SendMessage(String sMessage,String sTag)
{
 Vector vClients=server.GetClients();
 
 Client client;
 String sName;
 DataOutputStream dosDataOut;
 System.out.println("Begin to sent message now");
 if(!sTag.equals("4"))
 {
  for(Enumeration e=vClients.elements();e.hasMoreElements();)
  {
  System.out.println("message is sent");
  client=(Client)(e.nextElement());
  dosDataOut=client.dosRemoteOut;
 
  if(dosDataOut.equals(dosRemoteOut))
  {
try
{
System.out.println("sent over");
dosDataOut.writeUTF(sMessage);
}
catch(IOException x)
{
System.out.println(x.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
  }
 
 }
}
else
{
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
dosDataOut=client.dosRemoteOut;
sName=client.sName;
if(sName.equals(sToName))
{
try
{
dosDataOut.writeUTF(sMessage);
}
catch(IOException ex)
{
System.out.println(ex.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
}
}
}
}
}
public static void main(String s[])
{
new Server();
}
}

#8


改了登录的 ,注册的你可以参考下登录的server.java 的改法

Client.java
package Client;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Client
{
JFrame d = new JFrame();
JPanel panel=new JPanel();
JTextField tfRegName=new JTextField(10);
JPasswordField pfRegPwd=new JPasswordField(10);
JLabel lRegName=new JLabel("昵称:");
JLabel lRegPwd=new JLabel("密码:");
JButton bEnter=new JButton("登陆");
JButton bRegister=new JButton("注册");

JFrame fMain=new JFrame("JAVA聊天室");
JPanel pMessage=new JPanel();
JPanel pUserList=new JPanel();
JPanel pTalk=new JPanel();
JPanel pMain=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel pTalkAndMsg=new JPanel();
JTextArea taMessage=new JTextArea(25,40);
JScrollPane spMessage;
JScrollPane spUserList;
JTextField tfTalk=new JTextField(30);
JLabel lb1=new JLabel("对");
JLabel lb2=new JLabel("说:");
JList lUserList=new JList();
JComboBox cbUserList=new JComboBox();
int v,h;
Socket sock;
DataOutputStream dosRemoteOut;
String sLocalName,sPassword,sMessage,sTag,sRemoteName;

public Client()
{
d.getContentPane().add(panel);
panel.setLayout(new GridLayout(3,3));
panel.add(lRegName);
panel.add(tfRegName);
panel.add(lRegPwd);
panel.add(pfRegPwd);
panel.add(bEnter);
panel.add(bRegister);
d.setTitle("登陆");
d.setSize(300,200);
d.setVisible(true);
bEnter.addActionListener(new Enter());
bRegister.addActionListener(new Register());
}
void Connect()
{
try
{
sock=new Socket("127.0.0.1",5001);
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
sMessage=sTag+":"+sLocalName+":"+sPassword;
System.out.println(sMessage);
dosRemoteOut.writeUTF(sMessage);
new Monitor(this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接服务器失败");
}
}
class MouseEvt extends MouseAdapter
{
public void mouseCliecked(MouseEvent me)
{
String sName;
boolean bTag=false;
sName=lUserList.getSelectedValue().toString();

for(int i=0;i<=cbUserList.getItemCount();i++)
{
if(cbUserList.getItemAt(i).toString().equals(sName))
{
bTag=true;
break;
}
}
if(!bTag)
{
cbUserList.addItem(sName);
}
}
}
void InitMain()
{
fMain.getContentPane().add(pMain);
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
taMessage.setEditable(false);
spMessage=new JScrollPane(taMessage,v,h);
spUserList=new JScrollPane(lUserList,v,h);
lUserList.setVisibleRowCount(70);
MouseEvt evt=new MouseEvt();
lUserList.addMouseListener(evt);
pMessage.setLayout(new BorderLayout());
pMessage.add(spMessage,BorderLayout.CENTER);
pUserList.setLayout(new BorderLayout());
pUserList.add(spUserList,BorderLayout.CENTER);

p1.setLayout(new BorderLayout());
p1.add(lb1,BorderLayout.WEST);

cbUserList.addItem("所有人");
p1.add(cbUserList,BorderLayout.CENTER);
p1.add(lb2,BorderLayout.EAST);
p2.setLayout(new BorderLayout());
p2.add(tfTalk,BorderLayout.CENTER);
pTalk.setLayout(new BorderLayout());
pTalk.add(p1,BorderLayout.WEST);
pTalk.add(p2,BorderLayout.CENTER);

pTalkAndMsg.setLayout(new BorderLayout());
pTalkAndMsg.add(pMessage,BorderLayout.CENTER);
pTalkAndMsg.add(pTalk,BorderLayout.SOUTH);

pMain.setLayout(new BorderLayout());
pMain.add(pTalkAndMsg,BorderLayout.CENTER);
pMain.add(pUserList,BorderLayout.WEST);

tfTalk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
sRemoteName=cbUserList.getSelectedItem().toString();
if(!sRemoteName.equals("所有人"))
{
sMessage="3:"+tfTalk.getText();
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"对所有人说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
else
{
sRemoteName=cbUserList.getSelectedItem().toString();
sMessage="4:"+sLocalName+":"+sRemoteName+":"+tfTalk.getText();;
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"悄悄的对"+sRemoteName+"说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
}
catch(IOException x)
{
System.out.println(x.getMessage()+":连接丢失");
}
}
});

fMain.setSize(800,600);
fMain.setVisible(true);
}
class Monitor extends Thread
{
private Client client;

public Monitor(Client client)
{
this.client=client;
}

public void run()
{
DataInputStream disRemoteIn;
System.out.println("ok");

try
{
disRemoteIn=new DataInputStream(client.sock.getInputStream());

while(true)
{

sMessage=disRemoteIn.readUTF();
System.out.println(sMessage);
StringTokenizer stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
char c=sTag.charAt(0);
switch(c)
{
case '1':
{
System.out.println("first");
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
System.out.println("second");
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"密码不正确或没有该用户!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '2':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"已经有一个相同的用户名!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '3':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"对所有人说: "+sMessage+"\n");
break;
}
case '4':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"悄悄的对你说: "+sMessage+"\n");
break;
}
case '5':
{
String[] sUserList=new String[50];
sUserList[1]=sLocalName;
for(int count=2;stkMessage.hasMoreTokens();++count)
{
sUserList[count]=stkMessage.nextToken();
}
lUserList.setListData(sUserList);
}
}
}
}
catch(IOException e)
{
System.out.println("call error");
}
}
}

class Enter implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="1";
d.setVisible(true);
Connect();
}
}

class Register implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="2";
d.setVisible(false);
Connect();
}
}
public static void main(String s[])
{
new Client();
}
}

#9


强,果然高手。。。。友情Up。。。

#1


客户端:
package Client;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Client
{
JDialog d=new JDialog();
JPanel panel=new JPanel();
JTextField tfRegName=new JTextField(10);
JPasswordField pfRegPwd=new JPasswordField(10);
JLabel lRegName=new JLabel("昵称:");
JLabel lRegPwd=new JLabel("密码:");
JButton bEnter=new JButton("登陆");
JButton bRegister=new JButton("注册");

JFrame fMain=new JFrame("JAVA聊天室");
JPanel pMessage=new JPanel();
JPanel pUserList=new JPanel();
JPanel pTalk=new JPanel();
JPanel pMain=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel pTalkAndMsg=new JPanel();
JTextArea taMessage=new JTextArea(25,40);
JScrollPane spMessage;
JScrollPane spUserList;
JTextField tfTalk=new JTextField(30);
JLabel lb1=new JLabel("对");
JLabel lb2=new JLabel("说:");
JList lUserList=new JList();
JComboBox cbUserList=new JComboBox();
int v,h;
Socket sock;
DataOutputStream dosRemoteOut;
String sLocalName,sPassword,sMessage,sTag,sRemoteName;

public Client()
{
d.getContentPane().add(panel);
panel.setLayout(new GridLayout(3,3));
panel.add(lRegName);
panel.add(tfRegName);
panel.add(lRegPwd);
panel.add(pfRegPwd);
panel.add(bEnter);
panel.add(bRegister);
d.setTitle("登陆");
d.setSize(300,200);
d.setVisible(true);
bEnter.addActionListener(new Enter());
bRegister.addActionListener(new Register());
}
void Connect()
{
try
{
sock=new Socket("127.0.0.1",5001);
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
sMessage=sTag+":"+sLocalName+":"+sPassword;
System.out.println(sMessage);
dosRemoteOut.writeUTF(sMessage);
new Monitor(this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接服务器失败");
}
}
class MouseEvt extends MouseAdapter
{
public void mouseCliecked(MouseEvent me)
{
String sName;
boolean bTag=false;
sName=lUserList.getSelectedValue().toString();

for(int i=0;i<=cbUserList.getItemCount();i++)
{
if(cbUserList.getItemAt(i).toString().equals(sName))
{
bTag=true;
break;
}
}
if(!bTag)
{
cbUserList.addItem(sName);
}
}
}
void InitMain()
{
fMain.getContentPane().add(pMain);
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
taMessage.setEditable(false);
spMessage=new JScrollPane(taMessage,v,h);
spUserList=new JScrollPane(lUserList,v,h);
lUserList.setVisibleRowCount(70);
MouseEvt evt=new MouseEvt();
lUserList.addMouseListener(evt);
pMessage.setLayout(new BorderLayout());
pMessage.add(spMessage,BorderLayout.CENTER);
pUserList.setLayout(new BorderLayout());
pUserList.add(spUserList,BorderLayout.CENTER);

p1.setLayout(new BorderLayout());
p1.add(lb1,BorderLayout.WEST);

cbUserList.addItem("所有人");
p1.add(cbUserList,BorderLayout.CENTER);
p1.add(lb2,BorderLayout.EAST);
p2.setLayout(new BorderLayout());
p2.add(tfTalk,BorderLayout.CENTER);
pTalk.setLayout(new BorderLayout());
pTalk.add(p1,BorderLayout.WEST);
pTalk.add(p2,BorderLayout.CENTER);

pTalkAndMsg.setLayout(new BorderLayout());
pTalkAndMsg.add(pMessage,BorderLayout.CENTER);
pTalkAndMsg.add(pTalk,BorderLayout.SOUTH);

pMain.setLayout(new BorderLayout());
pMain.add(pTalkAndMsg,BorderLayout.CENTER);
pMain.add(pUserList,BorderLayout.WEST);
/*发送至服务器消息格式:
1:代表登陆信息,格式:"1:用户名:密码";
2:代表注册信息,格式:"2:用户名:密码";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:收消息用户名:消息";
从读取服务器消息格式:
1:代表登陆信息,格式:"1:成功"或"1:失败";
2:代表注册信息,格式:"2:成功"或"2:失败";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:消息";
5:代表用户列表信息,格式:"5:用户名1,用户名2,用户名3......,用户名n";*/
tfTalk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
sRemoteName=cbUserList.getSelectedItem().toString();
if(!sRemoteName.equals("所有人"))
{
sMessage="3:"+tfTalk.getText();
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"对所有人说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
else
{
sRemoteName=cbUserList.getSelectedItem().toString();
sMessage="4:"+sLocalName+":"+sRemoteName+":"+tfTalk.getText();;
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"悄悄的对"+sRemoteName+"说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
}
catch(IOException x)
{
System.out.println(x.getMessage()+":连接丢失");
}
}
});

fMain.setSize(800,600);
fMain.setVisible(true);
}
class Monitor extends Thread
{
private Client client;

public Monitor(Client client)
{
this.client=client;
}

public void run()
{
DataInputStream disRemoteIn;

try
{
disRemoteIn=new DataInputStream(client.sock.getInputStream());
while(true)
{
sMessage=disRemoteIn.readUTF();
StringTokenizer stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
char c=sTag.charAt(1);
switch(c)
{
case '1':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"密码不正确或没有该用户!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '2':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"已经有一个相同的用户名!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '3':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"对所有人说: "+sMessage+"\n");
break;
}
case '4':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"悄悄的对你说: "+sMessage+"\n");
break;
}
case '5':
{
String[] sUserList=new String[50];
sUserList[1]=sLocalName;
for(int count=2;stkMessage.hasMoreTokens();++count)
{
sUserList[count]=stkMessage.nextToken();
}
lUserList.setListData(sUserList);
}
}
}
}
catch(IOException e)
{

}
}
}

class Enter implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="1";
d.setVisible(false);
Connect();
}
}

class Register implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="2";
d.setVisible(false);
Connect();
}
}
public static void main(String s[])
{
new Client();
}
}

#2


急用,请各位高手帮帮忙,帮帮小弟。谢谢

#3


(1) JDialog d=new JDialog();换用JFrame
(2) frame.show()

#4


我测了一下,发现好象是客户端不能正确读取服务器反回的登陆或注册信息。老是停在线程RUN方法的sMessage=disRemoteIn.readUTF();地方.

#5


看楼上的饿!!!

#6


服务器端执行不到run方法

#7


server.java
package Server;
import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
private boolean bListening=true;
private Vector vClients=new Vector(50,1);
private String responseLine;

class Client
{
DataOutputStream dosRemoteOut;
String sName="";
}

Server()
{
ServerSocket serverSock;
try
{
serverSock=new ServerSocket(5001);
System.out.println("服务器已启动..............");
}
catch(IOException e)
{
System.out.println(e.getMessage()+":服务器创建失败");
return;
}
while(bListening)
{
try
{
Socket socket=serverSock.accept();
System.out.println(socket.getInetAddress().getHostAddress()+"已连接......");
DataInputStream disRemoteIn=new DataInputStream(socket.getInputStream());


//Client client=new Client();
//client.dosRemoteOut=dosRemoteOut;
//vClients.addElement(client);
new ServerMonitor(socket,disRemoteIn,this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+"连接失败");
}
}
}

synchronized Vector GetClients()
{
return vClients;
}

synchronized void AddClient(Client client)
{
vClients.addElement(client);
}

synchronized void RemoveClient(Client client)
{
vClients.removeElement(client);
}

class ServerMonitor extends Thread
{
/*从读取客户端消息格式:
1:代表登陆信息,格式:"1:用户名:密码";
2:代表注册信息,格式:"2:用户名:密码";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:收消息用户名:消息";
发送至客户端消息格式:
1:代表登陆信息,格式:"1:成功"或"1:失败";
2:代表注册信息,格式:"2:成功"或"2:失败";
3:代表公聊信息,格式:"3:用户名:消息";
4:代表私聊信息,格式:"4:发消息用户名:消息";
5:代表用户列表信息,格式:"5:用户名1,用户名2,用户名3......,用户名n";*/
private Socket sock;
private DataInputStream disRemoteIn;
private Server server;
private boolean listening=true;
private DataOutputStream dosRemoteOut;
private String sMessage,sFromName,sPassword,sToName,sTag;
private StringTokenizer stkMessage;
private Client client=new Client();
ServerMonitor(Socket sock,DataInputStream disRemoteIn,Server server)
{

try
{
//String str=disRemoteIn.readUTF();
//System.out.println("Client accept: "+str);

this.sock=sock;
this.disRemoteIn=disRemoteIn;
this.server=server;
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
//sMessage = disRemoteIn.readUTF();
//System.out.println("Client accept: "+sMessage);

/*
BufferedReader d = new BufferedReader(new InputStreamReader(sock.getInputStream()));
responseLine = d.readLine();
System.out.println(responseLine);
sMessage=d.readLine();

while ((responseLine = d.readLine()) != null){
// 把每一行显示出来
System.out.println(responseLine);
if ( responseLine.indexOf("") != -1 )
break;
}*/
//stkMessage=new StringTokenizer(sMessage,":");
//System.out.println("Client accept: "+stkMessage);

}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接失败");
}
}

synchronized public void run()
{

try
{

while(listening)
{
//System.out.println("receive is ok");
sMessage=disRemoteIn.readUTF();
//System.out.println(sMessage);
if(sMessage==null)
{
System.out.println("Message NULL");
}
stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
//System.out.println(sTag);
char c=sTag.charAt(0);

switch(c)
{
case '1':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("xy")&&sPassword.equals("xy"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="1:成功";
System.out.println(sMessage);
SendMessage(sMessage,"");
}
else
{
sMessage="1:失败";
//SendMessage(sMessage,"");
dosRemoteOut.writeUTF(sMessage);
listening=false;
}
break;
}
case '2':
{
sFromName=stkMessage.nextToken();
sPassword=stkMessage.nextToken();
System.out.println(sFromName+" "+sPassword);
if(sFromName.equals("zg")&&sPassword.equals("zg"))
{
client.dosRemoteOut=dosRemoteOut;
client.sName=sFromName;
server.AddClient(client);
sMessage="2:成功";
SendMessage(sMessage,"");
}
else
{
sMessage="2:失败";
SendMessage(sMessage,"");
listening=false;
}
break;
}
case '3':
{
sFromName=stkMessage.nextToken();
sMessage="3:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"");
break;
}
case '4':
{
sFromName=stkMessage.nextToken();
sToName=stkMessage.nextToken();
sMessage="4:"+sFromName+":"+stkMessage.nextToken();
SendMessage(sMessage,"4");
break;
}
case '5':
{
sMessage="5:";
Vector vClients=server.GetClients();
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
sMessage=sMessage+":"+client.sName;
}
SendMessage(sMessage,"");
break;
}
}
}
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接丢失");
}
finally
{
try
{
if(dosRemoteOut!=null)
{
server.RemoveClient(client);
dosRemoteOut.close();
dosRemoteOut=null;
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
disRemoteIn=null;
}
if(sock!=null)
{
sock.close();
sock=null;
}
}
catch(IOException ioe)
{

}
}
}
public void SendMessage(String sMessage,String sTag)
{
 Vector vClients=server.GetClients();
 
 Client client;
 String sName;
 DataOutputStream dosDataOut;
 System.out.println("Begin to sent message now");
 if(!sTag.equals("4"))
 {
  for(Enumeration e=vClients.elements();e.hasMoreElements();)
  {
  System.out.println("message is sent");
  client=(Client)(e.nextElement());
  dosDataOut=client.dosRemoteOut;
 
  if(dosDataOut.equals(dosRemoteOut))
  {
try
{
System.out.println("sent over");
dosDataOut.writeUTF(sMessage);
}
catch(IOException x)
{
System.out.println(x.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
  }
 
 }
}
else
{
for(Enumeration e=vClients.elements();e.hasMoreElements();)
{
client=(Client)(e.nextElement());
dosDataOut=client.dosRemoteOut;
sName=client.sName;
if(sName.equals(sToName))
{
try
{
dosDataOut.writeUTF(sMessage);
}
catch(IOException ex)
{
System.out.println(ex.getMessage()+":发送消息失败");
server.RemoveClient(client);
}
}
}
}
}
}
public static void main(String s[])
{
new Server();
}
}

#8


改了登录的 ,注册的你可以参考下登录的server.java 的改法

Client.java
package Client;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Client
{
JFrame d = new JFrame();
JPanel panel=new JPanel();
JTextField tfRegName=new JTextField(10);
JPasswordField pfRegPwd=new JPasswordField(10);
JLabel lRegName=new JLabel("昵称:");
JLabel lRegPwd=new JLabel("密码:");
JButton bEnter=new JButton("登陆");
JButton bRegister=new JButton("注册");

JFrame fMain=new JFrame("JAVA聊天室");
JPanel pMessage=new JPanel();
JPanel pUserList=new JPanel();
JPanel pTalk=new JPanel();
JPanel pMain=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel pTalkAndMsg=new JPanel();
JTextArea taMessage=new JTextArea(25,40);
JScrollPane spMessage;
JScrollPane spUserList;
JTextField tfTalk=new JTextField(30);
JLabel lb1=new JLabel("对");
JLabel lb2=new JLabel("说:");
JList lUserList=new JList();
JComboBox cbUserList=new JComboBox();
int v,h;
Socket sock;
DataOutputStream dosRemoteOut;
String sLocalName,sPassword,sMessage,sTag,sRemoteName;

public Client()
{
d.getContentPane().add(panel);
panel.setLayout(new GridLayout(3,3));
panel.add(lRegName);
panel.add(tfRegName);
panel.add(lRegPwd);
panel.add(pfRegPwd);
panel.add(bEnter);
panel.add(bRegister);
d.setTitle("登陆");
d.setSize(300,200);
d.setVisible(true);
bEnter.addActionListener(new Enter());
bRegister.addActionListener(new Register());
}
void Connect()
{
try
{
sock=new Socket("127.0.0.1",5001);
dosRemoteOut=new DataOutputStream(sock.getOutputStream());
sMessage=sTag+":"+sLocalName+":"+sPassword;
System.out.println(sMessage);
dosRemoteOut.writeUTF(sMessage);
new Monitor(this).start();
}
catch(IOException e)
{
System.out.println(e.getMessage()+":连接服务器失败");
}
}
class MouseEvt extends MouseAdapter
{
public void mouseCliecked(MouseEvent me)
{
String sName;
boolean bTag=false;
sName=lUserList.getSelectedValue().toString();

for(int i=0;i<=cbUserList.getItemCount();i++)
{
if(cbUserList.getItemAt(i).toString().equals(sName))
{
bTag=true;
break;
}
}
if(!bTag)
{
cbUserList.addItem(sName);
}
}
}
void InitMain()
{
fMain.getContentPane().add(pMain);
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
taMessage.setEditable(false);
spMessage=new JScrollPane(taMessage,v,h);
spUserList=new JScrollPane(lUserList,v,h);
lUserList.setVisibleRowCount(70);
MouseEvt evt=new MouseEvt();
lUserList.addMouseListener(evt);
pMessage.setLayout(new BorderLayout());
pMessage.add(spMessage,BorderLayout.CENTER);
pUserList.setLayout(new BorderLayout());
pUserList.add(spUserList,BorderLayout.CENTER);

p1.setLayout(new BorderLayout());
p1.add(lb1,BorderLayout.WEST);

cbUserList.addItem("所有人");
p1.add(cbUserList,BorderLayout.CENTER);
p1.add(lb2,BorderLayout.EAST);
p2.setLayout(new BorderLayout());
p2.add(tfTalk,BorderLayout.CENTER);
pTalk.setLayout(new BorderLayout());
pTalk.add(p1,BorderLayout.WEST);
pTalk.add(p2,BorderLayout.CENTER);

pTalkAndMsg.setLayout(new BorderLayout());
pTalkAndMsg.add(pMessage,BorderLayout.CENTER);
pTalkAndMsg.add(pTalk,BorderLayout.SOUTH);

pMain.setLayout(new BorderLayout());
pMain.add(pTalkAndMsg,BorderLayout.CENTER);
pMain.add(pUserList,BorderLayout.WEST);

tfTalk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
sRemoteName=cbUserList.getSelectedItem().toString();
if(!sRemoteName.equals("所有人"))
{
sMessage="3:"+tfTalk.getText();
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"对所有人说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
else
{
sRemoteName=cbUserList.getSelectedItem().toString();
sMessage="4:"+sLocalName+":"+sRemoteName+":"+tfTalk.getText();;
dosRemoteOut.writeUTF(sMessage);
taMessage.append(sLocalName+"悄悄的对"+sRemoteName+"说: "+tfTalk.getText()+"\n");
tfTalk.setText("");
}
}
catch(IOException x)
{
System.out.println(x.getMessage()+":连接丢失");
}
}
});

fMain.setSize(800,600);
fMain.setVisible(true);
}
class Monitor extends Thread
{
private Client client;

public Monitor(Client client)
{
this.client=client;
}

public void run()
{
DataInputStream disRemoteIn;
System.out.println("ok");

try
{
disRemoteIn=new DataInputStream(client.sock.getInputStream());

while(true)
{

sMessage=disRemoteIn.readUTF();
System.out.println(sMessage);
StringTokenizer stkMessage=new StringTokenizer(sMessage,":");
sTag=stkMessage.nextToken();
char c=sTag.charAt(0);
switch(c)
{
case '1':
{
System.out.println("first");
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
System.out.println("second");
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"密码不正确或没有该用户!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '2':
{
sMessage=stkMessage.nextToken();
if(sMessage.equals("成功"))
{
InitMain();
}
else
{
JOptionPane.showMessageDialog(null,"已经有一个相同的用户名!");
if(dosRemoteOut!=null)
{
dosRemoteOut.close();
}
if(disRemoteIn!=null)
{
disRemoteIn.close();
}
if(sock!=null)
{
sock.close();
}
System.exit(1);
}
break;
}
case '3':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"对所有人说: "+sMessage+"\n");
break;
}
case '4':
{
sRemoteName=stkMessage.nextToken();
sMessage=stkMessage.nextToken();
taMessage.append(sRemoteName+"悄悄的对你说: "+sMessage+"\n");
break;
}
case '5':
{
String[] sUserList=new String[50];
sUserList[1]=sLocalName;
for(int count=2;stkMessage.hasMoreTokens();++count)
{
sUserList[count]=stkMessage.nextToken();
}
lUserList.setListData(sUserList);
}
}
}
}
catch(IOException e)
{
System.out.println("call error");
}
}
}

class Enter implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="1";
d.setVisible(true);
Connect();
}
}

class Register implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
sLocalName=tfRegName.getText();
sPassword=pfRegPwd.getText();
sTag="2";
d.setVisible(false);
Connect();
}
}
public static void main(String s[])
{
new Client();
}
}

#9


强,果然高手。。。。友情Up。。。