求聊天室聊天的服务器端的java源代码
2个回答
展开全部
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer{
public static void main(String[] args)throws Exception{
ServerSocket svSocket =null;
//Vector threads 为ServerThread集合
Vector threads = new Vector();
//开始监听
System.out.println ("listening...");
try {
//创建服务端套接口
svSocket = new ServerSocket(5555);
}catch (Exception ex) {
System.out.println ("Server create ServerSocket failed!");
return;
}
try{
int nid = 0;
//监听是否有客户端连接
while(true){
Socket socket = svSocket.accept();
System.out.println ("accept a client");
//创建一个新的ServerThread
ServerThread st = new ServerThread(socket,threads);
//为客户端分配一个ID号
st.setID(nid++);
threads.add(st);
new Thread(st).start();
//向所有人广播有新客户端连接
for(int i=0;i < threads.size();i++){
ServerThread temp = (ServerThread)threads.elementAt(i);
//st.write("<#>Welcome "+ temp.getID()+" to enter the chatroom");
}
System.out.println ("Listen again...");
}
}catch(Exception ex){
System.out.println ("server is down");
}
}
}
/**
* 监听线程,监听是否有客户端发消息过来
*/
class ServerThread implements Runnable{
private Vector threads;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private int nid;
//构造器
public ServerThread(Socket socket,Vector threads){
this.socket = socket;
this.threads = threads;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
}
}
//实现run方法
public void run(){
System.out.println ("Thread is running");
try{
//监听客户端是否发消息过来
while(true){
String receive = in.readUTF();
if(receive == null)
return;
//当某客户离开,给其它客户端发消息
if(receive.equals("leave")){
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("***"+getID()+"leaving...***");
}
}else{
//把某客户端发过来的发送到所有客户端
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("<"+getID()+">: "+receive);
}
}
}
}catch(Exception ex){
//从Vector中删除该线程,表示该线程已经离开聊天室
threads.removeElement(this);
//ex.printStackTrace();
}
try{
socket.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 服务端向客户端发送信息
*/
public void write(String msg){
synchronized(out){
try{
out.writeUTF(msg);
}catch(Exception ex){
}
}
}
/**
* 获得线程ID号
*/
public int getID(){
return this.nid;
}
/**
* 设置线程ID号
*/
public void setID(int nid){
this.nid = nid;
}
}
import java.io.*;
import java.util.*;
public class ChatServer{
public static void main(String[] args)throws Exception{
ServerSocket svSocket =null;
//Vector threads 为ServerThread集合
Vector threads = new Vector();
//开始监听
System.out.println ("listening...");
try {
//创建服务端套接口
svSocket = new ServerSocket(5555);
}catch (Exception ex) {
System.out.println ("Server create ServerSocket failed!");
return;
}
try{
int nid = 0;
//监听是否有客户端连接
while(true){
Socket socket = svSocket.accept();
System.out.println ("accept a client");
//创建一个新的ServerThread
ServerThread st = new ServerThread(socket,threads);
//为客户端分配一个ID号
st.setID(nid++);
threads.add(st);
new Thread(st).start();
//向所有人广播有新客户端连接
for(int i=0;i < threads.size();i++){
ServerThread temp = (ServerThread)threads.elementAt(i);
//st.write("<#>Welcome "+ temp.getID()+" to enter the chatroom");
}
System.out.println ("Listen again...");
}
}catch(Exception ex){
System.out.println ("server is down");
}
}
}
/**
* 监听线程,监听是否有客户端发消息过来
*/
class ServerThread implements Runnable{
private Vector threads;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private int nid;
//构造器
public ServerThread(Socket socket,Vector threads){
this.socket = socket;
this.threads = threads;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
}
}
//实现run方法
public void run(){
System.out.println ("Thread is running");
try{
//监听客户端是否发消息过来
while(true){
String receive = in.readUTF();
if(receive == null)
return;
//当某客户离开,给其它客户端发消息
if(receive.equals("leave")){
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("***"+getID()+"leaving...***");
}
}else{
//把某客户端发过来的发送到所有客户端
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("<"+getID()+">: "+receive);
}
}
}
}catch(Exception ex){
//从Vector中删除该线程,表示该线程已经离开聊天室
threads.removeElement(this);
//ex.printStackTrace();
}
try{
socket.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 服务端向客户端发送信息
*/
public void write(String msg){
synchronized(out){
try{
out.writeUTF(msg);
}catch(Exception ex){
}
}
}
/**
* 获得线程ID号
*/
public int getID(){
return this.nid;
}
/**
* 设置线程ID号
*/
public void setID(int nid){
this.nid = nid;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询