getsockopt函数总是错误

撕碎 2013-11-06 11:15:43

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <netinet/sctp.h>
#include <errno.h>

void
Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
{
if (getsockopt(fd, level, optname, optval, optlenptr) < 0) {
printf("errno :%d\n", errno);
exit(0);
}

}

int
main(void)
{
int sock_fd, retsz;
struct sctp_status status;;

retsz = sizeof(int);
//bzero(&status, retsz);
sock_fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
Getsockopt(sock_fd,IPPROTO_SCTP, SCTP_STATUS, &status, &retsz);
printf("%d\n", status.sstat_outstrms);
}

我想问一下为什么getsockopt函数会出错,显示 Invalid argument,errno = 22;
是不是不支持SCTP_STATUS?如果是,要怎么才能支持?怎么才知道支不支持呢?谢谢
...全文
634 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoheHopee 2015-07-04
  • 打赏
  • 举报
回复
要获取sctp_status一定要有sctp的association id:只有发送或者接受信息之后才能获取这个socket option infomation struct sctp_sndrcvinfo sri; rd_sz = Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf), (SA *)&cliaddr, &len, &sri,&msg_flags); status.sstat_assoc_id = sri.sinfo_assoc_id;
JoheHopee 2015-07-04
  • 打赏
  • 举报
回复
unp这本书的例子有些是不适用与linux的,这里的错误就是sctp_peer_addr_params不能用来获取sctp association id
JoheHopee 2015-07-04
  • 打赏
  • 举报
回复
The SCTP_PEER_ADDR_PARAMS socket option in the sctp_address_to_associd() function cannot be used to learn the association id, at least not on linux. Since you already get the association id from the sctp_recvmsg() call, use the association id of the struct sctp_sndrcvinfo instead: status.sstat_assoc_id = sri.sinfo_assoc_id; getsockopt( sock_fd, IPPROTO_SCTP, SCTP_STATUS, &status, &retsz);
撕碎 2013-11-10
  • 打赏
  • 举报
回复
引用 7 楼 mymtom 的回复:
还没有解决吗?
没有阿,其他选项能正常支持,就sctp的选项,我一直在猜是否是对sctp选项的参数不支持,你觉得呢?
mymtom 2013-11-10
  • 打赏
  • 举报
回复
还没有解决吗?
撕碎 2013-11-08
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <netinet/sctp.h>
#include <errno.h>

void
Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
{
		if (getsockopt(fd, level, optname, optval, optlenptr) < 0) {
			printf("errno :%d\n", errno);
			exit(-1);
		}
					
}


int
main(void)
{
	int	sock_fd, retsz;
	struct sctp_status status;;
	//int	status;

	retsz = sizeof(struct sctp_status);
	/*sock_fd = socket(AF_INET, SOCK_STREAM, 0);
	Getsockopt(sock_fd, SOL_SOCKET,  SO_RCVBUF, &status, &retsz);
	printf("%d\n", status);*/
	sock_fd = socket(AF_INET,SOCK_SEQPACKET, IPPROTO_SCTP);
	bzero(&status, sizeof(struct sctp_status)); 
	Getsockopt(sock_fd, IPPROTO_SCTP, SCTP_STATUS, &status, &retsz);
	printf("%d\n", status.sstat_outstrms);
}
这是全部代码
mymtom 2013-11-07
  • 打赏
  • 举报
回复
试试修改23行 retsz = sizeof(int); 改为 retsz = sizeof(status);
撕碎 2013-11-06
  • 打赏
  • 举报
回复
    retsz = sizeof(int);
之前修改成获取缓冲区的时候,忘记修改回来了。。。
撕碎 2013-11-06
  • 打赏
  • 举报
回复
这段代码我改过,用通用套接字选项获取接受缓冲区大小,能成功。。
撕碎 2013-11-06
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:
errno = 22 一般情况都是传递的参数不合法导致! 确认下你socket值是否有效,还有后面几个参数设备是否有效!
socket有效,因为上面的实例代码只是一部分,其中在我原先的程序中,它能通过socket值从客户端获取信息,其他参数的话,大写的是常量,status用bzero初始化过了(我那里注释忘去掉了),retsz也经过初始化,这两个参数会被getsockopt修改。。
max_min_ 2013-11-06
  • 打赏
  • 举报
回复
errno = 22 一般情况都是传递的参数不合法导致! 确认下你socket值是否有效,还有后面几个参数设备是否有效!

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