Here's the FULL code for the server:
Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
WORD sockVersion;
WSAData wsaData;
int nret;
sockVersion=MAKEWORD(1,1);
WSAStartup(sockVersion,&wsaData);
SOCKET listeningSocket;
listeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(listeningSocket==INVALID_SOCKET)
{
MessageBox(NULL,"Listening socket invalid.","Error",MB_ICONERROR);
WSACleanup();
return 0;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family=AF_INET;
serverInfo.sin_addr.s_addr=INADDR_ANY;
serverInfo.sin_port=htons(4567);
nret=bind(listeningSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
if(nret==SOCKET_ERROR)
{
MessageBox(NULL,"Error binding listening socket to port","Error",MB_ICONERROR);
WSACleanup();
return 0;
}
nret=listen(listeningSocket,2);
if(nret==SOCKET_ERROR)
{
MessageBox(NULL,"Error listening on port 4567","Error",MB_ICONERROR);
WSACleanup();
return 0;
}
SOCKET clientsock;
clientsock=accept(listeningSocket,NULL,NULL);
if(clientsock==INVALID_SOCKET)
{
MessageBox(NULL,"Error accepting connection through listening socket","Error",MB_ICONERROR);
WSACleanup();
return 0;
}
char buffer[256];
nret=recv(clientsock,buffer,256,0);
if(nret==SOCKET_ERROR)
{
MessageBox(NULL,"Error receiving data","Error",MB_ICONERROR);
WSACleanup();
return 0;
}
LPTSTR mes;
wsprintf(mes,"%d",buffer);
MessageBox(NULL,mes,"Received message",0);
closesocket(clientsock);
closesocket(listeningSocket);
WSACleanup();
return 0;
}
Sorry about the lack of comments, it's a bad habit of mine. What I do is receive the buffer and store it in another buffer the same size. Then format it using wsprintf, so it can be displayed in the MessageBox function. When it is displayed it is a number.