Skip to content

Instantly share code, notes, and snippets.

@Kermit
Created April 22, 2010 21:26
Show Gist options
  • Save Kermit/375841 to your computer and use it in GitHub Desktop.
Save Kermit/375841 to your computer and use it in GitHub Desktop.
Text communication with non-blocking sockets.
#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include <string>
#include <vector>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
void MakeAddrStruct(sockaddr_in* sockAddr, const int portNumber)
{
string host = "192.168.1.4";
sockAddr->sin_family = AF_INET;
sockAddr->sin_port = htons(portNumber);
sockAddr->sin_addr.s_addr = inet_addr(host.c_str());
}
void Sum(SOCKET socket, fd_set sockets, timeval timeout)
{
string command = "SUM 1 2 3 4 5 6\r\n";
char buffer[128];
int gSize;
if (send(socket, command.c_str(), command.size(), 0) <= 0)
cout << "Blad przesylania komedny SUM" << endl;
else
cout << command;
FD_ZERO(&sockets);
FD_SET(socket, &sockets);
command.clear();
while (select(FD_SETSIZE, &sockets, NULL, NULL, &timeout) > 0)
{
gSize = recv(socket, buffer, sizeof(buffer), 0);
if (buffer[gSize] == 10)
buffer[gSize + 1] = '\0';
else
buffer[gSize] = '\0';
command += buffer;
}
cout << command;
}
void Primes(SOCKET socket, fd_set sockets, timeval timeout)
{
string command = "PRIMES 1 10\r\n";
char buffer[128];
int gSize;
if (send(socket, command.c_str(), command.size(), 0) <= 0)
cout << "Blad przesylania komedny PRIMES" << endl;
else
cout << command;
FD_ZERO(&sockets);
FD_SET(socket, &sockets);
command.clear();
while (select(FD_SETSIZE, &sockets, NULL, NULL, &timeout) > 0)
{
gSize = recv(socket, buffer, sizeof(buffer), 0);
if (buffer[gSize] == 10)
buffer[gSize + 1] = '\0';
else
buffer[gSize] = '\0';
command += buffer;
}
cout << command;
}
void Quit(SOCKET socket, fd_set sockets, timeval timeout)
{
string command = "QUIT\r\n";
char buffer[128];
int gSize;
if (send(socket, command.c_str(), command.size(), 0) <= 0)
cout << "Blad przesylania komedny QUIT" << endl;
else
cout << command;
FD_ZERO(&sockets);
FD_SET(socket, &sockets);
command.clear();
while (select(FD_SETSIZE, &sockets, NULL, NULL, &timeout) > 0)
{
gSize = recv(socket, buffer, sizeof(buffer), 0);
if (buffer[gSize] == 10)
buffer[gSize + 1] = '\0';
else
buffer[gSize] = '\0';
command += buffer;
}
cout << command;
}
int main()
{
WSADATA wsaData;
WORD version = MAKEWORD(2, 2);
SOCKET sock = INVALID_SOCKET;
sockaddr_in sockAddr = {0};
char buffer[128];
string command;
fd_set sockets;
int gSize;
timeval timeout = {0, 100000};
if (WSAStartup(version, &wsaData) == 0)
{
if (LOBYTE(wsaData.wVersion) == 2 && HIBYTE(wsaData.wVersion) == 2)
{
cout << "Wypelniam strukture danymi ...";
MakeAddrStruct(&sockAddr, 12345);
cout << "zrobione." << endl;
cout << "Tworze gniazdo ...";
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ) == INVALID_SOCKET)
cout << "blad!!!" << endl;
else
cout << "zrobione." << endl;
cout << "Lacze z serwerem ...";
if (connect(sock, (sockaddr*)(&sockAddr), sizeof(sockAddr)) != 0)
cout << WSAGetLastError() << endl;
else
cout << "zrobione." << endl;
u_long iMode = 1;
ioctlsocket(sock, FIONBIO, &iMode);
FD_ZERO(&sockets);
FD_SET(sock, &sockets);
while (select(FD_SETSIZE, &sockets, NULL, NULL, &timeout) > 0)
{
gSize = recv(sock, buffer, sizeof(buffer), 0);
if (buffer[gSize] == 10)
buffer[gSize + 1] = '\0';
else
buffer[gSize] = '\0';
command += buffer;
}
cout << command << endl;
Sum(sock, sockets, timeout);
Primes(sock, sockets, timeout);
Quit(sock, sockets, timeout);
}
else
cout << "Zla wersja WinSock" << endl;
}
else
cout << "Nie moge zainicjalizowac WinSock" << endl;
cout << "Wylaczam wszystko ...";
closesocket(sock);
WSACleanup();
cout << "zrobione." << endl;
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment