Tutorwinwsock
Tutorwinwsock
This is very simple version to add multiple connection support to your server project. Here we are going to
use ioctlsocket to make it non blocking and small struct table for saving the client sockets. You should have
some sort of experience with winsockets or at least knowing how recv and send functions work. If you are
interested for getting to know winsockets better then you should head to microsoft developer pages, cause
pretty much everything else on the web is garbage and they seem to keep example codes updated on msdn
pages. Also you should know that the non blocking version is very cpu hungry cause of continuous polling
but with little tweak we can make it work nicely, especially with something small. Asynchronous Sockets are
for something bigger and stable, they kinda take non blocking sockets to next level. Hope this helps you to
find I/O strategy you are looking for.
This is the top of our code and almost all variables are there. And don't forget to link ws2_32 library.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <winsock2.h>
bool connected;
SOCKET ss;
};
_clients_b clients[MAX_CLIENTS];
//function declarations
void start_server();
In this function we are setting up the server and i decided to put it all in one function, to make it
more easier to read.
So basically start_server function is made of:
WSAStartup
socket
setsockopt
bind
listen
ioctlsocket
int main() {
//we might need to add some delays cause our code might be too fast
//commenting this function will eat your cpu like the hungriest dog
ever
//plus we don't need to loop that fast anyways
Sleep(1);
memset(&recvbuf, 0, sizeof(recvbuf));
if (clients[cc].connected) {
//receive data
receiveres = recv(clients[cc].ss, recvbuf,
DEFAULT_BUFLEN, 0);
Sleep(10);
// Clean up winsock
WSACleanup();
return 0;
}
void start_server() {
int wsaresult, i = 1;
WSADATA wsaData;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(DEFAULT_PORT);
// Initialize Winsock
wsaresult = WSAStartup(MAKEWORD(2, 2), &wsaData);
//if error
if (wsaresult != 0) {
printf("WSAStartup failed with error: %d\n", wsaresult);
}
if (server_socket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
}
//Binding part
wsaresult = bind(server_socket, (sockaddr*)&server, sizeof(server));
if (wsaresult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
}
unsigned long b = 1;
if (wsaresult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
}
Client Code
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
//return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
//return 1;
}
int loopthis = 1;
while (loopthis == 1) {
cout << "Type to say: ";
getline(cin, sendbuf2);
cout << endl;
if (strcmp(sendbuf2.c_str(), "break") == 0) {
cout << "exiting...\n";
loopthis = 0;
break;
}
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
int ok;
cin >> ok;
return 0;
}