forked from manucabral/winsock-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows_client.c
More file actions
97 lines (79 loc) · 2.44 KB
/
windows_client.c
File metadata and controls
97 lines (79 loc) · 2.44 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
#include <stdio.h>
#include <winsock.h>
// Winsock library
#pragma comment(lib, "ws2_32.lib")
#define RECEIVE_BUFFER_SIZE 32
void error(char *message)
{
fprintf(stderr, "%s: %d", message, WSAGetLastError());
exit(1);
return;
}
void close(SOCKET so)
{
closesocket(so);
WSACleanup();
return;
}
int main(int argc, char *argv[])
{
SOCKET so;
WSADATA wsa;
char *server_ip;
char message[200];
char buffer[RECEIVE_BUFFER_SIZE];
unsigned short server_port;
int bytes_received;
struct sockaddr_in server;
if (argc != 3)
{
printf("Usage: %s <server_ip> <server_port>\n", argv[0]);
return 1;
}
server_ip = argv[1];
server_port = atoi(argv[2]);
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
error("Failed WSAStartup");
printf("Winsock Initialised.\n");
/**
* @brief Creates a socket and return socket descriptor
* AF_INET: IP Version 4 (Address Family)
* SOCK_STREAM: Oriented to TCP protocol (Type)
* 0: (Protocol)
*/
if ((so = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
error("Error on created the socket");
printf("Socket created.\n");
/**
* @brief Setting server values
* sin_family: Target addrees family (IP Version 4)
* sin_addr.s_addr: Target server ip conversion to unsigned long value
* sin_port = Value in TCP/IP network byte order (using htons)
*/
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(server_port);
/**
* @brief Establishes a connection
* so: Target socket descriptor.
* (struct sockaddr *) &server: A pointer to the sockaddr structure to which the connection should be established
* sizeof server: The lenght in bytes of the sockaddr struct.
*/
if (connect(so, (struct sockaddr *)&server, sizeof server) < 0)
error("Failed to connect");
printf("Connected to %s:%d\n", server_ip, server_port);
while (strcmp(message, "QUIT") != 0)
{
if ((bytes_received = recv(so, buffer, RECEIVE_BUFFER_SIZE, 0)) == SOCKET_ERROR)
error("Failed on receive");
buffer[bytes_received] = '\0';
printf("[Server]: %s", buffer);
scanf("%s", message);
printf("[You]: %s\n", message);
send(so, message, strlen(message) + 1, 0);
}
closesocket(so);
WSACleanup();
return 0;
}