Question:
1, not through the heartbeat packets to ensure that the connection is normal, because the client can't direct send the data to the server, otherwise the server will be open for each connection thread to monitor data,
2, in the chain of connection is high and frequent frequently when sending data chain table lookup, will cause the information delay is high, (oh, suddenly thought of by incremental id to identify each connection, find the position should be able to directly into the connection, when the impact speed should be bandwidth),
3, because the client is not directly related to the service side, so they can't quickly determine the connection has been disconnected, and reconnection and clean up the data,
4, forgive small white temporarily couldn't think of other problems, despite the many problems,
Tested in a number of connections (only connection and stored in the list for other tasks) reached 10000, server memory footprint in 30 + MB, if in solving the above 1, 2, 3 points, to use the Socket server for message delivery, his efficiency? Or will it work successfully?
The following is a test code
Public class SocketReciever {
Private ServerSocket listen=null;
Private ListConns=null;
Public static void main (String [] args) throws FileNotFoundException {
Int id=0;
SocketReciever reciever=new SocketReciever ();
ListConns=reciever. GetConns ();
FileOutputStream fos=new FileOutputStream (" the TXT ", true);
PrintStream ps=new PrintStream (fos);
Try {
While (true) {
The Socket Socket=reciever. GetServerSocket (). The accept ();
Conns. Add (new CusSocket (id++, socket));
OutputStream oStream=socket. GetOutputStream ();
OStream. Write (String. The valueOf (new Date (). The getTime ()). GetBytes (" utf-8 "));
OStream. Write (1);
}
} the catch (IOException e) {
E.p rintStackTrace (ps);
}
}
Public SocketReciever () {
Try {
Listen=new ServerSocket (23220);
} the catch (IOException e) {
e.printStackTrace();
}
Conns=new ArrayList(a);
}
Public ServerSocket getServerSocket () {
Return to listen;
}
Public ListGetConns () {
Return conns;
}
}
Public class CusSocket {
Public int flag;
Public Socket Socket;
Public CusSocket (int flag, Socket Socket) {
This. The socket=socket;
. This flag=flag;
}
}
Still have some questions: when we establish a TCP connection, the system will create a memory to store a Socket object, takes up bandwidth exchange data, it will not exchange data bandwidth? TCP connection which resources will consume?
Maybe idea is very bad, write also is very bad, but, please don't like do not spray, please civilization criticism,
CodePudding user response:
No one, only their top