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 100
|
#include <ws2tcpip.h>
#include <iostream>
const char CONNECT_IP[] = "127.0.0.1";
const char CONNECT_PORT[] = "55000";
using namespace std;
int main( void )
{
cout << "SERVER::\n\n";
cout << "Discovering Winsock version... ";
WSAData webservicesdata; //Declare WSAData
WORD winsockVersion=MAKEWORD(2, 0); //Winsock 2.0
cout << "Winsock version " << winsockVersion << " discovered.\n";
cout << "Starting winsock... ";
int err=WSAStartup(winsockVersion, &webservicesdata);
if (err==-1)
{
cout << "Error Starting Winsock (v" << winsockVersion <<")\n";
}else if (err==0){
cout << "Complete.\n";
}
//Getaddrinfo - the info of a certain address
struct addrinfo GivenHostInfo;
struct addrinfo* PointerToSocketAddrResults;
//Client addrinfo
struct sockaddr_storage client_addr;
socklen_t client_addr_size;
memset(&GivenHostInfo, 0, sizeof GivenHostInfo);
GivenHostInfo.ai_flags = AI_PASSIVE;
GivenHostInfo.ai_family = AF_UNSPEC;
GivenHostInfo.ai_socktype = SOCK_STREAM;
//Obtain addrinfo and fill socketinfo object
getaddrinfo(CONNECT_IP, CONNECT_PORT, &GivenHostInfo, &PointerToSocketAddrResults);
//Create Socket File Descriptor (pointer to where the socket is in memory)
cout << "Creating socket file descriptor... ";
SOCKET MySocketFD = socket(PointerToSocketAddrResults->ai_family,PointerToSocketAddrResults->ai_socktype,PointerToSocketAddrResults->ai_protocol);
if (MySocketFD != INVALID_SOCKET){cout << "Complete.\n";}else{cout << WSAGetLastError() << " - ERROR!\n";}
//bind() the Socket to the specified port
cout << "Binding to Port<" << CONNECT_PORT << ">... ";
SOCKET binderr = bind(MySocketFD, PointerToSocketAddrResults->ai_addr, PointerToSocketAddrResults->ai_addrlen);
if (binderr==-1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}
//listen() on specified port
cout << "Listening on Port<" << CONNECT_PORT << ">... ";
SOCKET listenErr = listen (MySocketFD, 1);
if (listenErr == -1 || listenErr == INVALID_SOCKET){cout << WSAGetLastError() << " - ERROR!\n";}else{cout<< "Complete.\n";}
//accept() a connection on the specified port
cout << "Accepting client connection on Port<" << CONNECT_PORT << ">... ";
client_addr_size = sizeof client_addr;
SOCKET ClientSocketFD = accept(MySocketFD,(struct sockaddr *)&client_addr,&client_addr_size);
if (ClientSocketFD == -1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}
//send() a piece of data
cout << "Sending information... ";
int sendData = send(ClientSocketFD,"Hello World!",13,0);
if (sendData == -1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}
//recv() a piece of data
cout << "Receiving information... ";
char DataFromClient;
int recvData = recv(ClientSocketFD,&DataFromClient,20,0);
if (recvData == -1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}
//Close all connections
cout << "Closing connections... ";
shutdown(MySocketFD,2);
shutdown(ClientSocketFD,2);
cout << "Complete.\n";
//Free dynamicly allocated addrinfo
cout << "Freeing Address Info... ";
freeaddrinfo(PointerToSocketAddrResults);
cout << "Complete.\n";
//Clean up Winsock
cout << "Cleaning Up WinSock... ";
int socketcleanup = WSACleanup();
if (socketcleanup == 0){cout << "Complete.\n";}else{cout << WSAGetLastError() << " - ERROR!\n";}
system("PAUSE");
return 0;
}
|