Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / XSocket 0.8.3

Author
Message
Karsten
17
Years of Service
User Offline
Joined: 24th Jan 2007
Location: N/A
Posted: 8th Sep 2008 23:06 Edited at: 8th Sep 2008 23:26
XSocket For Windows

by Karsten Pedersen

xsocket-0.8.3-win32-src.zip

About

This is a simple Object Orientated Winsock2 wrapper for windows which was created to allow easy netcode for our game to be entered in Microsoft Imagine Cup 2008.

How to use

I have included a sample usage app along with the XSocket.h/.cpp files for people who learn better by example.
For those who were looking for a step by step walkthrough... I hope this will suffice.

First make sure to...



Also make sure all this is done within a class called Logic as we will be passing the class instance to the XSocket class

Server

Create a new instance of XSocket which will do all the listening...



Notice the "this"? This allows the XSoket class to be able to call methods from the parent class (Logic). The 9999 is the port(int).
Now simply call...



This will not block your code because it simply creates a thread which keeps accepting connections and calling the connectionRecieved() method. When this gets called we need another socket to actually accept the connection. To do this I usually create an array of XSockets...



You might like to keep count of what the last socket is so you can keep adding to it. Now in the connectionRequest() method simply put...



This will create a new instance of the current XSocket and also connect it to the requesting client. If the accept is successful, the method Logic::connect() will be called.

When they disconnect, the message disconnect() will be called so you might like to reuse the XSocket.

When data is recieved by the XSocket the method Logic::dataRecieved(string data) will be called. data contains the message that was sent.

You can send a message to the client with xSockets[xSocketsCount]->xSend("hello"). To send it to all clients put it in a simple loop like...



Client

Do same as server for most things apart from...



Then the same xSend(), dataRecieved(), connected(), disconnected() methods apply.

Things to remember

All the methods like connectionRequest() need to be made even if you are working on the client. (just leave it blank)

The class which contains the XSocket class must be called Logic

#include "XSocket.h" MUST be included before windows.h or any header which included windows.h (incuding darkgdk.h). This is because windows.h includes the old winsock.h which conflicts with winsock2.h which my wrapper uses

Extra

I know this brief tutorial is almost useless, so please feel free to post any questions

Also if you would like a version to work on Linux/BSD let me know. It is also slightly more up to date.

regards,

Attachments

Login to view attachments
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 9th Sep 2008 05:38
Kick-ass.
sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 9th Sep 2008 17:15
Thanks for that

Login to post a reply

Server time is: 2024-09-30 05:36:27
Your offset time is: 2024-09-30 05:36:27