Home > Back-end >  [the socket network programming] server socket and what's the difference between a client socke
[the socket network programming] server socket and what's the difference between a client socke

Time:09-21

As shown in figure is client program, the client to the server first sends a string of a string, and then again after the server receives the sent back to the client,
My question is, crossed the socket is not their own client socket, since to send data to the server, and to receive data from the service end, why not use server socket but the socket?
I also see a server-side program, send and recv USES is the client from the server socket

CodePudding user response:

3. When the socket is called to create a socket, the return of the socket descriptor it exists in agreement of space, but not a specific address, if you want to give it to give an address, you must call the bind () function, the system will connect the call (), listen () automatically when randomly assigned to a port, usually the server at startup will bind a well-known address (such as IP address + port number), is used to provide services, customers can through it to link to the server; And the client need not specify, there is automatically assigned a port and its own IP address, which is why the server often listen the call before will call the bind () and the client will not have to call, the client but in the connect () is randomly generated by the system a,
5. Listen for the second parameter can be lined up to the maximum number of connection, the socket () to create the default is an active type socket, will listen function socket is a passive type, waiting for the client's connection request,
6. The accept () of the second parameter used to return to the address of the client agreement (don't need to be set to null), if the accept successfully, then the return value is automatically generated by the kernel is a description of the new words (the first parameter is the server socket descriptor, returns the connected socket descriptor, two sockets are not the same), on behalf of the TCP link with return customers,
A server usually only create a listening socket descriptor, he in the life cycle of the server, the kernel for each by the server process to accept the customer creates a connected socket description words, when the server completed for a customer service, the corresponding connected soocket descriptor was shut down,

CodePudding user response:

1 server accept the client connect, creates a new socket and client communication, that is, the service side, there are two sockets, a socket user listen and accept, the other a socket is used to send and recv;
2 the client only a socket, used to connect, send and recv,
  • Related