-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_client.c
51 lines (43 loc) · 1.11 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdbool.h>
int main() {
struct sockaddr_in remoteServer, remoteClient;
int sock;
char buffer[40], resultBuffer[40];
unsigned int len;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket");
exit(-1);
}
remoteServer.sin_family = AF_INET;
remoteServer.sin_port = htons(22355);
inet_pton(AF_INET, "127.0.0.1", &remoteServer.sin_addr);
len = sizeof(struct sockaddr_in);
while(1) {
bzero(buffer, 40);
bzero(resultBuffer, 40);
printf("Enter message ");
fgets(buffer, 40, stdin);
//buffer[strlen(buffer)] = '\0';
if (sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr *)&remoteServer, len) < 0) {
puts("send failed");
return 1;
} else {
puts("Sent to server successfully");
}
int n = recvfrom(sock, resultBuffer, 40, 0, (struct sockaddr *)&remoteServer, &len);
if(n < 0) {
perror("Failed to receive");
exit(-1);
}
printf("Got an ack %s\n", resultBuffer);
}
}