-
Notifications
You must be signed in to change notification settings - Fork 17
/
sctp-client.c
129 lines (100 loc) · 3.22 KB
/
sctp-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//SCTP client for 2 stream communcations
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#define SIZE 1024
char buf[SIZE];
#define TIME_PORT 2013
char *usage_msg = "usage: app ip-addr istreams ostreams";
void usage() {
fprintf(stderr, "%s\n", usage_msg);
exit(1);
}
int main(int argc, char *argv[]) {
int sockfd, client_sockfd;
int nread, len;
struct sockaddr_in serv_addr, client_addr;
time_t t;
struct sockaddr_in *addresses;
int addr_size = sizeof(struct sockaddr_in);
int addr_count = 1;
int port = TIME_PORT;
int n;
struct sctp_initmsg initmsg;
struct sctp_status status;
sctp_assoc_t associd;
struct sctp_sndrcvinfo sinfo;
struct sctp_event_subscribe events;
int flags;
int ret;
// if (argc != 4) usage();
/* create endpoint */
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_SCTP);
if (sockfd < 0) {
perror(NULL);
exit(2);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(port);
if (sctp_bindx(sockfd, (struct sockaddr *) &serv_addr, addr_count,SCTP_BINDX_ADD_ADDR) == -1)
{
perror("sctp bindx");
exit(2);
}
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_max_instreams = 2;
initmsg.sinit_num_ostreams = 2;
printf("Asking for: input streams: %d, output streams: %d\n",initmsg.sinit_max_instreams,initmsg.sinit_num_ostreams);
if (setsockopt(sockfd, IPPROTO_SCTP,SCTP_INITMSG, &initmsg, sizeof(initmsg))) {
perror("set sock opt\n");
}
/* specify queue */
listen(sockfd, 5);
for (;;) {
len = sizeof(client_addr);
client_sockfd = accept(sockfd, (struct sockaddr *) &client_addr,
&len);
if (client_sockfd == -1) {
perror(NULL); continue;
}
memset(&status, 0, sizeof(status));
len = sizeof(status);
status.sstat_assoc_id = 0;
if (getsockopt(client_sockfd, IPPROTO_SCTP,SCTP_STATUS, &status, &len) == -1) {
perror("get sock opt\n");
}
printf("Got: input streams: %d, output streams: %d\n",status.sstat_instrms,status.sstat_outstrms);
/* transfer data */
len = sizeof(client_addr);
bzero(&sinfo, sizeof(sinfo));
nread = sctp_recvmsg(client_sockfd, buf, SIZE,(struct sockaddr *) &client_addr, &len,&sinfo, &flags);
if (nread == 0) {
break;
}
printf("stream now is %d\n",sinfo.sinfo_stream);
printf("read CTime on stream %d\n", sinfo.sinfo_stream);
printf("sinfo flags: %d\n", sinfo.sinfo_flags);
write(1, buf, nread);
memset(&events , 0, sizeof(events));
events.sctp_data_io_event = 1;
ret = setsockopt(client_sockfd, SOL_SCTP, SCTP_EVENTS, &events, sizeof(events));
memset(&buf, 0, sizeof(buf));
/* transfer data */
len = sizeof(client_addr);
bzero(&sinfo, sizeof(sinfo));
nread = sctp_recvmsg(client_sockfd, buf, SIZE,(struct sockaddr *) &client_addr, &len,&sinfo, &flags);
if (nread == 0) {
break;
}
printf("stream now is %d\n",sinfo.sinfo_stream);
printf("read GMTime on stream %d\n", sinfo.sinfo_stream);
printf("sinfo flags: %d\n", sinfo.sinfo_flags);
write(1, buf, nread);
close(client_sockfd);
}
}