Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

Reading Socket Messages in the Windows Proc

This topic is 2524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the code:

Called Every Frame:(MainLoop.cpp)

void MainLoop()
{
while (PeekMessage(&msg,APP.hWnd,0,0,PM_REMOVE)>0)// GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}

Game.ServerSendData--;
if(Game.ServerSendData<=0)
{
if(Server.Host || APP.Connected)
{
send(Server.MySocket,"lol",strlen("lol"),0);
APP.ServerLog.Write("Sent Ntwk Message");
}
else if(!Server.Host && !APP.Connected)
{
Server.Connect("127.0.0.1",1234);
APP.ServerLog.Write("Sent Request to Connect");
}
Game.ServerSendData=2000;
}
}


Windows Proc:(WndProc.cpp)

#define MY_HOST_MESSAGE 1224
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case MY_HOST_MESSAGE:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_CONNECT:
{
switch(WSAGETSELECTERROR(lParam))
{
case WSAECONNREFUSED:APP.ServerLog.Write("Connection Was Rejected (Hah!)");break;
case WSAETIMEDOUT:APP.ServerLog.Write("Connection Timed Out (Hah!)");break;
}
APP.ServerLog.Write("New Request");
Server.Connected=true;
APP.Connected=true;
}break;
case FD_ACCEPT://connection request made
{
switch(WSAGETSELECTERROR(lParam))
{
case WSAECONNREFUSED:APP.ServerLog.Write("Connection Was Rejected (Hah!)");break;
case WSAETIMEDOUT:APP.ServerLog.Write("Connection Timed Out (Hah!)");break;
}
MessageBox(NULL,"Got New Request","Network",MB_OK|MB_ICONEXCLAMATION);
APP.ServerLog.Write("Connection Was Successful");
Server.Connected=true;
APP.Connected=true;
}break;
case FD_READ://incoming data to read
{
char buffer[80];
//memset(buffer, 0, sizeof(buffer));
//recv(Server.MySocket, buffer, 80-1, 0);
int i = recv(Server.MySocket, buffer, 80, 0 );
buffer = '\0';
MessageBox(NULL,buffer,"Network Message",MB_OK|MB_ICONEXCLAMATION);
APP.ServerLog.Write(buffer);
}break;
case FD_CLOSE://lost connection
{
MessageBox(NULL,"Lost Connection","Network",MB_OK|MB_ICONEXCLAMATION);
APP.ServerLog.Write("Lost Connection");
}break;
}
}break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


And here Is the server stuff:(ServerStats.h)

#include <string>
#include <vector>
#include <WinSock2.h>
#include <windows.h>
class ServerStats
{
public:
bool Host,Connected;
std::vector<std::string> ConnectedIPs;
SOCKET MySocket;
WSADATA nData;
SOCKADDR_IN Target;//Socket address information
std::string DefaultIP;
void Init(bool nHost);
void Quit();
void Connect(std::string nHost,int Port);
void Listen(int PortNO);
};


ServerStats.cpp:

#include "ServerStats.h"
#include <winsock.h>
#include "App.h"
extern myApp APP;
#define MY_HOST_MESSAGE 1224
//htonl()-> host to visitor (long)
//ntohl()-> visitor to host (long)
//htons()-> host to visitor (short)
//ntohs()-> visitor to host (short)

void ServerStats::Init(bool nHost)
{
unsigned int nErr=WSAStartup(MAKEWORD(2,2),&this->nData);
if(nErr!=0)
{
std::stringstream nError;nError<<"WSA Startup Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
ServerStats::DefaultIP="127.0.0.1";//"5.14.221.173";
ServerStats::Host=nHost;
ServerStats::MySocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"Socket Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
ServerStats::Connected=false;
}
void ServerStats::Quit()
{
if(ServerStats::MySocket){closesocket(ServerStats::MySocket);}
WSACleanup();
}
void ServerStats::Connect(std::string nHost,int Port)
{
if(!ServerStats::Host && !ServerStats::Connected)
{
ServerStats::Target.sin_family=AF_INET;
ServerStats::Target.sin_port=htons(Port);
ServerStats::Target.sin_addr.s_addr=inet_addr(nHost.c_str());
connect(ServerStats::MySocket,(SOCKADDR*)&Target,sizeof(ServerStats::Target));
//connect()
unsigned int nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"Connect Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
WSAAsyncSelect(ServerStats::MySocket,APP.hWnd,MY_HOST_MESSAGE,FD_ACCEPT | FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE);
nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"WSA SyncSelect : "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
}
else if(ServerStats::Host)
{
APP.ServerLog.Write("Tried To Connect When A Host");
}
else
{
APP.ServerLog.Write("Tried To Connect When Already Connected");
}
}
void ServerStats::Listen(int PortNO)
{
if(ServerStats::Host)
{
ServerStats::Target.sin_family=AF_INET;
ServerStats::Target.sin_port=htons(PortNO);//assign port to this socket
ServerStats::Target.sin_addr.s_addr=inet_addr(ServerStats::DefaultIP.c_str());
bind(ServerStats::MySocket,(LPSOCKADDR)&this->Target,sizeof(ServerStats::Target));
unsigned int nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"Bind Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
listen(this->MySocket,SOMAXCONN);
nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"Listen Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
WSAAsyncSelect(ServerStats::MySocket,APP.hWnd,MY_HOST_MESSAGE,FD_ACCEPT | FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE);
nErr=WSAGetLastError();
if(nErr!=0)
{
std::stringstream nError;nError<<"WSA SyncSelect Error: "<<nErr;
APP.ServerLog.Write((nError.str()).c_str());
}
}
else
{
APP.ServerLog.Write("Tried To Listen When NOT A Host");
}
}


So yesterday i tried acually testing online with my friend (i was the host, he was the client), and i recieved the FD_ACCEPT message... but i dont know why i cant get messages, and how to find the clients ip when they try to join... what are the solutions to this problem?

Share this post


Link to post
Share on other sites
Advertisement
Is there a particular reason why you're using WSA* functions (which comprise a notoriously flaky and difficult-to-use API) instead of more direct socket calls, or better yet, a networking library?

Share this post


Link to post
Share on other sites
XD I just saw it on a tutorial, im using win32 and dont want to make any new threads... is there something i should be doing different? i thought all the other ones were blocking functions... Im also trying to make it so there is 1 host, and many other (around 10 or so) clients.

Share this post


Link to post
Share on other sites
Does this even work on a LAN?

On a server, you'll get an accept event, after which you need to actually accept the connection on the socket, which creates a new socket that you actually read/write on -- the listening socket is just there to accept new incoming connections.

You don't need to get the client's IP address at all. If the client connects to you, you can talk to it through that connection.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!