SCTP 学习

ubuntu 下安装 libsctp, lksctp(不行的话在redHat下试验)

apt-get install libsctp lksctp

服务端程序

  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: #include <string.h>
  4: #include <sys/types.h>
  5: #include <sys/socket.h>
  6: #include <netinet/in.h>
  7: #include <netinet/sctp.h>
  8:
  9: const int buf_size = 1024;
 10: const int echo_port = 2013;
 11:
 12: char* usage_msg="usage: sctp_echo [ip_addr]+ istreams ostreams";
 13: void usage()
 14: {
 15:     fprintf(stderr,"%s\n",usage_msg);
 16:     exit(1);
 17: }
 18:
 19: int main(int argc, char** argv)
 20: {
 21:
 22:     char buf[buf_size];
 23:     time_t time;
 24:
 25:     int sockfd, client_sockfd,flags;
 26:     int nread,len;
 27:     struct sockaddr_in serv_addr, client_addr;
 28:     struct sctp_initmsg initmsg;
 29:     struct sctp_status status;
 30:     sctp_assoc_t associd;
 31:     struct sctp_sndrcvinfo sinfo;
 32:     struct sctp_event_subscribe events;
 33:
 34:     struct sockaddr_in *addrs;
 35:     int addr_size = sizeof(struct sockaddr_in);
 36:     int addr_count = 1;
 37:     int num = 0;
 38:
 39:     if(argc < 4) usage();
 40:
 41:     sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_SCTP);
 42:     if(sockfd < 0)
 43:     {
 44:         fprintf(stderr,"socket descriptor is invalid.");
 45:         exit(1);
 46:     }
 47:
 48:     serv_addr.sin_family = AF_INET;
 49:     serv_addr.sin_addr.s_addr = INADDR_ANY;
 50:     serv_addr.sin_port = htons(echo_port);
 51:
 52:     int brst = sctp_bindx(sockfd,(struct sockaddr*)&serv_addr,
 53:                 addr_count,SCTP_BINDX_ADD_ADDR);
 54:     if(brst == -1)
 55:     {
 56:         fprintf(stderr,"error while binding address.");
 57:         exit(1);
 58:     }
 59:
 60:     memset(&initmsg,0,sizeof(initmsg));
 61:     initmsg.sinit_max_instreams = atoi(argv[2]);
 62:     initmsg.sinit_num_ostreams = atoi(argv[3]);
 63:     fprintf(stdout,"asking for input streams:%d, output streams %d\n",
 64:             initmsg.sinit_max_instreams, initmsg.sinit_num_ostreams);
 65:
 66:     brst = setsockopt(sockfd,IPPROTO_SCTP,SCTP_INITMSG,
 67:             &initmsg,sizeof(initmsg));
 68:     if(brst != 0)
 69:     {
 70:         fprintf(stderr,"error while setting sctp options.\n");
 71:         exit(1);
 72:     }
 73:
 74:     listen(sockfd,5);
 75:
 76:     for(;;)
 77:     {
 78:         len = sizeof(client_addr);
 79:         client_sockfd = accept(sockfd, (struct sockaddr*)&client_addr, &len);
 80:         if(client_sockfd == -1)
 81:         {
 82:             fprintf(stderr,"error while accepting connection, continue.\n");
 83:             continue;
 84:         }
 85:
 86:         memset(&status,0,sizeof(status));
 87:         len = sizeof(status);
 88:         status.sstat_assoc_id = 0;
 89:
 90:         brst = getsockopt(client_sockfd,IPPROTO_SCTP,SCTP_STATUS,&status,&len);
 91:         if(brst == -1)
 92:         {
 93:             fprintf(stderr,"error while getting socket option.\n");
 94:         }
 95:
 96:         fprintf(stdout,"input stream:%d, output stream:%d\n",
 97:                 status.sstat_instrms, status.sstat_outstrms);
 98:         for(;;)
 99:         {
100:             len = sizeof(&sinfo,sizeof(sinfo));
101:             memset(buf,0,buf_size);
102:             nread = sctp_recvmsg(client_sockfd,buf,buf_size,
103:                     (struct sockaddr*)&client_addr, &len, &sinfo, &flags);
104:             if(nread <= 0) break;
105:             fprintf(stdout,"%s\n",buf);
106:             fprintf(stdout,"read %d bytes on channel %d\n",
107:                     nread, sinfo.sinfo_stream);
108:         }
109:         close(client_sockfd);
110:     }
111:
112:     return 0;
113: }
114: 

