implementation of TCP-based Java socket Programming instance code: Simple implementation of the server and client socket programming, and pass a simple string. (You need to bind the server's port when the server declares the socket, and the port is used only by one socket for the critical resource.) When the server is programmed, the Accept () method is called and the server enters the waiting connection state. )
Java TCP/IP server connection established by the source Code implementation (SOCKET->BIND->LISTEN->ACCEPT):
(1) In the socket programming, we first set up a server, and set up a socket on the server, the socket binding interface, and then listen to the client may send a connection to establish the request. This set of processes is done at the ServerSocket construction time (it should be explained that both sockets and ServerSocket in Java are java.io.Closeable interfaces implemented. Let's look at how the constructor implements the port bindings and the connection listening:
Public ServerSocket (intthrows IOException { thisnull); }
for a constructor that has only port input, we re-use another constructor: serversocket (int port, int backlog, inetaddress bindaddr). Where port is the port number of the binding, the backlog is the maximum number of connections and defaults to 50. The BINDADDR is a local IP address that is bound.
PublicServerSocket (intPortintBacklog, inetaddress bindaddr)throwsIOException {Setimpl (); if(Port < 0 | | port > 0xFFFF) Throw NewIllegalArgumentException ("Port value out of range:" +port); if(Backlog < 1) Backlog= 50; Try{bind (Newinetsocketaddress (BINDADDR, Port), backlog); } Catch(SecurityException e) {close (); Throwe; } Catch(IOException e) {close (); Throwe; } }View Code
We can see that there is the code for bind (new Inetsocketaddress (BINDADDR, Port), backlog) in this constructor, which is used for IP address and port binding , We can only set the port when using sockets, the IP address defaults to NULL. Enter into the Bind method:
Public voidBind (socketaddress endpoint,intBacklogthrowsIOException {...Try{SecurityManager Security=System.getsecuritymanager (); if(Security! =NULL) Security.checklisten (Epoint.getport ()); Getimpl (). Bind (Epoint.getaddress (), Epoint.getport ()); Getimpl (). Listen (backlog); Bound=true; } Catch(SecurityException e) {bound=false; Throwe; } Catch(IOException e) {bound=false; Throwe; } }
I omitted the code is mainly to carry out security checks, check whether the port has been closed or occupied, the address passed in is legitimate, and provide the corresponding processing methods. And the try package is the core thing. This involves SecurityManager, SocketImpl class, not currently known. However, the bind and listen fields that we can see should be the code that the socket is ready to go into the blocking state (because it calls accept later, so I guess this step is not blocking).
After the Accept method is related to do not know the class, I wait for the search Shuo first not to find abuse, first write to this, after looking at ripe again summary.
(2) As for the service socket after writing, the client is how to connect, in fact, is also completed in the construction phase (Socket->connect), the following list of relevant source code, after doing analysis. (Connect is the method of actively establishing a connection, which means that each type of request requires programming a socket, and the client and the server are in a many-to-one relationship at this point)
Public int port) throws unknownhostexception, IOException { thisnullnew inetsocketaddress (host, port): New inetsocketaddress (inetaddress.getbyname (null), port), nullTrue ); }
Where host is the IP address and port is the port number.
PrivateSocket (socketaddress address, socketaddress localaddr,BooleanStreamthrowsIOException {Setimpl (); //Backward Compatibility if(Address = =NULL) Throw NewNullPointerException (); Try{Createimpl (stream); if(Localaddr! =NULL) bind (LOCALADDR); Connect (address); } Catch(IOException | IllegalArgumentException |SecurityException e) { Try{close (); } Catch(IOException CE) {e.addsuppressed (CE); } Throwe; } }View Code
If you see the Connect method enough, don't follow it down.
Java Socket Programming