服务器端
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
public class ChatServer extends Thread {
public final static int PORT=8888; //服务器侦听的端口
ServerSocket ss;
java.awt.List user_list; //在服务器端显示用户的列表
Vector users; //存放当前在线的用户名
Vector connections; //存放当前在线的用户名
String username;
//当一个用户退出是,将起对应的信息系统再系统中删除
public synchronized void removeUser(String un,Connection con){
user_list.remove(un);
users.remove(un);
connections.remove(con);
}
public ChatServer(){
super("Server");
try{
ss=new ServerSocket(PORT);
}catch(IOException e){
e.printStackTrace();
}
//生成显示窗口
Frame f=new Frame("Sever Monitor");
user_list=new java.awt.List(30);
f.add("Center",user_list);
f.setSize(500,200);
f.addWindowListener(new closeWin());
f.show();
users=new Vector();
connections=new Vector();
start();
}
public void run(){
try{
while (true){
Socket s=ss.accept();
Connection c=new Connection(s,this);
//检查用户名是否与已有的相同
username=c.readData();
while(users.contains(username)){
c.writeData("error");
username=c.readData();
}
c.writeData("right");
c.username=username;
c.start();
//向其它用户发送信息
serverWriter("用户"+username+"加入",false,null);
//将新用户加入系统中
synchronized(users){
users.addElement(username);
connections.addElement(c);
user_list.add(username);
}
//再新用户的窗口内显示已有的用户的信息
Enumeration em=users.elements();
while(em.hasMoreElements()){
String str=(String)em.nextElement();
serverWriter("用户:"+str,true,username);
//}
//在客户端显示用户列表
//int i;
//String str1;
//String slist[];
//slist=user_list.getItems();
//for (i=0;i<30;i++ )
//{
//str1=slist[i];
//serverWriter(str1,true,username);
}
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[]args){
new ChatServer();
}
class closeWin extends WindowAdapter{
public void windowClosing(WindowEvent e){
Frame frm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
/*将信息发送给相应的用户,str为要发送的信息,w表示为私聊,此时在un中存放聊天的
对象的用户名。w假表示非私聊,此时un无意义。*/
public synchronized void serverWriter(String str,boolean w,String un){
Enumeration em=connections.elements();
if(w)
while(em.hasMoreElements()){
Connection c=(Connection)em.nextElement();
if(c.username.equals(un))
c.writeData(str+"\n");
}
else
while(em.hasMoreElements()){
Connection c=(Connection)em.nextElement();
c.writeData(str+"\n");
}
}
}
class Connection extends Thread{
ChatServer cs;
Socket s;
DataInputStream dis;
DataOutputStream dos;
public String username;
String talker=null;
boolean whisper;
public Connection(Socket s,ChatServer cs){
this.cs=cs;
this.s=s;
try{
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
}catch (IOException e){
e.printStackTrace();
}
}
public void run(){
String temp;
try{
while (true){
temp=readData(); //读数据
//显示数据
cs.serverWriter("["+username+"]"+temp,whisper,talker);
} }catch (IOException e){
}finally{
close();
}
}
public void close(){
try{
dis.close();
dos.close();
s.close();
}catch (IOException e){}
cs.removeUser(username,this);
cs.serverWriter("用户"+username+"退出",false,null);
}
public String readData() throws IOException {
String temp=dis.readUTF();
if(temp.charAt(0)==´#´){ //如果是私聊,则提取出谈话的对象
whisper=true;
int kk=temp.indexOf(´#´,1);
talker=temp.substring(1,kk);
return new String(temp.substring(kk=1));
}
whisper=false;
return new String(temp.substring(1));
}
public void writeData(String str){
try{
dos.writeUTF(str);
dos.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}
谢谢了,各位
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
public class ChatServer extends Thread {
public final static int PORT=8888; //服务器侦听的端口
ServerSocket ss;
java.awt.List user_list; //在服务器端显示用户的列表
Vector users; //存放当前在线的用户名
Vector connections; //存放当前在线的用户名
String username;
//当一个用户退出是,将起对应的信息系统再系统中删除
public synchronized void removeUser(String un,Connection con){
user_list.remove(un);
users.remove(un);
connections.remove(con);
}
public ChatServer(){
super("Server");
try{
ss=new ServerSocket(PORT);
}catch(IOException e){
e.printStackTrace();
}
//生成显示窗口
Frame f=new Frame("Sever Monitor");
user_list=new java.awt.List(30);
f.add("Center",user_list);
f.setSize(500,200);
f.addWindowListener(new closeWin());
f.show();
users=new Vector();
connections=new Vector();
start();
}
public void run(){
try{
while (true){
Socket s=ss.accept();
Connection c=new Connection(s,this);
//检查用户名是否与已有的相同
username=c.readData();
while(users.contains(username)){
c.writeData("error");
username=c.readData();
}
c.writeData("right");
c.username=username;
c.start();
//向其它用户发送信息
serverWriter("用户"+username+"加入",false,null);
//将新用户加入系统中
synchronized(users){
users.addElement(username);
connections.addElement(c);
user_list.add(username);
}
//再新用户的窗口内显示已有的用户的信息
Enumeration em=users.elements();
while(em.hasMoreElements()){
String str=(String)em.nextElement();
serverWriter("用户:"+str,true,username);
//}
//在客户端显示用户列表
//int i;
//String str1;
//String slist[];
//slist=user_list.getItems();
//for (i=0;i<30;i++ )
//{
//str1=slist[i];
//serverWriter(str1,true,username);
}
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[]args){
new ChatServer();
}
class closeWin extends WindowAdapter{
public void windowClosing(WindowEvent e){
Frame frm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
/*将信息发送给相应的用户,str为要发送的信息,w表示为私聊,此时在un中存放聊天的
对象的用户名。w假表示非私聊,此时un无意义。*/
public synchronized void serverWriter(String str,boolean w,String un){
Enumeration em=connections.elements();
if(w)
while(em.hasMoreElements()){
Connection c=(Connection)em.nextElement();
if(c.username.equals(un))
c.writeData(str+"\n");
}
else
while(em.hasMoreElements()){
Connection c=(Connection)em.nextElement();
c.writeData(str+"\n");
}
}
}
class Connection extends Thread{
ChatServer cs;
Socket s;
DataInputStream dis;
DataOutputStream dos;
public String username;
String talker=null;
boolean whisper;
public Connection(Socket s,ChatServer cs){
this.cs=cs;
this.s=s;
try{
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
}catch (IOException e){
e.printStackTrace();
}
}
public void run(){
String temp;
try{
while (true){
temp=readData(); //读数据
//显示数据
cs.serverWriter("["+username+"]"+temp,whisper,talker);
} }catch (IOException e){
}finally{
close();
}
}
public void close(){
try{
dis.close();
dos.close();
s.close();
}catch (IOException e){}
cs.removeUser(username,this);
cs.serverWriter("用户"+username+"退出",false,null);
}
public String readData() throws IOException {
String temp=dis.readUTF();
if(temp.charAt(0)==´#´){ //如果是私聊,则提取出谈话的对象
whisper=true;
int kk=temp.indexOf(´#´,1);
talker=temp.substring(1,kk);
return new String(temp.substring(kk=1));
}
whisper=false;
return new String(temp.substring(1));
}
public void writeData(String str){
try{
dos.writeUTF(str);
dos.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}
谢谢了,各位
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货