📄 serverthread.java
字号:
/**
* Email: taorundong@126.com
*
* @author taorundong
* @version 1.00 07/02/04
*/
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class ServerThread extends Thread{
private //synchronized
People you = null;
DataInputStream input = null;
DataOutputStream output = null;
Hashtable clientHashList = null;
Socket clientSocket = null;
String name = null;
JTextArea messageContent = null;
JComboBox box = null;
ServerThread (Socket client,Hashtable peopleList,JTextArea text,JComboBox comboBox){
try{
input = new DataInputStream(client.getInputStream());
output = new DataOutputStream(client.getOutputStream());
}
catch(Exception e){
e.printStackTrace();
}
box = comboBox;
messageContent = text;
clientHashList = peopleList;
clientSocket = client;
}
public void run(){
initChating();
chating();
}
public String getPeopleName(){
return name;
}
public void setPeopleName(String temp){
you.updataName(temp);
}
//the people who send the message needn't add the enter key
public void initChating(){
try{
//receive the name of the client
name = input.readUTF();//the beginning
you = new People(name);
you.setOutputStream(output);
you.setInputStream(input);
you.setClientSocket(clientSocket);
//to tell all of the clients somebody on
outPutNameToEveryClient(name+" 上线了");
clientHashList.put(name,you);
box.addItem(name);
messageContent.append(name+" 上线了"+"\n");
output.writeUTF("Welcome to the chating room"+" "+name);
}
catch(Exception e){
System.out.println("init error!");
e.printStackTrace();
}
}
public void chating(){
String message = null;
while(!clientSocket.isClosed()){
try{
message = input.readUTF();
messageContent.append(messageIntegrate(message,name)+"\n");
outPutToEveryClient(message);
}
catch(Exception e){
// System.out.print(you.getName()+"is out"+"\n");
//Though the JVM will close the socket for you,you should close it yourself
try{
clientSocket.close();
} //if any error,then close the socket immediately
catch(Exception ee){
ee.printStackTrace();
}
e.printStackTrace();
clientHashList.remove(name); //remove the people from the hashList
box.removeItem(name); //remove from the comboBox
messageContent.append(name+" is out"+"\n");
outPutToEveryClient(name+" is out");
// System.out.println(temp.elements().hasMoreElements());
//this.destroy(); //exit the thread
}
}
}
public void outPutToEveryClient(String message){
Enumeration Enum = clientHashList.elements();
while(Enum.hasMoreElements()){
//except self
People client = (People)Enum.nextElement();
if(client.getName()!=name){
try{
client.getOutputStream().writeUTF(messageIntegrate(message,you.getName()));
}
catch(Exception e){
e.printStackTrace();
}
}
else{
continue;
}
}
}
public void outPutNameToEveryClient(String name){
Enumeration Enum = clientHashList.elements();
while(Enum.hasMoreElements()){
People client = (People)Enum.nextElement();
try{
client.getOutputStream().writeUTF(name);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public String messageIntegrate(String message,String clientName){
return message+" from "+clientName+" "+
clientSocket.getInetAddress().getHostAddress();
}
public String getClientSocketInfo(String temp){
return clientSocket.getInetAddress().getHostName()+"\n"+clientSocket.getInetAddress().getHostAddress();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -