LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1097|回复: 3

Unix域socket 使用setsockopt设置SO_RCVNBUF后竟然无效?

[复制链接]
发表于 2005-10-8 12:34:49 | 显示全部楼层 |阅读模式
设置完后,使用getsockopt,取出SO_RCVNBUF设置为0,感觉setsockopt没有成功啊。
另外,有没有系统的工具可以查看某个socket的SNDBUF和RCVBUF长度的?
谢谢!
发表于 2005-10-8 13:02:47 | 显示全部楼层
SO_RCVNBUF不对, 应该是SO_RCVBUF.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-8 13:47:00 | 显示全部楼层
哦,这是笔误。是SO_RCVBUF.编译通过并运行,使用getsockopt取出来的值是0,不知道为什么?
回复 支持 反对

使用道具 举报

发表于 2005-10-9 09:52:25 | 显示全部楼层
获取TCP, UDP, UNIX(Stream, Datagram)的sendbuf , recvbuf大小的代码:


/*
*
*/

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
    int                    nsize;
    int                    tcpfd, udpfd, unixfd, unixudpfd;
    socklen_t            nlen = 8;
   
    if ((tcpfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        printf("error: socket\n");
        perror("reason");
        exit(1);
    }
   
    nsize = 0;
    if (getsockopt(tcpfd, SOL_SOCKET, SO_SNDBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("TCP send buff size: %d\n", nsize);

    nsize = 0;
    if (getsockopt(tcpfd, SOL_SOCKET, SO_RCVBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("TCP recv buff size: %d\n", nsize);

    nsize = 0;
    if ((udpfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("error: socket\n");
        perror("reason");
        exit(1);
    }
   
    nsize = 0;
    if (getsockopt(udpfd, SOL_SOCKET, SO_SNDBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("UDP send buff size: %d\n", nsize);

    nsize = 0;
    if (getsockopt(udpfd, SOL_SOCKET, SO_RCVBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("UCP recv buff size: %d\n", nsize);

    if ((unixfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        printf("error: socket\n");
        perror("reason");
        exit(1);
    }
   
    nsize = 0;
    if (getsockopt(unixfd, SOL_SOCKET, SO_SNDBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("Unix domain Stream send buff size: %d\n", nsize);

    nsize = 0;
    if (getsockopt(unixfd, SOL_SOCKET, SO_RCVBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("Unix domain Stream recv buff size: %d\n", nsize);
   
    if ((unixudpfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
        printf("error: socket\n");
        perror("reason");
        exit(1);
    }
   
    nsize = 0;
    if (getsockopt(unixudpfd, SOL_SOCKET, SO_SNDBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("Unix domain Datagram send buff size: %d\n", nsize);

    nsize = 0;
    if (getsockopt(unixfd, SOL_SOCKET, SO_RCVBUF, &nsize, &nlen)) {
        printf("error: getsockopt\n");
        perror("reason");
        exit(1);
    }
    printf("Unix domain Datagram recv buff size: %d\n", nsize);

    close(tcpfd);
    close(udpfd);
    close(unixfd);

    return 0;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表