socket foundation (c language)

Do not use select
ordinary basic socket connection, the response to multiple clients
is in order, according to the order of establishing the connection
1. Establish the server socket 2. The server socket is ready to establish a new connection, accept is blocked 3. The client establishes a socket , the client connects to the server 4. The server accepts returns, establishes a connection and blocks to recv, ready to accept data (several clients will resume several connections) 5. The client sends data to the socket 6. The connection of the server At this time, recv receives the data and returns the code without saying a word, first write it in c: server.c
















#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define MAXLINE 4096

int main(int argc, char** argv)
{
    int    listenfd, connfd;
    struct sockaddr_in     servaddr;
    char    buff[4096];
    int     n;

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
    printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
    exit(0);
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(6666);

    if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
    printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
    exit(0);
    }

    if( listen(listenfd, 10) == -1){
    printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
    exit(0);
    }

    printf("======waiting for client's request======\n");
    while(1){
    if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
        printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
        continue;
    }
    n = recv(connfd, buff, MAXLINE, 0);
    buff[n] = '\0';
    printf("recv msg from client: %s\n", buff);
    close(connfd);
    }

    close(listenfd);
}     

client.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
    
#define MAXLINE 4096

int main(int argc, char** argv)
{   
    int    sockfd, n;
    char    recvline[4096], sendline[4096];
    struct sockaddr_in    servaddr;
    
    if( argc != 2){
    printf("usage: ./client <ipaddress>\n");
    exit(0);
    }
    
    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
    printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
    exit(0);
    }
    
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(6666);
    if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
    printf("inet_pton error for %s\n",argv[1]);
    exit(0);
    }
    
    if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
    printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
    exit(0);
    }
    
    printf("send msg to server: \n");
    fgets(sendline, 4096, stdin);
    if( send(sockfd, sendline, strlen(sendline), 0) < 0)
    {
    printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
    }

    close(sockfd);
    exit(0);
}


gcc server.c -o server
gcc client.c -o client

test server side:
[root@centos32 c]# ./server
======waiting for client's request======


Open three clients and observe
the first one
[root@centos32 c]# ./client  127.0.0.1
send msg to server:


Do not enter the first
-------------------------
the second
[root@centos32 c]# ./client  127.0.0.1
send msg to server:
hello this is 2222222222222
Enter

Observe that there is no response on the server side

---------------------
The third client:

[root@centos32 c]# ./client  127.0.0.1
send msg to server:
hello this is 33333333
Enter

Observe that there is no response on the server side

---------------
Go to the first client
[root@centos32 c]# ./client  127.0.0.1
send msg to server:
this is 111111111

Enter,
observe the server

as
[root@centos32 c]# ./server
======waiting for client's request======
recv msg from client: hello this is 1111111111

recv msg from client: hello this is 2222222222222

recv msg from client: hello this is 33333333


It is found that the value returned by the server is returned in
the After the connection is established to the server, it is blocked on recv.
Even if the subsequent client sends data first, it will not return


first.

If there is no select, etc., the
socket is blocked.
When the first client connects, it will accept and wait there. Only the first one has a response, and the last two will continue. The
server The return is also returned in the order of connection establishment.


Look at socket establishment process. There are a
total of four processes. The server blocks twice, waiting for the connection and receiving the message. The
client establishes a connection and sends data twice to trigger the server

------ -------(1)------------
Server:
The server first establishes a socket handle
listenfd = socket(AF_INET, SOCK_STREAM, 0)
bind address
bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)
listen socket
listen(listenfd, 10)
to establish a connection handle,
and block ##################
connfd =accept(listenfd, ( struct sockaddr*)NULL, NULL)
Note
------------(2)---------
Client:
Create a socket handle
sockfd = socket(AF_INET, SOCK_STREAM, 0)
use this handle to connect to the remote address
connect( sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)
--------------(3)--------------
blocking return of server accept
n = recv(connfd, buff, MAXLINE, 0);
blocking data ################
---------------(4)- --Client handle
send data send(sockfd, sendline, strlen(sendline), 0) ----------------(5)----------- --- Server: accept message recv and return ######################################## ############## There will be a problem with the order of ordinary sockets. If you want, the later client sends data first, and the server does not wait for the response of the previous client, and directly returns the corresponding response of the later client. , you need to introduce select, etc. Reference












http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610661&siteId=291194637