fWINSOCK


Quick Data

Header file:f3cwinsock.h
Object name:fWINSOCK
Object index:1 of 1 Object

General Description

The fWINSOCK class is a wrapper for the Windows Sockets functions. This class will allow you to create a TCP/IP server and also to connect to a TCP/IP server. This can be used on the internet, if you are so inclined or connected.

Release one said this: This class does not work! I have not been able to get it to set up a sever yet - so maybe the client connection section works, but I am not sure. If you want to, just fiddle with the server part, and see if you can get it to work.

Release 1.01 said: This class now works. Thanks to Brian C. Becker <bcbeckerz@yahoo.com> you can use this class, and it works well. Thankyou, Brian!

Release 1.03 said: This header file changed it's name to f3cwinsock.h after problems with the original winsock.h that's part of the Windows API. Sorry for any problems!

About the last thing that needs to be mentioned is about what version is used. This class uses Winsock 1.1 - which most computers running Windows have nowadays.


Methods


Prototype Description

void Setup(HWND Handle, bool StoreSocket, int BufferSize); This function doesn't actually set anything up, it just gives the class some information relating to how it should setup things when required. You should call this before you do anything else. Handle is the handle of the window to send socket events to. If StoreSocket is TRUE, then the class will store an incoming socket as well as giving you the socket handle - which means that you can ignore the handle, as the class will remember it. If you pass FALSE for StoreSocket, then you will need to remember the socket values as clients connect. The idea behind this is that if you only are going to have one client, you can make the class remember for you, or if you are going to have more than one client, you can set up various other methods of looking after the socket values. BufferSize is the size of the receive buffer - typically this is 2048 bytes, but you can change this.
bool SetupServer(int Port); This function sets up a TCP/IP server on all available local addresses (ie. your LAN address, as well as an internet address, if you are connected) on the specified port. This function will return TRUE if successful, or FALSE otherwise.
SOCKET AcceptConnection(void); This function accepts an incoming connection. You can intercept the connection with one of the events. The value returned is the socket identifier for that connection. If you specified that you wanted the class to remember the socket - then it will store that away, and you can ignore the return value of this function. However, remembering the socket will only work for up to one client.
void KickOff(SOCKET Socket); This function kicks the specfied client off. Nice, simple, and instant.
void ShutdownServer(void); This function shuts down the server which you previously started.
bool Connect(char* IP, int Port); This function attempts to connect to a listening TCP/IP server. IP is the IP address of the server, or a fully qualified domain name. Port is the port to connect on. For example, IP could be "192.168.0.1" or "localhost", or even "www.yahoo.com". This function returns TRUE if successful, or FALSE otherwise.
void CloseClientConnection(void); This function forces the class to close down the connection to the server. It does this nicely, of course.
char* Read(SOCKET Socket); This function reads data from the specified socket. The data returned is binary data, and the buffer will be the size of BufferSize, which is by default 2048 bytes.
void Write(SOCKET SendTo, char* Data, int Size); This function writes data out to a socket. SendTo is the socket to send the data to, Data is a pointer to the data to send, and Size is the amount of data to send.

Events

Please note that the events listed here are done in pairs; one as a property, and the other as a function. The property is assigned a function name, ie. Object.OnClick = &FunctionName, whilst the function is called to verify that an event has occured. Please see the chapter on event handling for information on how to use these correctly.

The Socket parameter passed along with each event is the socket that is sending, receiving, or closing, as applicable. Exactly what it does for the OnConnection event... I'm not sure.


Prototype Description Sent under

void (*OnIncomingConnection)(fWINSOCK* Sender, SOCKET Socket); This event occurs when there is a connection waiting to occur. You can accept the connection with AcceptConnection() function of fWINSOCK. Really only useful to servers. WM_NET
void (*OnConnection)(fWINSOCK* Sender, SOCKET Socket); This event occurs after a connection has been established. Only useful to servers. WM_NET
void (*OnRead)(fWINSOCK* Sender, SOCKET Socket); This event occurs when there is incoming data. WM_NET
void (*OnWrite)(fWINSOCK* Sender, SOCKET Socket); This event occurs when there is outgoing data. WM_NET
void (*OnClose)(fWINSOCK* Sender, SOCKET Socket); This event occurs when the connection to a server or a connection from a client has closed. WM_NET

Sample Code

The following snippet gives a basic idea of how to use this component.


//Create instances of appropriate classes...
fWINSOCK Server;
fWINSOCK Client;

//Setup some basic things...
Server.Setup(Window.GetHandle(), TRUE, 2048);
Client.Setup(Window.GetHandle(), TRUE, 2048);

//Set up the server...
if (!Server.SetupServer(5557))
   {
   MessageBox(NULL, "Could not setup server!!", "NULL", 0);
   }

//Now get client to attach to server...
//(Note that 127.0.0.1 is the loopback - it means the machine
// that you are using).
if (!Client.Connect("127.0.0.1", 5557))
   {
   MessageBox(NULL, "Could not connect to server!!", "NULL", 0);
   }

Back to indexThe FreeFoote Foundation Classes Documentation