Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-21-2005, 12:31 AM   #1 (permalink)
newdarkness
Registered User
 
Join Date: Jun 2005
Posts: 4
newdarkness is on a distinguished road
C++ multithreaded server

Hello, I just started C++ and I need a multitthreaded server which broadcasts all messages by clients to every other client.

The piece of crap I came up with looks like this - and shows lots of errors, so I'm pretty sure its totally wrong - i just dont know Whats wrong.

Code:
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <iostream>

int main ( int argc, int argv[] )
{

  std::cout << "running....\n";

  try
    {
      while ( true )
	{

	  connection::open();

	  try
	    {
	      while ( true )
		{
		  std::string data;
		  new_sock >> data;
		  broadcast(data);
		}
	    }
	  catch ( SocketException& ) {}

	}
    }
  catch ( SocketException& e )
    {
    std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
    }

  return 0;
};

struct connection
{
ServerSocket server(3000);
ServerSocket socket[100];
bool i[100];


int reserve()
	{
	for(int iid=0;iid < 100;iid++)
		{
		if(!i[iid])
			{
			i[iid] = true;
			return iid;
			}
		}
	};

bool broadcast(std::string data)
	{
	for(bool iid=0;iid < 100;iid++)
		{
		if(i[iid])
			{
			socket[iid] << data;
			}
		}
	return true;
	};


bool open()
	{
	int iid=reserve();
	server.accept (socket[iid]);

	try
		{
		while ( true )
			{
			std::string data;
			socket[iid] >> data;
			broadcast(data);
			}
 	   	}
		catch ( SocketException& ) {}

	release(iid);
	return true;
	};

bool release(int iid)
	{
	i[iid] = false;
	return true;
	};
};
newdarkness is offline   Reply With Quote
Old 06-21-2005, 01:23 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
Code:
...
struct connection
{
    ServerSocket ...
Don't you mean
Code:
...
class connection
{
    ServerSocket ...
Code:
...
connection::open();
...
This is not the proper way of doing an open() call to a connection.
Code:
connection connect(3000);
// or
connection connect();
connect->open();
// or 
connect.open();
might be a better aproach, depending on, how you want to dereference your classes.
And in order to have it multithreaded, you need to use some sort of threadding.. One capturing the initial request, which spawns a child process, per clientrequest, doing all the communication with the caller and broadcasting to the clients.
So your codepart
Code:
...
while ( true )
{
    std::string data;
    new_sock >> data;
    spawn_child_broadcast(data);
...
And among others theres alot of syntactical errors, for instance
Code:
...
for(bool iid=0;iid < 100;iid++)
You can't store a value in a bool variabel, it either is true (1, 2, 3, 4, 5 ....) or false (0), it dosn't have a storage facility where you can do calculations on it, for that theres int, double, float, etc.

And why do you do an infinite loop in your open() function, when your main function want's to do one aswell..
Code:
...
ServerSocket server(3000);
...
server.accept (socket[iid]);
...
What does your ServerSocket class look like ??
What book are you learning this from ?
I see alot of loop through alot of sockets, but basicaly, your code isn't much of a multithreaddet solution.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-21-2005, 03:16 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
For broadcasting i don't think a multitthreaded server is a good way.
For example 2 people broadcast a message and the server opens 2 threads to send to the clients. Then they could cought up by both sending a message to 1 person and the client will choke or shows 2 popups rapidly fast.

In my opinion the server should have a thread safe queue list for all recieved message.
(recieve message multithreaded, but don't send them at the same time)
And another process/timer sends the queued messages all at once or one at a time.
If it's used on high traffic networks you should send all messages at once to the client.
DJMaze is offline   Reply With Quote
Old 06-22-2005, 01:28 AM   #4 (permalink)
newdarkness
Registered User
 
Join Date: Jun 2005
Posts: 4
newdarkness is on a distinguished road
@redhead
I dont really have a book, what I tried to do is to have two loops, one for new connections and one for the connection state itself. - I probably screwed it all up anyways ...
@DJMaze:
I want a chat server, where everyone can send a message which is then broadcasted to every client.
newdarkness is offline   Reply With Quote
Old 06-22-2005, 04:44 AM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
If you want a chat server maybe check-out some ircd's their sources ?
http://directory.google.com/Top/Comp...vers/Chat/IRC/
DJMaze is offline   Reply With Quote
Old 06-23-2005, 12:59 AM   #6 (permalink)
newdarkness
Registered User
 
Join Date: Jun 2005
Posts: 4
newdarkness is on a distinguished road
I just want it really simple - client connects, each message ist broadcasted to all connected clients..
newdarkness is offline   Reply With Quote
Old 06-23-2005, 05:34 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Lots of errors newdarkness. I didn't bother to run it.

My proposal is to move up with your C++ skills without sockets first. For example you have to forward declare classes or structs before you can use them the way you do.

After practicing with C++ go to:
http://www.linuxgazette.com/issue74/tougher.html
For a decent intro.
__________________
Valmont is offline   Reply With Quote
Old 06-23-2005, 08:13 AM   #8 (permalink)
newdarkness
Registered User
 
Join Date: Jun 2005
Posts: 4
newdarkness is on a distinguished road
Lol - for some reason the code there is exactly the one I started with- yeah, probably I gotta learn some more C++ before I get it to run multithreaded
newdarkness is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 01:16 PM
Terminal Server Help Riverdome Windows 5 12-06-2004 02:22 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 04:47 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting