MyNetwork: Easy to Use Linux Network Sockets

MyNetwork Library

Download Here

This is based off the previous easySocket library. The MyNetwork Library is an easy to use c++ wrapper for creating tcp based servers within a linux enviroment. It is a very bare bones library allowing for simple yet efficent way to handle multiple clients. The example below is all that is needed to start a server on a machine.

STARTING SERVER

#include "MyNetwork.h"
#include "MyNetwork.cpp"
#include <iostream>
#include <string>
#include <cstring>


using namespace std;

//start the server listening on port 5454
MyNetwork net("5454");


void serverFunction(int a)
{
  

}

int main()
{
	//start the main server loop
    net.start(serverFunction);
    
    return 0;

}

The net object is initialized on port 5454. At the entry point of the program the start() method is called. This starts the listening socket which then forks the process once a connection takes place. The connection is then handled by the serverFunction() method. Returning from this function will disconnect the client and kill the forked process while leaving the listening socket functioning as normal. This will be discussed more in the next example.

BASIC CLIENT INTERACTION

Below is an example of some basic client interaction. We will be using two forms of the sendPacket() method. One form accepts a buffer, where the other accepts const char strings. For receiving information we will be using the readPacket() which will be saved into a string object.

#include "MyNetwork.h"
#include "MyNetwork.cpp"
#include <iostream>
#include <string>
#include <cstring>


using namespace std;

//start the server listening on port 5454
MyNetwork net("5454");


void serverFunction(int a)
{
  	//these are used to store some variables we take in from the user
	std::string response, buff;
	
	//this is sent to the connected client
	net.sendPacket("Hello, What's your name?"); 	
	response = net.readPacket();
	
	
	//here we will send a buffer to a client
	buff= "your name is "+ response;
	net.sendPacket(&buff, buff.size());
	
}

int main()
{
    //start the main server loop
    net.start(serverFunction);
    
    return 0;

}

COMMAND HANDLING

To further the interaction we can turn this server into something of a bot by listening for commands on the incomming stream of information. Thus allowing you to form a very crude shell, using fork() and exec(). Maybe a crude google search tool with some wget hackery. For the following example we will just send responses back to the client using the net.sendPacket() method. There are a few ways to do this. The cleanest is to create a function that handles the clients input passing the incoming buffer as an argument. It may look something like this…


#include "MyNetwork.h"
#include "MyNetwork.cpp"
#include <iostream>
#include <string>
#include <cstring>


using namespace std;

//start the server listening on port 5454
MyNetwork net("5454");


int handleCommands(std::string * data_)
{
	std::string data = *data_;
	
	//if one of the lines contains our search string
		if(data.find("hello") != std::string::npos)
		{
			net.sendPacket("Why hello how are you?\n");			
		}
		if((data.find("im good")!= std::string::npos) ||(data.find("good")!=std::string::npos))
		{
			net.sendPacket("I'm glad to hear that, anything new?\n");
		}
		if(data.find("quit") != std::string::npos)
		{
			return -1;	//return <0 to kill the connection
		}			
	
	return 0;
}




void serverFunction(int a)
{

    //send a greeting
    net.sendPacket("Hell0o0o there");


  //this will loop forever and print what is comming in on the server side terminal
	while(1)
	{
	std::string incoming = net.readPacket();	
	cout<<incoming;	
	
		
		//use the function above for handling the commands
		if(handleCommands(&incoming)<0)
		{
			return;//this return will only disconnect the current connection being handled and not affect the listener
		}
	
	} 

 
	
}

int main()
{
    //start the main server loop
    net.start(serverFunction);
    
    return 0;

}