-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
90 lines (76 loc) · 2.32 KB
/
client.c
File metadata and controls
90 lines (76 loc) · 2.32 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main() {
// Server address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
// Convert socket address from plain text into numberic binary
int address_text_to_binary_result = inet_pton(
// Address family (IPv4 or IPv6)
AF_INET,
// String being passed in
"127.0.0.1",
// dst, buffer in which to store the numberic address. For this we can pass in the .sin_addr of the server address
&server_address.sin_addr
);
// Exit if the address conversion failed. Here we have to check if it's <= 0, because 0 is returned if the input string is not valid, and -1 for any other error
if (address_text_to_binary_result <= 0) {
printf("Address text to binary conversion failed\n");
return 1;
}
// Create the client socket
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
// Exit if creation failed
if (client_socket < 0) {
printf("Client socket creation failed\n");
return 1;
}
// Attempt to connect to the server
int connected = connect(
// The client socket we're using to connect to the server with
client_socket,
// Address, address of the serer socket to connect to
(struct sockaddr *)&server_address,
// Size in bytes of the server address
sizeof(server_address)
);
// Exit if connection fails
if (connected < 0) {
printf("Connection to server failed\n");
return 1;
}
// Send a message to the server
char * message = "Hello from the client socket once again";
int num_of_bytes_sent = send(
// Client socket to send with
client_socket,
// buffer, pointer to the char * message to send
message,
// buffer size
strlen(message),
// flage
0
);
if (num_of_bytes_sent < 0) {
printf("Failed to sent message");
return 1;
}
// Read the response from the server
char buffer[1024];
int num_of_bytes_read = read(client_socket, buffer, 1024);
// Exit if failed to read response
if (num_of_bytes_read < 0) {
printf("Failed to read response from server");
return 1;
}
for (int i = 0; i < num_of_bytes_read; i++) {
printf("Char %i: %c\n", i, buffer[i]);
}
printf("Client success\n");
return 0;
}