Okely dokely, I'm stumped. ... I followed that tutorial (Johnnie's winsock tut or whatever) and now I've no idea what I'm doing ...
I understand setting up the connection and closing it (this is a listening app) but how do I get data sent to this? I didn't understand what was going on in the sending recieving bit of the tutorial, it didn't explain it enough IMO ...
Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#define NET_ERR -1
#define NET_OK 0
bool CheckData ( SOCKET listen, SOCKET client ) {
return true;
}
int ReportError ( LPSTR function, int id ) {
FILE* ErrorLog = fopen ( "errors.log", "a" );
char buff[256];
sprintf ( "%s: Returned error id - %i", function, id );
fputs ( buff, ErrorLog );
fclose ( ErrorLog );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow ) {
WORD SockVer;
WSADATA WsaData;
bool alive = true;
SockVer = MAKEWORD ( 1, 1 );
WSAStartup ( SockVer, &WsaData );
SOCKET Sock;
Sock = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( Sock == INVALID_SOCKET ) {
ReportError ( "socket()", WSAGetLastError ( ) )
WSACleanup ( );
return NET_ERR;
}
SOCKADDR_IN ServerInfo;
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr.S_addr = INADDR_ANY;
ServerInfo.sin_port = htons ( 8888 );
if ( bind ( Sock, (LPSOCKADDR)&ServerInfo, sizeof ( struct sockaddr ) == SOCKET_ERROR ) {
ReportError ( "bind()", WSAGetLastError ( ) );
WSACleanup ( );
return NET_ERR;
}
if ( listen ( Sock, 10 ) == SOCKET_ERROR ) {
ReportError ( "listen()", WSAGetLastError ( ) );
WSACleanup ( );
return NET_ERR;
}
SOCKET Client;
Client = accept ( Sock, NULL, NULL );
if ( Client == INVALID_SOCKET ) {
ReportError ( "accept()", WSAGetLastError ( ) );
WSACleanup ( );
return NET_ERR;
}
while ( alive ) {
alive = CheckData ( Sock, Client );
}
}
That's what I have so far. Now basically what I want to do is while the server thingy hasn't been told to disconnect, check for a message sent. So I need to scan the stream for data I guess which I want to do in the CheckData function.
Is there a simple way to check for say an int incoming? I just want to do a switch on the number and if it's X do this if Y do that etc ...
Alternatively a better more in depth tutorial would be much better ...