注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Tsecer的回音岛

Tsecer的博客

 
 
 

日志

 
 

从一个简单代码看listen及其它  

2013-07-02 00:45:09|  分类: Linux内核 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
一、listen意义、参数意义,socket缓冲区
简单的例子,说明一下内核中listen的作用及参数的意义。主要想说明一点:在socket执行listen之后,即使上层没有执行accept,此时的三次握手可以直接完成,也就是说connect过来的连接将会处于established状态,在这个状态下,客户端和服务器可以互相发送报文数据,正常沟通。顺便打开也可以看一下listen中参数的意义,以及socket默认缓冲区的大小。
一下代码从网络上代码拼凑修改而成。
二、服务器端代码
服务器的特点是只有listen操作,没有执行任何accept操作,listen参数为5,
[root@Harry socklistener]# cat socolistener.c
/*
 * Listing 1:
 * Simple "Hello, World!" server
 * Ivan Griffin (ivan.griffin@ul.ie)
 */

#include <stdio.h>   /* */
#include <stdlib.h>  /* exit() */
#include <string.h>  /* memset(), memcpy() */
#include <sys/utsname.h>   /* uname() */
#include <sys/types.h>
#include <sys/socket.h>   /* socket(), bind(),
                             listen(), accept() */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>  /* fork(), write(), close() */

/*
 * prototypes
 */
int _GetHostName(char *buffer, int length);

/*
 * constants
 */
const char MESSAGE[] = "Hello, World!\n";
const int BACK_LOG = 5;

int main(int argc, char *argv[])
{
    int serverSocket = 0,
        on = 0,
        port = 0,
        status = 0,
        childPid = 0;
    struct hostent *hostPtr = NULL;
    char hostname[80] = "";
    struct sockaddr_in serverName = { 0 };

    if (2 != argc)
    {
        fprintf(stderr, "Usage: %s <port>\n",
      argv[0]);
        exit(1);
    }
    port = atoi(argv[1]);

    serverSocket = socket(PF_INET, SOCK_STREAM,
      IPPROTO_TCP);
    if (-1 == serverSocket)
    {
        perror("socket()");
        exit(1);
    }


    status = _GetHostName(hostname,
      sizeof(hostname));
    if (-1 == status)
    {
        perror("_GetHostName()");
        exit(1);
    }

    hostPtr = gethostbyname(hostname);
    if (NULL == hostPtr)
    {
        perror("gethostbyname()");
        exit(1);
    }

    (void) memset(&serverName, 0,
      sizeof(serverName));
    (void) memcpy(&serverName.sin_addr,
      hostPtr->h_addr,
      hostPtr->h_length);

/*
 * to allow server be contactable on any of
 * its IP addresses, uncomment the following
 * line of code:
 * serverName.sin_addr.s_addr=htonl(INADDR_ANY);
 */

    serverName.sin_family = AF_INET;
    /* network-order */
    serverName.sin_port = htons(port);

    status = bind(serverSocket,
   (struct sockaddr *) &serverName,
        sizeof(serverName));
    if (-1 == status)
    {
        perror("bind()");
        exit(1);
    }

    status = listen(serverSocket, BACK_LOG);
    sleep(1000);

    return 0;
}

/*
 * Local replacement of gethostname() to aid
 * portability */
int _GetHostName(char *buffer, int length)
{
    struct utsname sysname = { 0 };
    int status = 0;

    status = uname(&sysname);
    if (-1 != status)
    {
        strncpy(buffer, sysname.nodename, length);
    }

    return (status);
}
[root@Harry socklistener]#
三、客户端代码
客户端的代码,特点是一旦连接上就不断的向服务器填鸭灌海的发送数据,每次发送一个页面。
[root@Harry socklistener]# cat client.c
#include  <unistd.h>
#include  <sys/types.h>       /* basic system data types */
#include  <sys/socket.h>      /* basic socket definitions */
#include  <netinet/in.h>      /* sockaddr_in{} and other Internet defns */
#include  <arpa/inet.h>       /* inet(3) functions */
#include <netdb.h> /*gethostbyname function */

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#define MAXLINE (4096)

void handle(int connfd);

