Server Socket |
| BIND () |
| Listen () |
| Accept () |
| Receive () |
| Send () |
| Close |
To create a socket, you must bind the local IP address and port BIND (ipendpoint address) used for TCP communication)
The listen (INT backlog) Backlog parameter indicates that the system is waiting for the user.ProgramThe number of connections in the service queue. Any customer that exceeds the number of connections cannot access the server.
After the listen method is executed, the server is ready to receive any incoming connections, that is, the accept method is used. Socket myclient = mysocket. accect ();
When the program executes the accept method, it is paused until a client requests a connection. Once a client connects to the server, the myclient object contains all the connection information that communicates with the client. At this time, the client
The server can call the receive () and send () Methods to receive and send data.
Client Socket |
| |
| |
| Connect () |
| Send () |
| Receive () |
| Close |
For client programs, the client must also bind an address to the created socket object. However, instead of using the bind method, the client uses the connect method:
For example:
IPaddress IP = IPaddress. parse ("127.0.0.1 ");
Ipendpoint IEP = new ipendpoint (IP, 8000 );
Socket socket = new socket (addressfamily. InterNetwork,
Sockettype. Stream, protocoltype. TCP );
Socket. Connect (IEP );
Once a connection is established, the client can use the send () and receive () Methods to communicate with each other just like the method used by the server. Note that after the communication is complete, you must stop the session with shutdown and then close the session.
Closed socket
For example:
Socket. Shutdown (socketshutdown. Both );
Socket. Close ();
This method allows the socket object to wait until the data in the internal buffer zone is sent.