-
Notifications
You must be signed in to change notification settings - Fork 15
/
sender.c
51 lines (43 loc) · 987 Bytes
/
sender.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "sender.h"
typedef struct sockaddr_in sockaddr_in;
typedef struct sockaddr sockaddr;
struct Ctx
{
int sock;
sockaddr_in target;
};
typedef struct Ctx Ctx;
void *sender_open (const char *ip, int port)
{
Ctx *ctx = malloc(sizeof(Ctx));
ctx->sock = socket(AF_INET, SOCK_DGRAM, 0);
if (ctx->sock == -1) {
fprintf(stderr, "%s: create sock err\n", __func__);
exit(-1);
}
ctx->target.sin_family = AF_INET;
ctx->target.sin_port = htons(port);
ctx->target.sin_addr.s_addr = inet_addr(ip);
return ctx;
}
void sender_close (void *snd)
{
Ctx *c = (Ctx*)snd;
close(c->sock);
free(c);
}
int sender_send (void *snd, const void *data, int len)
{
assert(len < 65536);
Ctx *c = (Ctx*)snd;
return sendto(c->sock, data, len, 0, (sockaddr*)&c->target, sizeof(sockaddr_in));
}