?? socketserver.java
字號:
package chat.socket;
import chat.*;
import java.io.*;
import java.net.*;
public class SocketServer extends BaseServer
{
public final static int PORT_NUMBER = 2222; // Our chat application port
// Invoke type of method to be called
public final static int INVOKE_GET_HISTORY = 0;
public final static int INVOKE_BROADCAST = 1;
public final static int INVOKE_GET_USER = 2;
public final static int INVOKE_DISCONNECT = 3;
// Status of logon
public final static int LOGON_SUCCESSFUL = 0;
public final static int LOGON_FAILED = -1;
// Type of data to be sent
public final static int SENT_MSG = 0;
public final static int SENT_USER = 1;
public final static int REMOVE_USER = 2;
public final static int CLEAR_USER = 3;
public static void main(String[] args) throws IOException
{
SocketServer ss = new SocketServer();
}
public SocketServer() throws IOException
{
Socket client;
ServerSocket ss = new ServerSocket(PORT_NUMBER);
while (true)
{
client = ss.accept();
// Separate thread for different user
SocketSkeleton skel = new SocketSkeleton(client);
addClient(skel);
skel.start();
}
}
// SocketSkeletion thread
class SocketSkeleton extends Thread implements ChatClient
{
protected Socket receiveSocket;
protected Socket sendSocket;
protected DataInputStream inInput;
protected DataOutputStream inOutput;
protected DataOutputStream outOutput;
public SocketSkeleton(Socket client)
{
receiveSocket = client;
}
public void run()
{
int methodID;
try
{
String userid = initialize();
broadcastUser(userid);
while(true)
{
methodID = inInput.readInt();
switch(methodID)
{
case INVOKE_BROADCAST: handleBroadcast();
break;
case INVOKE_GET_HISTORY: handleGetHistory();
break;
case INVOKE_GET_USER: handleGetUser();
break;
case INVOKE_DISCONNECT: handleDisconnectUser();
break;
}
/*if (methodID<0)
{
handleDisconnectUser();
}*/
}
}catch (Exception ioe) {}
}
protected void handleBroadcast() throws IOException
{
String message = inInput.readUTF();
broadcastMessage(message);
addMessageToLog(message);
}
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 handleGetUser() throws IOException
{
String user;
int count = userServerList.size();
inOutput.writeInt(count);
for (int i=0; i < count; i++)
{
user = (String) (userServerList.elementAt(i));
inOutput.writeUTF(user);
}
}
protected void handleDisconnectUser() throws IOException
{
// Method to remove user from list
// kill this thread
String user = inInput.readUTF();
removeClient(this);
broadcastRemovedUser(user);
System.out.println("Exit : " + user + " has left the server!");
//destroy();
}
protected String initialize() throws IOException, LogonFailedException
{
InputStream is;
OutputStream os;
InetAddress addr;
is = receiveSocket.getInputStream();
inInput = new DataInputStream(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 DataOutputStream(os);
System.out.println("(" + addr.toString() + ") : " + userid + " has joined the server!");
}
else
{
inOutput.writeInt(LOGON_FAILED);
throw new LogonFailedException("Invalid userid / password");
}
return userid;
}
public void displayMessage(ChatMessage message) throws IOException
{
java.util.Date msgDate = message.messageDate;
long timeValue = msgDate.getTime();
String msgText = message.messageText;
outOutput.writeInt(SENT_MSG);
outOutput.writeLong(timeValue);
outOutput.writeUTF(msgText);
}
// Overwrite methods
public void addUser(String id) throws Exception
{
outOutput.writeInt(SENT_USER);
outOutput.writeUTF(id);
}
public void removeUser(String id) throws Exception
{
outOutput.writeInt(REMOVE_USER);
outOutput.writeUTF(id);
}
public void clearUser() throws Exception
{
outOutput.writeInt(CLEAR_USER);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -