-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_client.c
77 lines (59 loc) · 1.94 KB
/
udp_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
/*
************ UDP CLIENT CODE *******************
* Adapted from Code Source: http://www.programminglogic.com/sockets-programming-in-c-using-udp-datagrams/
* WARNING: Proof of Concept, only. Not tested for security or efficiency.
*
* Original file header:
*
* Copyright (c) 2008 Amit Singh (osxbook.com).
* http://osxbook.com/software/fslogger/
*
* Source released under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.0.
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt for details.
*
*/
#include "udp_client.h"
// Default ports
char ip[MAX_IP] = "127.0.0.1";
int port = 12345;
void set_dest(char *ip_addr, int dport){
strncpy(ip,ip_addr,MAX_IP - 1);
port = dport;
}
//int set_sock(){
void set_sock(){
/*Create UDP socket*/
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = inet_addr(ip);
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Initialize size variable to be used later on*/
addr_size = sizeof serverAddr;
}
int send_packet(char *packet, int plen){
/*Send message to server*/
sendto(clientSocket,packet,plen,0, (struct sockaddr *)&serverAddr,addr_size);
return 0;
}
/*
// Original function that does everything
int send_udp(char *packet, int plen){
int clientSocket;
struct sockaddr_in serverAddr;
socklen_t addr_size;
//Create UDP socket
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
//Configure settings in address struct
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = inet_addr(ip);
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
//Initialize size variable to be used later on
addr_size = sizeof serverAddr;
//Send message to server
sendto(clientSocket,packet,plen,0, (struct sockaddr *)&serverAddr,addr_size);
return 0;
}
*/