-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer_C++.cpp
85 lines (67 loc) · 2.56 KB
/
Server_C++.cpp
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
#include <winsock2.h> //socket library for windows
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 800 //Max length of buffer
#define PORT 80 //Port for UDP (default 8888)
int main()
{
SOCKET s; // socket object
struct sockaddr_in server, si_other; // socket server and client
int slen ,recv_len; // len server - client
char buf[BUFLEN]; // buffer (recieved data)
char msg[BUFLEN]="Hello client"; // buffer (sent data)
WSADATA wsa; // WSA object
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( PORT );
slen = sizeof(si_other);
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//keep listening for data
while(1)
{
printf("Waiting for data...");
fflush(stdout);
//clear the buffer by filling null
memset(buf,'\0', BUFLEN);
//try to receive some data
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
//print details of the client and data received
printf("Received data from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n" , buf);
//now reply the client
if (sendto(s, msg, strlen(msg), 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
}
closesocket(s); //close socket connection
WSACleanup(); //clean the WSA
return 0;
}