SCTP一对一模式(TCP)模式的客户端代码

发布时间:2016-12-9 21:28:51 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"SCTP一对一模式(TCP)模式的客户端代码",主要涉及到SCTP一对一模式(TCP)模式的客户端代码方面的内容,对于SCTP一对一模式(TCP)模式的客户端代码感兴趣的同学可以参考一下。

以下代码为基于sctp协议一对一模式(TCP)模式的客户端代码. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/sctp.h> #include <arpa/inet.h> const int buf_size = 1024; const int echo_port = 19000; char *usage_msg = "usage echo_c ip_addr istreams ostream ochannel"; void usage() { fprintf(stderr,"%s\n", usage_msg); exit(1); } int main(int argc, char** argv) { int sockfd, len; struct sockaddr_in serv_addr; struct sctp_initmsg initmsg; struct sctp_status status; struct sctp_sndrcvinfo sinfo; int ochannel; if(argc < 5) usage(); sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_SCTP); if(sockfd < 0) { printf("error while initializing socket.\n"); exit(1); } struct sockaddr_in *addrs = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(argv[1]); serv_addr.sin_port = htons(echo_port); memcpy(addrs,&serv_addr,sizeof(struct sockaddr_in)); memset(&initmsg, 0, sizeof(initmsg)); initmsg.sinit_max_instreams = atoi(argv[2]); initmsg.sinit_num_ostreams = atoi(argv[3]); printf("asking for %d input stream, %d output stream.\n", initmsg.sinit_max_instreams,initmsg.sinit_num_ostreams); int brst = setsockopt(sockfd, IPPROTO_SCTP,SCTP_INITMSG, &initmsg,sizeof(initmsg)); if(brst != 0) { printf("error while set socket option.\n"); exit(1); } brst = sctp_connectx(sockfd, (struct sockaddr*) addrs, 1); if(brst < 0) { printf("can not connect to host.\n"); exit(1); } memset(&status,0,sizeof(status)); len = sizeof(status); status.sstat_assoc_id = 1; brst = getsockopt(sockfd, IPPROTO_SCTP,SCTP_STATUS,&status,(socklen_t*)&len); if(brst < 0) { printf("error while geting socket option.\n"); exit(1); } printf("input stream:%d, output stream:%d\n", status.sstat_instrms,status.sstat_outstrms); ochannel = atoi(argv[4]); if(ochannel >= status.sstat_outstrms) { printf("writing on illegal channel %d \n", ochannel); } while(true) { bzero(&sinfo,sizeof(sinfo)); printf("input a message: "); char buff[100]; bzero(buff, 100); scanf("%s", buff); printf("input ochannel:"); scanf("%d", &ochannel); sinfo.sinfo_stream = ochannel; sctp_send(sockfd, buff, strlen(buff), &sinfo, 0); } close(sockfd); return 0; } 以下代码为基于SCTP协议的一对一模式(TCP)模式的客户端代码。

上一篇:云存储我来了~
下一篇:数字信号产生之贝努里分布的随机数

相关文章

相关评论