41 lines
971 B
Plaintext
41 lines
971 B
Plaintext
package com.surfbird.socket.server.service;
|
|
|
|
import java.io.IOException;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.util.Map;
|
|
|
|
import com.surfbird.util.ClientCollect;
|
|
|
|
public class SocketServer implements Runnable{
|
|
private ServerSocket server;
|
|
private Map<String, MultiSocket> mclient;
|
|
|
|
/**
|
|
* 启动SOCKET服务器
|
|
* @param serPort:服务器端口.
|
|
* @param interval:定时器间隔时间(分).
|
|
* @throws IOException
|
|
**/
|
|
public SocketServer(int serPort){
|
|
try {
|
|
this.server = new ServerSocket(serPort);
|
|
this.mclient = ClientCollect.instance().getClient();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void run() {
|
|
try {
|
|
while(true){
|
|
Socket socket = server.accept();
|
|
MultiSocket multi = new MultiSocket(socket, mclient);
|
|
new Thread(multi).start();
|
|
mclient.put(multi.getClient().getAddress() + ":" + multi.getClient().getPort(), multi);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |