-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient_C++.cpp
More file actions
70 lines (54 loc) · 1.78 KB
/
Client_C++.cpp
File metadata and controls
70 lines (54 loc) · 1.78 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
// sentdata.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <winsock2.h> //socket windows
#include <ws2tcpip.h> //tcpip library
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define BUFLEN 800 //Max length of buffer
#define PORT 80 //Max length of buffer
#define SERVER "192.168.4.49" //Port for UDP (default 8888)
int main()
{
WSADATA wsa; //WSA object
struct sockaddr_in si_other; //struct client
int s, slen=sizeof(si_other); //len client
char buf[BUFLEN]; //buffer (recieved data)
char const msg[BUFLEN]="Hello server!"; //buffer (sent data)
//Initialise winsock
printf("\nInitialising Winsock...");
if(WSAStartup(MAKEWORD(2,2), &wsa) !=0)
{
printf("Failed WSA. EC: %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
//Create a socket
printf("nInitialising Socket...\n");
if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
printf("Failed SOCKET. EC: %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
//keep sending data
while(1)
{
if(sendto(s, msg , strlen(msg), 0, (struct sockaddr *)&si_other, slen) == SOCKET_ERROR)
{
printf("Failed sendto. EC: %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
}
closesocket(s); //close connection
WSACleanup(); //clean wsa
return 0;
}