-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_socket.c
More file actions
99 lines (79 loc) · 2.73 KB
/
client_socket.c
File metadata and controls
99 lines (79 loc) · 2.73 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
91
92
93
94
95
96
97
98
99
/*
A C program that creates, initializes, and connects a client socket to a server socket.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//#include <arpa/inet.h> - Unavailable on Windows - Used in Linux
#include <winsock2.h>
#include <ws2tcpip.h>
#define BUFFER_SIZE 1024
//Getting server information through command-line arguments
int main(int argc, char *argv[]){
//Checking for required command-line arguments
if(argc != 3){
fprintf(stderr, "Usage %s <Server Address> <Port>\n", argv[0]);
return EXIT_FAILURE;
}
//Initializing Winsock - Windows method
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){
fprintf(stderr, "Winsock initialization failed\n");
return EXIT_FAILURE;
}
const char* server_address = argv[1];
int port = atoi(argv[2]);
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if(client_socket < 0){
perror("Socket Creation Failed");
WSACleanup();
return EXIT_FAILURE;
}
//Defining server address structure
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
//Converting server address to binary format
if (inet_pton(AF_INET, server_address, &server.sin_addr) <= 0){
perror("Invalid server address");
close(client_socket);
return EXIT_FAILURE;
}
//Connecting to the server
if(connect(client_socket, (struct sockaddr *)&server, sizeof(server)) < 0){
perror("Connection Failed");
close(client_socket);
return EXIT_FAILURE;
}
//Connection successful
printf("Connected to server %s on port %d\n", server_address, port);
//Messages can be sent or received from the server
char buffer[BUFFER_SIZE];
while(1){
printf("Type your message to be sent to the server (type exit to quit): ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = '\0'; //Removing newline character
if(strcmp(buffer, "exit") == 0){
//End of messages
break;
}
send(client_socket, buffer, strlen(buffer), 0);
int bytes_received = recv(client_socket, buffer, BUFFER_SIZE-1, 0);
if(bytes_received > 0){
buffer[bytes_received] = '\0';
printf("Server: %s\n", buffer);
}
else{
printf("Server Disconnected \n");
break;
}
}
//Closing the socket
close(client_socket);
printf("Connection closed\n");
// Cleanup Winsock
WSACleanup();
return EXIT_SUCCESS;
}