📄 chatserver.java
字号:
package com.chatroom;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class ChatServer {
public static void main(String[] args) {
ServerSocket server =null;
Socket socket =null;
Hashtable chatList;
chatList =new Hashtable();
while(true){
try {
server =new ServerSocket(6000);
} catch (IOException e1) {
// TODO: handle exception
System.out.println("Listening......");
}
try {
socket =server.accept();
InetAddress address =socket.getInetAddress();
System.out.println("用户的IP:"+address);
} catch (IOException e) {
// TODO: handle exception
}
if(socket!=null){
ServerThread chatThread =new ServerThread(socket,chatList);
chatThread.start();
}
else{
continue;
}
}
}
}
//多线程类
class ServerThread extends Thread{
String name=null,sex=null;
Socket socket =null;
DataOutputStream out=null;
DataInputStream in=null;
Hashtable chatList =null;
ServerThread(Socket t,Hashtable list) {
// TODO Auto-generated constructor stub
chatList =list;
socket =t;
try {
in =new DataInputStream(socket.getInputStream());
out =new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO: handle exception
}
}
public void run(){
while(true){
String s=null;
try {
//接收客户信息
s=in.readUTF();
System.out.println(s);
//判断信息的内容,如果为客户的昵称和性别,将该信息存放在chatList表中,枚举出chatList表中所有的聊天者,把接收到的信息发送给他们。
if (s.startsWith("姓名:")) {
name=s.substring(s.indexOf(":")+1,s.indexOf("性别"));
sex=s.substring(s.lastIndexOf(":")+1);
boolean boo =chatList.containsKey(name);
if(boo ==false){
chatList.put(name, this);
Enumeration enum1=chatList.elements();
while(enum1.hasMoreElements()){
ServerThread th=(ServerThread)enum1.nextElement();
th.out.writeUTF("聊天者:"+name+"性别"+sex);
if(th!=this){
out.writeUTF("聊天者:"+th.name+"性别"+th.sex);
}
}
}
}
//判断信息的内容,如果为客户发送的聊天信息,枚举出chatList表中所有的聊天者,把接收到的信息发送给他们。
else if(s.startsWith("聊天:")){
String message =s.substring(s.indexOf(":")+1);
Enumeration enum1=chatList.elements();
while(enum1.hasMoreElements()){
((ServerThread)enum1.nextElement()).out.writeUTF("聊天内容:"+message);
}
}
//判断信息的内容,如果为客户退出聊天室信息,枚举出chatList表中所有的聊天者,把接收到的信息发送给他们。
else if(s.startsWith("用户离线:")){
Enumeration enum1=chatList.elements();
while(enum1.hasMoreElements()){
try {
ServerThread th =(ServerThread)enum1.nextElement();
if(th!=this&&th.isAlive()){
th.out.writeUTF("用户离线:"+name);
}
} catch (IOException eee) {
// TODO: handle exception
}
}
chatList.remove(name);
socket.close();
System.out.println(name+"用户离线了");
break;
}
} catch (IOException ee) {
// TODO: handle exception
Enumeration enum1=chatList.elements();
while(enum1.hasMoreElements()){
try {
ServerThread th=(ServerThread)enum1.nextElement();
if(th!=this&&th.isAlive()){
th.out.writeUTF("用户离线:"+name);
}
} catch (IOException eee) {
// TODO: handle exception
}
}
chatList.remove(name);
try {
socket.close();
} catch (IOException eee) {
// TODO: handle exception
}
System.out.println(name+"用户离线了");
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -