Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 4cb5cd0

Browse files
committed
Add client.c
1 parent 5def047 commit 4cb5cd0

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ add_executable(CSE_381_IPC_PIPES
6464
add_executable(CSE_381_IPC_SHARED_MEMORY
6565
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/IPC (Interprocess Communication)/shared-memory.c")
6666

67+
add_executable(CSE_381_NETWORKING_CLIENT
68+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/Networking/client.c")
69+
6770
add_executable(CSE_381_NETWORKING_SERVER
6871
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/Networking/server.c")
6972

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
#include <netdb.h>
7+
#include <strings.h>
8+
#include <zconf.h>
9+
#include <string.h>
10+
11+
#define BUFFER_SIZE 256
12+
13+
int main(int argc, char *argv[]) {
14+
// Parse commandline parameters
15+
if (argc < 3) {
16+
fprintf(stdout, "Usage: %s HOST PORT\n", argv[0]);
17+
if (argc < 2) {
18+
fputs("HOST is required", stderr);
19+
}
20+
fputs("PORT is required", stderr);
21+
return EXIT_FAILURE;
22+
} else if (argc > 2) {
23+
fprintf(stdout, "Usage: %s HOST PORT\n", argv[0]);
24+
fputs("Too many arguments provided", stderr);
25+
return EXIT_FAILURE;
26+
}
27+
28+
struct hostent *host = gethostbyname(argv[1]);
29+
int port_number = atoi(argv[2]);
30+
31+
if (host == NULL) {
32+
fputs("Cannot find specified host", stderr);
33+
return EXIT_FAILURE;
34+
}
35+
36+
int socket_file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
37+
if (socket_file_descriptor < 0) {
38+
fputs("Cannot create IPv4 socket\n", stderr);
39+
return EXIT_FAILURE;
40+
}
41+
42+
// Build socket configuration
43+
struct sockaddr_in server_address;
44+
bzero((char *) &server_address, sizeof(server_address));
45+
server_address.sin_family = AF_INET;
46+
server_address.sin_port = htons(port_number);
47+
bcopy(host->h_addr,
48+
(char *) &server_address.sin_addr.s_addr,
49+
(size_t) host->h_length);
50+
51+
// Apply socket configuration and connect to server
52+
if (connect(socket_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
53+
fputs("Cannot make TCP connection to server\n", stderr);
54+
return EXIT_FAILURE;
55+
}
56+
57+
// Write
58+
char buffer[BUFFER_SIZE];
59+
bzero(buffer, BUFFER_SIZE);
60+
61+
printf("Enter a message to send to the server: ");
62+
fgets(buffer, BUFFER_SIZE - 1, stdin);
63+
64+
ssize_t bytes_transferred = write(socket_file_descriptor, buffer, strlen(buffer));
65+
if (bytes_transferred < 0) {
66+
fputs("Cannot write to socket\n", stderr);
67+
return EXIT_FAILURE;
68+
}
69+
70+
// Read
71+
bzero(buffer, BUFFER_SIZE);
72+
bytes_transferred = read(socket_file_descriptor, buffer, BUFFER_SIZE - 1);
73+
if (bytes_transferred < 0) {
74+
fputs("Cannot read from socket\n", stderr);
75+
return EXIT_FAILURE;
76+
}
77+
printf("Got message in reply: %s\n", buffer);
78+
79+
return EXIT_SUCCESS;
80+
}

0 commit comments

Comments
 (0)