客户端程序

  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: #include <string.h>
  4: #include <unistd.h>
  5: #include <sys/types.h>
  6: #include <sys/socket.h>
  7: #include <netinet/in.h>
  8: #include <netinet/sctp.h>
  9:
 10: const int buf_size = 1024;
 11: const int echo_port = 2013;
 12:
 13: char *usage_msg = "usage echo_c ip_addr istreams ostream ochannel";
 14: void usage()
 15: {
 16:     fprintf(stderr,"%s\n", usage_msg);
 17:     exit(1);
 18: }
 19:
 20: int main(int argc, char** argv)
 21: {
 22:     int sockfd, len;
 23:     struct sockaddr_in serv_addr;
 24:     struct sockaddr_in *addrs;
 25:     int addr_size = 1;
 26:
 27:     char* msg = "hello world.\n";
 28:     struct sctp_initmsg initmsg;
 29:     struct sctp_status status;
 30:     struct sctp_sndrcvinfo sinfo;
 31:     int ochannel;
 32:
 33:     int brst;
 34:
 35:     if(argc < 5) usage();
 36:     sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_SCTP);
 37:     if(sockfd < 0)
 38:     {
 39:         fprintf(stderr,"error while initializing socket.\n");
 40:         exit(1);
 41:     }
 42:
 43:     addrs = malloc(sizeof(struct sockaddr_in));
 44:     serv_addr.sin_family = AF_INET;
 45:     serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
 46:     serv_addr.sin_port = htons(echo_port);
 47:
 48:     memcpy(addrs,&serv_addr,sizeof(struct sockaddr_in));
 49:     memset(&initmsg, 0, sizeof(initmsg));
 50:     initmsg.sinit_max_instreams = atoi(argv[2]);
 51:     initmsg.sinit_num_ostreams = atoi(argv[3]);
 52:     fprintf(stdout,"asking for %d input stream, %d output stream.\n",
 53:             initmsg.sinit_max_instreams,initmsg.sinit_num_ostreams);
 54:
 55:     brst = setsockopt(sockfd, IPPROTO_SCTP,SCTP_INITMSG,
 56:             &initmsg,sizeof(initmsg));
 57:     if(brst != 0)
 58:     {
 59:         fprintf(stderr,"error while set socket option.\n");
 60:         exit(1);
 61:     }
 62:
 63:     brst = sctp_connectx(sockfd, (struct sockaddr*) addrs, 1);
 64:     if(brst < 0)
 65:     {
 66:         fprintf(stderr, "can not connect to host.\n");
 67:         exit(1);
 68:     }
 69:
 70:     memset(&status,0,sizeof(status));
 71:     len = sizeof(status);
 72:     status.sstat_assoc_id = 1;
 73:
 74:     brst = getsockopt(sockfd, IPPROTO_SCTP,SCTP_STATUS,&status,&len);
 75:     if(brst < 0)
 76:     {
 77:         fprintf(stderr,"error while geting socket option.\n");
 78:         exit(1);
 79:     }
 80:
 81:     fprintf(stdout,"input stream:%d, output stream:%d\n",
 82:             status.sstat_instrms,status.sstat_outstrms);
 83:
 84:     ochannel = atoi(argv[4]);
 85:     if(ochannel >= status.sstat_outstrms)
 86:     {
 87:         fprintf(stdout,"writing on illegal channel %d \n", ochannel);
 88:     }
 89:
 90:     bzero(&sinfo,sizeof(sinfo));
 91:     sinfo.sinfo_stream = ochannel;
 92:     sctp_send(sockfd, msg, strlen(msg), &sinfo, 0);
 93:     sinfo.sinfo_flags = SCTP_EOF;
 94:     sctp_send(sockfd,NULL,0,&sinfo,0);
 95:
 96:     close(sockfd);
 97:     return 0;
 98: }
 99: 
时间: 2024-11-05 07:34:37

SCTP 学习的相关文章

计算机网络学习1-网络层次

计算机网络学习中最基本的是计算机网络分层模型,目前学习需要了解的有两种 1.OSI(开放系统互连)有七层,记忆方法:无数网传会报应(物理层,数据链路层,网络层,传输层,会话层,表示层,应用层) 具体每层功能分析: 1.1物理层:规定建立,维持,断开通信端之间的机械.电气.功能.过程特性.单位bit: 物理层标准有:RJ-45,V3.5等 1.2数据链路层:不可靠物理介质上提供可靠的传输.单位frame: 数据链路层标准:SDLC,HDLC,PPP,帧中继等 1.3网络层:对数据包进行路由选择和转

