Home > other >  Socket server keep all connections to the server Socket to a linked list, rather than create a threa
Socket server keep all connections to the server Socket to a linked list, rather than create a threa

Time:10-02

The server we run tomcat and our Socket server at the same time, the end of the service through a short connection to the tomcat to send data, and the end of the service and our Socket server to maintain a long connection, the Socket server to save all connections to the server Socket to a list, rather than create a thread to monitor whether the client to send other requests, active to the client in we need to push information (such as a chat messages), tomcat server first send this information to the Socket server, Socket server to the list of corresponding client Socket connection and send this information,
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 List Conns=null;
Public static void main (String [] args) throws FileNotFoundException {
Int id=0;
SocketReciever reciever=new SocketReciever ();
List Conns=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 List GetConns () {
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
  • Related