int main(int argc, char **argv)
{
    char * servInetAddr = "127.0.0.1";
    int servPort = 6888;
    char buf[MAXLINE];
    int connfd;
    struct sockaddr_in servaddr;

    if (argc == 2) {
        servInetAddr = argv[1];
    }
    if (argc == 3) {
        servInetAddr = argv[1];
        servPort = atoi(argv[2]);
    }
    if (argc > 3) {
        printf("usage: echoclient <IPaddress> <Port>\n");
        return -1;
    }

    connfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(servPort);
    inet_pton(AF_INET, servInetAddr, &servaddr.sin_addr);

    if (connect(connfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
        perror("connect error");
        return -1;
    }
    handle(connfd);     /* do it all */
    close(connfd);
    printf("exit\n");
    exit(0);
}

void handle(int sockfd)
{
    char sendline[MAXLINE]={0};
    int mypid = getpid();
    printf("pid %d welcome to echoclient \n",mypid);
    int n = 0;
    for (;;) {
        n += write(sockfd, sendline, sizeof(sendline));
    printf("pid %d writen len %d\n", mypid,n);
    }
}
四、执行结果
同时创建18个进程,有12个socket连接connect成功,写入数据大小不等。
[root@Harry socklistener]# for ((i=0; i < 18; i++)) { ./client 127.0.0.1 2222 &  }
[1] 4134
pid 4135 welcome to echoclient
pid 4135 writen len 4096
pid 4135 writen len 8192
pid 4135 writen len 12288
pid 4135 writen len 16384
pid 4135 writen len 20480
pid 4135 writen len 24576
pid 4135 writen len 28672
pid 4135 writen len 32768
pid 4135 writen len 36864
pid 4135 writen len 40960
pid 4135 writen len 45056
pid 4135 writen len 49152
pid 4135 writen len 53248
pid 4135 writen len 57344
pid 4135 writen len 61440
pid 4135 writen len 65536
pid 4135 writen len 69632
pid 4135 writen len 73728
pid 4134 welcome to echoclient
pid 4134 writen len 4096
pid 4134 writen len 8192
pid 4134 writen len 12288
pid 4134 writen len 16384
pid 4134 writen len 20480
pid 4134 writen len 24576
pid 4134 writen len 28672
pid 4134 writen len 32768
pid 4134 writen len 36864
pid 4134 writen len 40960
pid 4134 writen len 45056
pid 4134 writen len 49152
pid 4134 writen len 53248
pid 4134 writen len 57344
pid 4134 writen len 61440
pid 4134 writen len 65536
pid 4134 writen len 69632
pid 4134 writen len 73728
[2] 4135
pid 4136 welcome to echoclient
pid 4136 writen len 4096
pid 4136 writen len 8192
pid 4136 writen len 12288
pid 4136 writen len 16384
pid 4136 writen len 20480
pid 4136 writen len 24576
pid 4136 writen len 28672
pid 4136 writen len 32768
pid 4136 writen len 36864
pid 4136 writen len 40960
pid 4136 writen len 45056
pid 4136 writen len 49152
pid 4136 writen len 53248
pid 4136 writen len 57344
pid 4136 writen len 61440
pid 4136 writen len 65536
pid 4136 writen len 69632
pid 4136 writen len 73728
[3] 4136
[4] 4137
pid 4137 welcome to echoclient
pid 4137 writen len 4096
pid 4137 writen len 8192
pid 4137 writen len 12288
pid 4137 writen len 16384
pid 4137 writen len 20480
pid 4137 writen len 24576
pid 4137 writen len 28672
pid 4137 writen len 32768
pid 4137 writen len 36864
pid 4137 writen len 40960
pid 4137 writen len 45056
pid 4137 writen len 49152
pid 4137 writen len 53248
pid 4137 writen len 57344
pid 4137 writen len 61440
pid 4137 writen len 65536
pid 4137 writen len 69632
pid 4137 writen len 73728
pid 4135 writen len 77824
pid 4135 writen len 81920
pid 4135 writen len 86016
pid 4135 writen len 90112
pid 4135 writen len 94208
pid 4135 writen len 98304
pid 4135 writen len 102400
pid 4135 writen len 106496
pid 4135 writen len 110592
pid 4135 writen len 114688
pid 4135 writen len 118784
pid 4135 writen len 122880
pid 4135 writen len 126976
pid 4135 writen len 131072
pid 4135 writen len 135168
pid 4135 writen len 139264
pid 4135 writen len 143360
pid 4135 writen len 147456
pid 4135 writen len 151552
pid 4135 writen len 155648
pid 4135 writen len 159744
pid 4135 writen len 163840
pid 4135 writen len 167936
pid 4135 writen len 172032
pid 4135 writen len 176128
pid 4135 writen len 180224
pid 4135 writen len 184320
pid 4135 writen len 188416
pid 4135 writen len 192512
pid 4135 writen len 196608
pid 4135 writen len 200704
pid 4135 writen len 204800
pid 4135 writen len 208896
pid 4135 writen len 212992
pid 4135 writen len 217088
pid 4135 writen len 221184
pid 4135 writen len 225280
pid 4135 writen len 229376
pid 4135 writen len 233472
pid 4135 writen len 237568
pid 4135 writen len 241664
pid 4135 writen len 245760
pid 4135 writen len 249856
pid 4135 writen len 253952
pid 4135 writen len 258048
pid 4135 writen len 262144
pid 4135 writen len 266240
pid 4135 writen len 270336
pid 4134 writen len 77824
pid 4134 writen len 81920
pid 4134 writen len 86016
pid 4134 writen len 90112
pid 4134 writen len 94208
pid 4134 writen len 98304
pid 4134 writen len 102400
pid 4134 writen len 106496
pid 4134 writen len 110592
pid 4134 writen len 114688
pid 4134 writen len 118784
pid 4134 writen len 122880
pid 4134 writen len 126976
pid 4134 writen len 131072
pid 4134 writen len 135168
pid 4134 writen len 139264
pid 4134 writen len 143360
pid 4134 writen len 147456
pid 4134 writen len 151552
pid 4134 writen len 155648
pid 4134 writen len 159744
pid 4134 writen len 163840
pid 4134 writen len 167936
pid 4134 writen len 172032
pid 4134 writen len 176128
pid 4134 writen len 180224
pid 4134 writen len 184320
pid 4134 writen len 188416
pid 4134 writen len 192512
pid 4134 writen len 196608
pid 4134 writen len 200704
pid 4134 writen len 204800
pid 4134 writen len 208896
pid 4134 writen len 212992
pid 4134 writen len 217088
pid 4134 writen len 221184
pid 4134 writen len 225280
pid 4134 writen len 229376
pid 4134 writen len 233472
pid 4134 writen len 237568
pid 4134 writen len 241664
pid 4134 writen len 245760
pid 4134 writen len 249856
pid 4134 writen len 253952
pid 4134 writen len 258048
pid 4134 writen len 262144
pid 4134 writen len 266240
pid 4134 writen len 270336
pid 4138 welcome to echoclient
pid 4138 writen len 4096
pid 4138 writen len 8192
pid 4138 writen len 12288
pid 4138 writen len 16384
pid 4138 writen len 20480
pid 4138 writen len 24576
pid 4138 writen len 28672
pid 4138 writen len 32768
pid 4138 writen len 36864
pid 4138 writen len 40960
pid 4138 writen len 45056
pid 4138 writen len 49152
pid 4138 writen len 53248
pid 4138 writen len 57344
pid 4138 writen len 61440
pid 4138 writen len 65536
pid 4138 writen len 69632
pid 4138 writen len 73728
pid 4136 writen len 77824
pid 4136 writen len 81920
pid 4136 writen len 86016
pid 4136 writen len 90112
pid 4136 writen len 94208
pid 4136 writen len 98304
pid 4136 writen len 102400
pid 4136 writen len 106496
pid 4136 writen len 110592
pid 4136 writen len 114688
pid 4136 writen len 118784
pid 4136 writen len 122880
pid 4136 writen len 126976
pid 4136 writen len 131072
pid 4136 writen len 135168
pid 4136 writen len 139264
pid 4136 writen len 143360
pid 4136 writen len 147456
pid 4136 writen len 151552
pid 4136 writen len 155648
pid 4136 writen len 159744
pid 4136 writen len 163840
pid 4136 writen len 167936
pid 4136 writen len 172032
pid 4136 writen len 176128
pid 4136 writen len 180224
pid 4136 writen len 184320
pid 4136 writen len 188416
pid 4136 writen len 192512
pid 4136 writen len 196608
pid 4136 writen len 200704
pid 4136 writen len 204800
pid 4136 writen len 208896
pid 4136 writen len 212992
pid 4136 writen len 217088
pid 4136 writen len 221184
pid 4136 writen len 225280
pid 4136 writen len 229376
pid 4136 writen len 233472
pid 4136 writen len 237568
pid 4136 writen len 241664
pid 4136 writen len 245760
pid 4136 writen len 249856
pid 4136 writen len 253952
pid 4136 writen len 258048
pid 4136 writen len 262144
pid 4136 writen len 266240
pid 4136 writen len 270336
[5] 4138
pid 4137 writen len 77824
pid 4137 writen len 81920
pid 4137 writen len 86016
pid 4137 writen len 90112
pid 4137 writen len 94208
pid 4137 writen len 98304
pid 4137 writen len 102400
pid 4137 writen len 106496
pid 4137 writen len 110592
pid 4137 writen len 114688
pid 4137 writen len 118784
pid 4137 writen len 122880
pid 4137 writen len 126976
pid 4137 writen len 131072
pid 4137 writen len 135168
pid 4137 writen len 139264
pid 4137 writen len 143360
pid 4137 writen len 147456
pid 4137 writen len 151552
pid 4137 writen len 155648
pid 4137 writen len 159744
pid 4137 writen len 163840
pid 4137 writen len 167936
pid 4137 writen len 172032
pid 4137 writen len 176128
pid 4137 writen len 180224
pid 4137 writen len 184320
pid 4137 writen len 188416
pid 4137 writen len 192512
pid 4137 writen len 196608
pid 4137 writen len 200704
pid 4137 writen len 204800
pid 4137 writen len 208896
pid 4137 writen len 212992
pid 4137 writen len 217088
pid 4137 writen len 221184
pid 4137 writen len 225280
pid 4137 writen len 229376
pid 4137 writen len 233472
pid 4137 writen len 237568
pid 4137 writen len 241664
pid 4137 writen len 245760
pid 4137 writen len 249856
pid 4137 writen len 253952
pid 4137 writen len 258048
pid 4137 writen len 262144
pid 4137 writen len 266240
pid 4137 writen len 270336
pid 4139 welcome to echoclient
pid 4139 writen len 4096
pid 4139 writen len 8192
pid 4139 writen len 12288
pid 4139 writen len 16384
pid 4139 writen len 20480
pid 4139 writen len 24576
pid 4139 writen len 28672
pid 4139 writen len 32768
pid 4139 writen len 36864
pid 4139 writen len 40960
pid 4139 writen len 45056
pid 4139 writen len 49152
pid 4139 writen len 53248
pid 4139 writen len 57344
pid 4139 writen len 61440
pid 4139 writen len 65536
pid 4139 writen len 69632
pid 4139 writen len 73728
[6] 4139
pid 4138 writen len 77824
pid 4138 writen len 81920
pid 4138 writen len 86016
pid 4138 writen len 90112
pid 4138 writen len 94208
pid 4138 writen len 98304
pid 4138 writen len 102400
pid 4138 writen len 106496
pid 4138 writen len 110592
pid 4138 writen len 114688
pid 4138 writen len 118784
pid 4138 writen len 122880
pid 4138 writen len 126976
pid 4138 writen len 131072
pid 4138 writen len 135168
pid 4138 writen len 139264
pid 4138 writen len 143360
pid 4138 writen len 147456
pid 4138 writen len 151552
pid 4138 writen len 155648
pid 4138 writen len 159744
pid 4138 writen len 163840
pid 4138 writen len 167936
pid 4138 writen len 172032
pid 4138 writen len 176128
pid 4138 writen len 180224
pid 4138 writen len 184320
pid 4138 writen len 188416
pid 4138 writen len 192512
pid 4138 writen len 196608
pid 4138 writen len 200704
pid 4138 writen len 204800
pid 4138 writen len 208896
pid 4138 writen len 212992
pid 4138 writen len 217088
pid 4138 writen len 221184
pid 4138 writen len 225280
pid 4138 writen len 229376
pid 4138 writen len 233472
pid 4138 writen len 237568
pid 4138 writen len 241664
pid 4138 writen len 245760
pid 4138 writen len 249856
pid 4138 writen len 253952
pid 4138 writen len 258048
pid 4138 writen len 262144
pid 4138 writen len 266240
pid 4138 writen len 270336
pid 4140 welcome to echoclient
pid 4140 writen len 4096
pid 4140 writen len 8192
pid 4140 writen len 12288
pid 4140 writen len 16384
pid 4140 writen len 20480
pid 4140 writen len 24576
pid 4140 writen len 28672
pid 4140 writen len 32768
pid 4140 writen len 36864
pid 4140 writen len 40960
pid 4140 writen len 45056
pid 4140 writen len 49152
pid 4140 writen len 53248
[7] 4140
[8] 4141
pid 4141 welcome to echoclient
pid 4141 writen len 4096
pid 4141 writen len 8192
pid 4141 writen len 12288
pid 4141 writen len 16384
pid 4141 writen len 20480
pid 4141 writen len 24576
pid 4141 writen len 28672
pid 4141 writen len 32768
pid 4141 writen len 36864
pid 4141 writen len 40960
pid 4141 writen len 45056
pid 4141 writen len 49152
pid 4141 writen len 53248
pid 4139 writen len 77824
pid 4139 writen len 81920
pid 4139 writen len 86016
pid 4139 writen len 90112
pid 4139 writen len 94208
pid 4139 writen len 98304
pid 4139 writen len 102400
pid 4139 writen len 106496
pid 4139 writen len 110592
pid 4139 writen len 114688
pid 4139 writen len 118784
pid 4139 writen len 122880
pid 4139 writen len 126976
pid 4139 writen len 131072
pid 4139 writen len 135168
pid 4139 writen len 139264
pid 4139 writen len 143360
pid 4139 writen len 147456
pid 4139 writen len 151552
pid 4139 writen len 155648
pid 4139 writen len 159744
pid 4139 writen len 163840
pid 4139 writen len 167936
pid 4139 writen len 172032
pid 4139 writen len 176128
pid 4139 writen len 180224
pid 4139 writen len 184320
pid 4139 writen len 188416
pid 4139 writen len 192512
pid 4139 writen len 196608
pid 4139 writen len 200704
pid 4139 writen len 204800
pid 4139 writen len 208896
pid 4139 writen len 212992
pid 4139 writen len 217088
pid 4139 writen len 221184
pid 4139 writen len 225280
pid 4139 writen len 229376
pid 4139 writen len 233472
pid 4139 writen len 237568
pid 4139 writen len 241664
pid 4139 writen len 245760
pid 4139 writen len 249856
pid 4139 writen len 253952
pid 4139 writen len 258048
pid 4139 writen len 262144
pid 4139 writen len 266240
pid 4139 writen len 270336
[9] 4142
[10] 4143
[11] 4144
[12] 4145
[13] 4146
[14] 4147
[15] 4148
[16] 4149
[17] 4150
[18] 4151
[root@Harry socklistener]# pid 4142 welcome to echoclient
pid 4142 writen len 4096
pid 4142 writen len 8192
pid 4142 writen len 12288
pid 4142 writen len 16384
pid 4142 writen len 20480
pid 4142 writen len 24576
pid 4142 writen len 28672
pid 4142 writen len 32768
pid 4142 writen len 36864
pid 4142 writen len 40960
pid 4142 writen len 45056
pid 4142 writen len 49152
pid 4142 writen len 53248
pid 4143 welcome to echoclient
pid 4143 writen len 4096
pid 4143 writen len 8192
pid 4143 writen len 12288
pid 4143 writen len 16384
pid 4143 writen len 20480
pid 4143 writen len 24576
pid 4143 writen len 28672
pid 4143 writen len 32768
pid 4143 writen len 36864
pid 4143 writen len 40960
pid 4143 writen len 45056
pid 4143 writen len 49152
pid 4143 writen len 53248
pid 4144 welcome to echoclient
pid 4144 writen len 4096
pid 4144 writen len 8192
pid 4144 writen len 12288
pid 4144 writen len 16384
pid 4144 writen len 20480
pid 4144 writen len 24576
pid 4144 writen len 28672
pid 4144 writen len 32768
pid 4144 writen len 36864
pid 4144 writen len 40960
pid 4144 writen len 45056
pid 4144 writen len 49152
pid 4144 writen len 53248
pid 4145 welcome to echoclient
pid 4145 writen len 4096
pid 4145 writen len 8192
pid 4145 writen len 12288
pid 4145 writen len 16384
pid 4145 writen len 20480
pid 4145 writen len 24576
pid 4145 writen len 28672
pid 4145 writen len 32768
pid 4145 writen len 36864
pid 4145 writen len 40960
pid 4145 writen len 45056
pid 4145 writen len 49152
pid 4145 writen len 53248
[root@Harry socklistener]#
[root@Harry socklistener]# connect error: Connection timed out
connect error: Connection timed out

pid 4140 writen len 53247
pid 4141 writen len 53247
pid 4142 writen len 53247
pid 4143 writen len 53247
pid 4144 writen len 53247
pid 4145 writen len 53247
pid 4146 writen len 53247
pid 4147 writen len 53247
之后写入数据开始减少,其中53247为之前53248-1,说明之前write返回值为-1。
五、系统配置
[root@Harry socklistener]# cat /proc/sys/net/ipv4/tcp_mem
81888    109184    163776
[root@Harry socklistener]# cat /proc/sys/net/ipv4/tcp_wmem
4096    16384    3493888
[root@Harry socklistener]# cat /proc/sys/net/ipv4/tcp_retr
tcp_retrans_collapse  tcp_retries1          tcp_retries2
[root@Harry socklistener]# cat /proc/sys/net/ipv4/tcp_retries1
3
[root@Harry socklistener]# cat /proc/sys/net/ipv4/tcp_retries2
15
[root@Harry socklistener]# uname -a
Linux Harry 2.6.31.5-127.fc12.i686.PAE #1 SMP Sat Nov 7 21:25:57 EST 2009 i686 athlon i386 GNU/Linux


有兴趣的同学可以解释一下现象。
  评论这张
 
阅读(1438)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017