C++ Socket 学习笔记

Socket学习笔记 以下均为整理,做参考之用. IP Address IP地址是指互联网协议地址(英语:Internet Protocol Address,又译为网际协议地址),是IP Address的缩写.IP地址是IP协议提供的一种统一的地址格式,它为互联网上的每一个网络和每一台主机分配一个逻辑地址,以此来屏蔽物理地址的差异. IP地址被用来给Internet上的电脑一个编号.大家日常见到的情况是每台联网的PC上都需要有IP地址,才能正常通信.我们可以把"个人电脑"比作"

Nmap 源码学习三 nmap_main主程序分析

主体程序位置在nmap.cc line:1640 学习要点: 程序在1650行,新建一个主机的单例对象, #ifndef NOLUA /* Only NSE scripts can add targets */ NewTargets *new_targets = NULL; /* Pre-Scan and Post-Scan script results datastructure */ ScriptResults *script_scan_results = NULL; #endif 从168

学习笔记之linux网络属性配置及其命令用法

Linux网络属性配置 先来了解一点网络的基础知识:(这些只需记住) TCP/IP:协议栈(使用中的模型) ISO,OSI:协议栈(学习中的模型) MAC:Media Access Control(介质访问控制) 48bits:(48位二进制) ICANN:24bits, 2^24(组织将这个地址做了定义) 地址块:2^24 网桥(bridge):MAC地址表 网桥自身特性: 静态指定: 动态学习:根据原地址学习: 交换机(switch):多端口网桥: IP(Internet protocol互

Chapter 2. OpenSSL的安装和配置学习笔记

Chapter 2. OpenSSL的安装和配置学习笔记 2.1 在linux上面安装OpenSSL我还是做点No paper事情比较在行,正好和老师的课程接轨一下.以前尝试过在Windows上面安装过openSSL,这次正好在Linux上面实现一下. 希望各位园友门纠错,征求意见中. 2.1.1 安装OpenSSL的系统环境和编译环境 测试环境:Linux MINT 16 Petra ? 1 2 3 4 5 6 $ lsb_release -a #查看linux发行版本系统信息 No LSB

LTE学习之路(4)——概述(续)

[EPS承载] 基础知识——TFT 数据包过滤器:通常是在数据包传送过程中允许或阻止它们的通过.如果要完成数据包过滤,就要设置好规则来指定哪些类型的数据包被允许通过和哪些类型的数据包将会被阻止. TFT(Traffic Flow Template):是关联到EPS承载上的一个数据包过滤器的集合,分为上行过滤模版UL TFT(UpLink TFT)和下行过滤模版DL TFT(DownLink TFT). UL TFT是一组上行数据包过滤器,DL TFT是一组下行数据包过滤器.每一个专用承载都关联一

TCP/IP协议组学习笔记

TCP/IP协议族学习笔记: 一.基础概念: (1)TCP(Transmission Control Protocol) 传输控制协议. (2)IP(Internet Protocol)网际协议.IP是一种不可靠的无连接数据协议,一种尽最大努力交付的服务. (3)ISP(Internet Service Provider)因特网服务提供者. (4)ISO 国际标准化组织. (5)IEEE 电气和电子工程学会. (6)W3C 万维网联盟. (7) OMA 开放移动联盟. (8)RFC(Reques

学习笔记:第一章——计算机网络概述

学习笔记:第一章--计算机网络概述 1.0 计算机网络的定义:一些互相连接的,自治的计算机的集合称为计算机网路. 1.1 计算机网络在信息时代中的作用:连通和共享. 1.2 因特网概述:       网络的概念:网络(network)是由若干个结点(node)和连接这些结点的链路(link)组成. 网络中的结点可以是计算机,集线器,交换机和路由器: 网络和网络是通过路由器连接: 因特网是世界上最大的网络. 1.3 因特网的组成: 从因特网的工作方式上将其分为以下两大快: (1)边缘部分:由所有的

《UNIX网络编程 卷1》之&quot;学习环境搭建&quot;(CentOS 7)

<UNIX网络编程 卷1>的源码可以从www.unpbook.com下载得到.解压之后的目录为unpv13e.  1. 编译 进入unpv13e目录,按如下步骤编译: 1 ./configure 2 3 cd lib 4 make // 可能遇到问题:redefinition of ‘struct in_pktinfo’ 5 6 cd ../libfree 7 make 8 9 cd ../libroute 10 make //这一步可能会出错,可忽略,只是表示你的系统不支持 4.4BSD,并