-
Notifications
You must be signed in to change notification settings - Fork 1
/
udpServer2.c
202 lines (171 loc) · 4.33 KB
/
udpServer2.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <mex.h>
#define BUFSIZE 177
#define PORT 9930
#define TIMEOUT 1
#define VLEN 10000
/* Globals */
int sockfd=-1;
struct sockaddr_in sa;
struct mmsghdr msgs[VLEN];
struct iovec iovecs[VLEN];
unsigned char bufs[VLEN][BUFSIZE+1];
struct timespec timeout;
/* Globals */
void diep(char *s)
{
perror(s);
exit(1);
}
void udpOpen()
{
int i;
if (sockfd>0) {
printf("Socket already opened!\n");
printf("sockfd: %d\n",sockfd);
return;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket()");
exit(EXIT_FAILURE);
}
int a = 10*1024*1024; /* Set socket buffer size to avoid dropped packets */
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char*)&a, sizeof(int)) == -1) {
fprintf(stderr, "Error setting socket receive buffer size \n");
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sa.sin_port = htons(9930);
if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
perror("bind()");
exit(EXIT_FAILURE);
}
memset(msgs, 0, sizeof(msgs));
for (i = 0; i < VLEN; i++) {
iovecs[i].iov_base = bufs[i];
iovecs[i].iov_len = BUFSIZE;
msgs[i].msg_hdr.msg_iov = &iovecs[i];
msgs[i].msg_hdr.msg_iovlen = 1;
}
timeout.tv_sec = TIMEOUT;
timeout.tv_nsec = 0;
}
void udpReceive(unsigned char *data )
{
int retval=-1, i;
/* Debug
if (sockfd>0) {
printf("Socket: %d\n",sockfd);
printf("MSG Debug, should be 1: %d\n",msgs[5].msg_hdr.msg_iovlen);
printf("Timeout Debug, should be 0: %d\n",timeout.tv_nsec);
return;
}
*/
retval = recvmmsg(sockfd, msgs, VLEN, 0, NULL);
if (retval == -1) {
perror("recvmmsg()");
exit(EXIT_FAILURE);
}
memcpy(data,bufs,(BUFSIZE+1)*VLEN);
/*
* for (i = 0; i < retval; i++) {
bufs[i][msgs[i].msg_len] = 0;
memcpy(data,bufs,(BUFSIZE+1)*VLEN);
printf("bufs[%d][3]: %u\n",i,bufs[i][10]);
printf("msgs[%d].msg_len=%d\n",i,msgs[i].msg_len);
}
*/
}
void udpClose()
{
printf("Closing socket %d\n",sockfd);
close(sockfd);
sockfd=-1;
}
/*
* #define _GNU_SOURCE
* #include <netinet/ip.h>
* #include <stdio.h>
* #include <stdlib.h>
* #include <string.h>
* #include <sys/socket.h>
*
* int
* main(void)
* {
* #define VLEN 10
* #define BUFSIZE 200
* #define TIMEOUT 1
* int sockfd, retval, i;
* struct sockaddr_in sa;
* struct mmsghdr msgs[VLEN];
* struct iovec iovecs[VLEN];
* char bufs[VLEN][BUFSIZE+1];
* struct timespec timeout;
*
* sockfd = socket(AF_INET, SOCK_DGRAM, 0);
* if (sockfd == -1) {
* perror("socket()");
* exit(EXIT_FAILURE);
* }
*
* sa.sin_family = AF_INET;
* sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
* sa.sin_port = htons(1234);
* if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
* perror("bind()");
* exit(EXIT_FAILURE);
* }
*
* memset(msgs, 0, sizeof(msgs));
* for (i = 0; i < VLEN; i++) {
* iovecs[i].iov_base = bufs[i];
* iovecs[i].iov_len = BUFSIZE;
* msgs[i].msg_hdr.msg_iov = &iovecs[i];
* msgs[i].msg_hdr.msg_iovlen = 1;
* }
*
* timeout.tv_sec = TIMEOUT;
* timeout.tv_nsec = 0;
*
* retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
* if (retval == -1) {
* perror("recvmmsg()");
* exit(EXIT_FAILURE);
* }
*
* printf("%d messages received\n", retval);
* for (i = 0; i < retval; i++) {
* bufs[i][msgs[i].msg_len] = 0;
* printf("%d %s", i+1, bufs[i]);
* }
* exit(EXIT_SUCCESS);
* }
*/
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char fun[81];
mwSize strlen=mxGetN(prhs[0])+1;
mxGetString(prhs[0],fun,strlen);
/* printf("%s\n",fun); */
if (strcmp(fun,"Open")==0) {
mexLock();
udpOpen();
}
if (strcmp(fun,"Receive")==0) {
unsigned char* data;
plhs[0] = mxCreateNumericMatrix(1,(BUFSIZE+1)*VLEN, mxUINT8_CLASS, mxREAL);
data = mxGetData(plhs[0]);
udpReceive(data);
}
if (strcmp(fun,"Close")==0) {
udpClose();
mexUnlock();
}
}