标签:
项目结构
项目设计
客户端同时大量请求服务端,服务端多线程处理连接,并发序列化获得客户端发送的数据,并做出处理。
IClients
package simple.socket;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;
public class IClients {
/**
* IServer Class
* </br> Start 1000 client connect the server
*/
public static void main(String[] args) {
// start 20 clients to connect the Server
for (int j = 0; j < 1000; j++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 90);
ObjectOutputStream outputStream = new ObjectOutputStream(
socket.getOutputStream());
Message message = new Message();
message.setUserId("" + new Random().nextLong());
message.setMessageDate(new Date().toString());
message.setUserName(Thread.currentThread().getName());
message.setMessage(Thread.currentThread().getName()
+ "xxxx");
outputStream.writeObject(message);
outputStream.close();
socket.close();
Thread.sleep(10);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
IServer
package simple.socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class IServer {
/**
* IServer Class
* Server main class
*/
ServerSocket serverSocket = null;
final static int SERVER_PORT = 90;
Socket connectSocket = null;
public IServer() {
try {
System.out.println("服务器启动了。。。");
serverSocket = new ServerSocket(SERVER_PORT);
} catch (IOException e) {
e.printStackTrace();
}
}
public void startServer() {
try {
while (!serverSocket.isClosed()) {
connectSocket = serverSocket.accept();
if (connectSocket != null) {
IServerHandel handle = new IServerHandel(connectSocket);
new Thread(handle).start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void shutdownServer() {
if (!serverSocket.isClosed()) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new IServer().startServer();
}
}
IServerHandel
package simple.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
public class IServerHandel implements Runnable {
/**
* IServerHandel Class
* </br>handle the Muliti-clients Socket connections
*/
Socket connectSocket = null;
ObjectInputStream inputStream = null;
Message message = null;
public IServerHandel(Socket connectSocket) {
this.connectSocket = connectSocket;
}
public void readMsg() {
try {
inputStream = new ObjectInputStream(connectSocket.getInputStream());
if ((message = (Message) inputStream.readObject()) != null) {
System.out.println(message.getMessage());
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void run() {
readMsg();
}
}
Message
package simple.socket;
import java.io.Serializable;
public class Message implements Serializable {
/**
* message Object
*/
private static final long serialVersionUID = -8256700753720022810L;
String userId = "";
String userName = "";
String message = "";
String messageDate = "";
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessageDate() {
return messageDate;
}
public void setMessageDate(String messageDate) {
this.messageDate = messageDate;
}
}
标签:
原文地址:http://www.cnblogs.com/rootuser/p/4415132.html