-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_client.c
More file actions
60 lines (50 loc) · 2.07 KB
/
sample_client.c
File metadata and controls
60 lines (50 loc) · 2.07 KB
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
#include <stdio.h>
#include "udp.h"
#define CLIENT_PORT 10000
// client code
int main(int argc, char *argv[])
{
// This function opens a UDP socket,
// binding it to all IP interfaces of this machine,
// and port number CLIENT_PORT.
// (See details of the function in udp.h)
printf("opening socket... \n");
int sd = udp_socket_open(0);
// Variable to store the server's IP address and port
// (i.e. the server we are trying to contact).
// Generally, it is possible for the responder to be
// different from the server requested.
// Although, in our case the responder will
// always be the same as the server.
struct sockaddr_in server_addr, responder_addr;
// Initializing the server's address.
// We are currently running the server on localhost (127.0.0.1).
// You can change this to a different IP address
// when running the server on a different machine.
// (See details of the function in udp.h)
printf("setting up port... \n");
int rc = set_socket_addr(&server_addr, "127.0.0.1", SERVER_PORT);
// Storage for request and response messages
char client_request[BUFFER_SIZE], server_response[BUFFER_SIZE];
// Demo code (remove later)
strcpy(client_request, "conn Alice");
// This function writes to the server (sends request)
// through the socket at sd.
// (See details of the function in udp.h)
printf("sending...\n");
rc = udp_socket_write(sd, &server_addr, client_request, BUFFER_SIZE);
printf("sent: %s\n", client_request);
if (rc > 0)
{
// This function reads the response from the server
// through the socket at sd.
// In our case, responder_addr will simply be
// the same as server_addr.
// (See details of the function in udp.h)
printf("reading...\n");
int rc = udp_socket_read(sd, &responder_addr, server_response, BUFFER_SIZE);
// Demo code (remove later)
printf("server_response: %s\n", server_response);
}
return 0;
}