-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
53 lines (49 loc) · 1.2 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <event.h>
#include <errno.h>
#include <net/ethernet.h>
#define D_READ_BUFFER 255
#define D_MAX_BUFFER 4095
void sendMsg (int i32ClientFd, struct sockaddr_in * sAddr)
{
char i8Buffer [D_MAX_BUFFER + 1] = {0};
int i32RcvSize = -1;
sprintf(i8Buffer,"%s","Hello World 5001");
printf("Sending %s\n",i8Buffer);
i32RcvSize = sendto (i32ClientFd, i8Buffer, D_MAX_BUFFER, 0, (struct sockaddr *)sAddr, sizeof(struct sockaddr));
if (i32RcvSize < 0)
{
perror("Sendto:");
return;
}
return;
}
int createUdpSocket()
{
return socket (AF_INET, SOCK_DGRAM, 0);
}
int main (int argc, char **argv)
{
int i32ClientFd = -1;
struct sockaddr_in servAddr;
memset (&servAddr, 0, sizeof (struct sockaddr_in));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl (INADDR_ANY);
servAddr.sin_port = htons (5000);
i32ClientFd = createUdpSocket();
if (i32ClientFd < 0)
{
printf("Socket creation failed: %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
sendMsg(i32ClientFd,&servAddr);
close(i32ClientFd);
exit (EXIT_SUCCESS);
}