communication socket programming --tcp

tcp flow of communication : connection-oriented, reliable transmission, the byte stream oriented

Client (client):

  1. Create socket
  2. Address bindings (not recommended active)
  3. Issues a connection request to the server
  4. Transmit / receive data
  5. Closes the socket

Server (server):

  1. Create socket - to establish contact with the card in the kernel to create a socket structure
  2. Information for the socket is bound address - tell the operating system what data processing to me - put me in the receive buffer
  3. Start listening - tells the operating system can start receiving cry Justifying connection requests - Client and server communication must first be established tcp connection - the ability to ensure that both parties have the data sent and received.
    When the server receives a connection request from a client, the process of establishing the connection is done in the kernel.
    tcp new socket server for receiving the first socket only new connection request, creates a new socket after the connection request arrives, created for subsequent communications with the client
  4. Socket socket server program to obtain a handle to the newly created operating
    server program receives a new connection in the kernel, creating a new socket
    server program gets socket operation handle kernel newly created
    on the server side program of this operation get communicates with the client handle
  5. The server can perform data communication with the client through the new socket
    receive data / transmit data
    tcp at the time of data communication, it can be transmitted whoever (because this time the connection has been established)
  6. Close socket: release of resources

tcp socket interface described communication:

1, to create a socket

int socket(int domain,int type,int protocol)

(Address field AF_INET, socket type SOCK_STREAM, protocol type IPPROTO_TCP go)

2, binding address information

int bind(int sockfd,sockaddr *addr,socklent_len);

3, start listening

int listen(int sockfd,int backlog);

backlog: kernel tcp connection pending queue maximum number of nodes - determines the amount of time the server can receive the same client connection requests

Network attack: SYN flood attacks:
malicious hosts continue to send a large number of requests to establish a connection to the server to establish a socket if the server is still for each request, the moment will run out of resources a server crash

4, the server program gets a handle to the newly created socket operation

int accept(int sockfd;struct sockaddr *addr,socklen_t len);

This step is a function from the kernel has completed acquisition socket connection queue, freeing up a seat can continue to receive new connections
sockfd: descriptor - which specify a new socket socket server socket for the new connection created by the acquisition, is the listening socket - socket listen queue is a queue
addr: address of the client information
len: input-output parameters: Specifies the length you want to get address information, as well as obtain information on the length of the address actually get
return value: the right to return specific operation of the newly created socket handle - file descriptors - for subsequent communication to the specified client; failed return -1

5, communicates via the new socket descriptor obtaining

int recv(int sockfd,char *buf,int len,int flag);

int send(int sockfd,char *data,int len,int flag);

Because the socket socket tcp described communication end information not only active, but also information on the terminal, so recv / send respective programs specified by the need to go to the end address.
sockfd: socket for server, it is newly created accept returns descriptor

6, the socket is closed

int close(int fd);

7, the client initiates a connection to the server

int connect(int sockfd,struct sockaddr *srvaddr,socklen_t len);

sockfd: client socket - if not bound source address, the operating system will automatically binding, while the service will end address information into the socket structure described in
srvaddr: address information of the server

Basic tcp server program problems

Process server blocking :
1, after the server finished processing the first data transceiver client, cycle and call accept, because there are no more connections come, so the flow back up here, waiting for the arrival of a new connection
2, the server receives the first program a client connection request, the data come Recv waiting to receive a first client; however, if the first client does not transmit data, program flow blocking here, even if the follow-up of other connections will not be processed.

Ultimately, the server will block the process, because the program does not know what is coming when there will be new connections do not know when the data will come, it can only fix the program flow to obtain written first connection, and then receive data (with clients end communication); however, get connected and communicate with clients are likely to be blocked;
therefore conclude: blocking server application process because the service-side code is not flexible enough;

Current solutions: the implementation of multi-stream solutions
server program has two main functions and are likely to clog: Get a new connection / communication with each client
if the main flow of execution is only responsible for obtaining new connections; after acquiring a new connection, create an execution flow as a function of communicating with the client
to create a new execution flow communication client with
no new connection comes, only the main execution flow blockage; but the client communicates with the flow of execution can still communicate
and if the blocking the implementation of client communications, the main flow of execution can still get a new connection, create a new execution flow communication with another client

Summary: The
server was unable to continue with the client communication
main reasons : the server process is fixed, in a process to perform a variety of functions, and each function of the interface are likely to clog
solutions : multi-stream program execution, each execution flow is only responsible for a function, a blocking execution flow will not affect the operation of other execution flow of
specific technologies : multi-process / multi-threaded

1, multi-process-tasking :
parent only get a new connection, if the acquisition of the new connection, then New Connection to create a child process to be responsible for communicating this new connection to the specified client
(1), the parent and child code-sharing, but the data are there; Sons new process has a socket descriptor; however, the parent process does not communicate with the client, so we must close the New socket
(2), the parent process to be taken to avoid zombie, and therefore the signal callback were waiting process

Avoid zombie child process: SIGCHLD signal custom processing; parent and child each have a newsock; parent process does not use resources to promptly shut down to prevent leakage
2, multithreaded processing :
the main thread just get a new connection, if acquired new connection, create a new thread to be responsible for communication through this new connection with the client
(1), unlike the multi-threaded multi-process, directly by copying all the data has a parent process; thread entry function, you want to pass a new socket communicating words, the function must form new parameter passing operation incoming socket thread entry function.
Address parameter passing time can not directly pass local variables - local variables because the loop is completed will be released
①, do not apply directly to a local variable, but in a direct application heap space, will deliver the first address space to a thread

TcpSocket *newsock = new TcpSocket();

②, directly to the value of the descriptor passed through mass participation
(2), resources are shared between multiple threads of course, means that the new sockets is only one, albeit the socket threads to be processed , but the main thread can not arbitrarily close the New socket (shared among threads file descriptor table)

Published 59 original articles · won praise 15 · views 1582

Guess you like