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