-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
49 lines (39 loc) · 1.57 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ws2tcpip.h> // For Windows socket definitions and functions
#include <winsock2.h> // For TCP/IP protocols and internet functionality
#define PORT 8080 // Port number for the socket connection
#define SERVER "127.0.0.1"
#define BUFFER_SIZE 1024 // Buffer size for data transmission
//////////////////////////////// MAIN /////////////////////////////////////////////////////////////////////
int main() {
WSADATA wsaData; // Structure to hold information about the Windows Sockets implementation
// Initialize Winsock
// Variable to store the result of Winsock functions
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// Create a socket for the connection to the server
SOCKET socketFD = createTCPIp4Socket();
if (socketFD == INVALID_SOCKET) {
printf("Error creating socket: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
struct sockaddr_in* addressPtr = createIP4Address(SERVER, PORT);
if (!addressPtr) {
closesocket(socketFD);
WSACleanup();
return 1;
}
int result = connectToServer(socketFD, *addressPtr); // Dereference pointer when passing
// Cleanup
free(addressPtr); //free the allocated memory
closesocket(socketFD);
WSACleanup();
return result; // Use the result of the connection attempt
}
/////////////////////////////// END OF MAIN////////////////////////////////////////////////////////////