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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <string.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
#define PORT "1231"
#define DEFAULT_BUFLEN 512
SOCKET ListenSocket;
SOCKET ClientSocket;
void process(const char* data);
int main()
{
WSADATA wsaData;
int iResult = 0;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(iResult != 0)
{
std::cout << "WSAStartup failed.\n";
system("pause");
return 1;
}
while(1)
{
struct addrinfo hints, *result;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, PORT, &hints, &result);
if(iResult != 0)
{
std::cout << "getaddrinfo failed.\n";
WSACleanup();
system("pause");
return 1;
}
ListenSocket = INVALID_SOCKET;
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if(ListenSocket == INVALID_SOCKET)
{
std::cout << "socket failed.\n";
freeaddrinfo(result);
WSACleanup();
system("pause");
return 1;
}
((sockaddr_in*)result->ai_addr)->sin_addr.S_un.S_addr = inet_addr("10.1.1.3");
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if(iResult == SOCKET_ERROR)
{
std::cout << "bind failed.\n";
closesocket(ListenSocket);
freeaddrinfo(result);
WSACleanup();
system("pause");
return 1;
}
freeaddrinfo(result);
if(listen(ListenSocket, 1) == SOCKET_ERROR)
{
std::cout << "listen failed.\n";
closesocket(ListenSocket);
WSACleanup();
system("pause");
return 1;
}
ClientSocket = INVALID_SOCKET;
ClientSocket = accept(ListenSocket, NULL, NULL);
if(ClientSocket == INVALID_SOCKET)
{
std::cout << "accept failed.\n";
closesocket(ListenSocket);
WSACleanup();
system("pause");
return 1;
}
char recvbuf[DEFAULT_BUFLEN];
int iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
char DataReceived[DEFAULT_BUFLEN * 2] = {NULL};
unsigned int c = 0;
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
//Do something with data received
for(int i = 0; i < iResult; i++)
{
if(recvbuf[i] == '\n')
{
DataReceived[c] = 0;
process(DataReceived);
c = 0;
continue;
}
DataReceived[c] = recvbuf[i];
c++;
}
}
else if (iResult == 0)
printf("Connection closed.\n");
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
closesocket(ClientSocket);
closesocket(ListenSocket);
}
WSACleanup();
return 0;
}
void process(const char* data)
{
const char cmd[] = {'e', 'x', 'i', 't', 0};
if(strcmp(data, cmd) == 0)
{
closesocket(ListenSocket);
closesocket(ClientSocket);
WSACleanup();
exit(0);
}
}
|