📄 socketserver.java
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
public class SocketServer extends BaseServer implements Runnable{
public final static int PORT_NUMBER=1234;
public final static int INVOKE_GET_HISTORY=0;
public final static int INVOKE_BROADCAST=1;
public final static int INVOKE_ELEMENT=2;
public final static int LOGON_SUCCESSFUL=0;
public final static int LOGON_FAILED=-1;
public int clientCount=0;
ServerSocket ss;
private ServerControl ctrl;
public SocketServer (ServerControl ctrl) throws IOException{
this.ctrl=ctrl;
ss=new ServerSocket(PORT_NUMBER);
}
public void run(){
Socket client;
while (true){
try{
client =ss.accept();
Socketskel skel=new Socketskel(client);
clientCount++;
ctrl.setCount("当前连接数: "+Integer.toString(clientCount));
addClient(skel);
skel.start();
}catch(Exception e){}
}
}
class Socketskel extends Thread implements ChatClient{
protected Socket receiveSocket;
protected Socket sendSocket;
protected DataInputStream inInput;
protected ObjectInputStream inInputObject;
protected DataOutputStream inOutput;
protected ObjectOutputStream outOutput;
public Socketskel(Socket client){
receiveSocket=client;
}
public void run(){
int methodID;
try{
initialize();
while(true){
methodID=inInput.readInt();
switch(methodID) {
case INVOKE_BROADCAST :
handleBroadcast();
break;
case INVOKE_GET_HISTORY :
handleGetHistory();
break;
case INVOKE_ELEMENT :
handleElement();
break;
}
}
}catch(IOException e){}
catch(LogonfailedException e){}
}
protected void handleBroadcast()throws IOException {
String message=inInput.readUTF();
broadcastMessage(message);
}
protected void handleElement() throws IOException {
try{
Element element=(Element)inInputObject.readObject();
broadcastElement(element);
}catch(ClassNotFoundException e){}
}
protected void handleGetHistory() throws IOException {
ChatMessage message;
java.util.Date msgDate;
String msgText;
int count=messageHistory.size();
inOutput.writeInt(count);
for(int i=0;i<count;i++){
message=(ChatMessage)(messageHistory.elementAt(i));
msgDate=message.messageDate;
msgText=message.messageText;
inOutput.writeLong(msgDate.getTime());
inOutput.writeUTF(msgText);
}
}
protected void initialize() throws IOException,LogonfailedException {
InputStream is;
OutputStream os;
InetAddress addr;
is= receiveSocket.getInputStream();
inInput=new DataInputStream(is);
inInputObject=new ObjectInputStream(is);
os= receiveSocket.getOutputStream();
inOutput=new DataOutputStream(os);
String userid=inInput.readUTF();
String password=inInput.readUTF();
int sendPort=inInput.readInt();
addr=receiveSocket.getInetAddress();
if(isValidUserInfo(userid,password)){
inOutput.writeInt(LOGON_SUCCESSFUL);
sendSocket=new Socket(addr,sendPort);
os=sendSocket.getOutputStream();
outOutput=new ObjectOutputStream(os);
}
else {
inOutput.writeInt(LOGON_FAILED);
throw new LogonfailedException("Fail");
}
}
public void drawElement(Element element) throws IOException{
outOutput.writeObject(element);
}
public void displayMessage(ChatMessage message) throws IOException{
outOutput.writeObject(message);
}
}
public int getClientCount (){
return clientCount;
}
public static void main(String args[]){
try{
SocketServer ss=new SocketServer(null);
}catch(IOException e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -