C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-21-2005, 11:56 AM   #1
Registered User
 
Join Date: Dec 2005
Posts: 3
Server fails while posting to socket

Hello All!

I am trying to implement a multithreaded chat/quiz server using posix threads. The server is intended to run under UNIX platform

I am expecting a problem, while client tries to write data into the socket. Client part goes down immediately, however the server continues its work being in active loop and serving client requests.
All files are compiled with GNU C++ compiler and it seems the compilation phase goes well. At least, i can't notice any error.

I tend to think, that the problem occurs in this piece of code...

Code:
try {
                while ((recvMsgSize = (*sock >> echoBuffer)) > 0) {
                        cout << client << ": " << echoBuffer << endl;
                        game->Receive(sock, echoBuffer);
                }
        }
...because, if i comment this line out "game->Receive(sock, echoBuffer);" the problem dissapears. Consequently, the error may hide in sendAll which is called out by Receive().

Server code

Code:
...

#include "QuizGameEngine.h" 

...

//
// Client handling
//
void HandleClient(TCPSocket *sock) {
  // Add client connection to the list

  pthread_mutex_lock(&mutx);
  c_connections.push_back(sock);
  pthread_mutex_unlock(&mutx);

  cout << "Handling client ";

  try { 
    cout << sock->getForeignAddress() << ":";
  }
  catch (SocketException) {
    cerr << "Unable to get foreign address" << endl;
  }
  try { 
    cout << sock->getForeignPort();
  }
  catch (SocketException) {
    cerr << "Unable to get foreign port" << endl;
  }

  pthread_t client;
  cout << " with thread " << client << endl;

  string echoBuffer;
  int recvMsgSize;

	try {
		while ((recvMsgSize = (*sock >> echoBuffer)) > 0) {
			cout << client << ": " << echoBuffer << endl;
			game->Receive(sock, echoBuffer);
		}
	}
	catch (SocketException) {
		cout << client << " has left the server." << endl;
	}

  // Remove client connection and Player from the list
  pthread_mutex_lock(&mutx);
  game->RemovePlayer(sock);
  c_connections.remove(sock);
  pthread_mutex_unlock(&mutx);
}
GameEngine -- header file, containing all the functions to interact wit server part


Code:
...

class QuizGameEngine {

...

	void Receive(TCPSocket *sock, string input) {

	  //ParseInput(sock, input);

	  SendAll(input);
	}


		/* First word is not a command, execute input */

		//ExecuteInput(sock, input);

		SendAll(input);

	}


	void SendTo(TCPSocket *sockIt, string msg) {

		*sockIt << msg;

	}



	void SendAll(string msg) {

		for (socket_list::iterator

				sockIt  = sockets->begin();

				sockIt != sockets->end();

				sockIt++) {



			SendTo((TCPSocket *)&sockIt, msg);

		}

	}



	void SendAllExcept(TCPSocket *sockNot, string msg) {

		for (socket_list::iterator

				sockIt  = sockets->begin();

                sockIt != sockets->end();

				sockIt++) {



			if ((TCPSocket *)&sockIt != sockNot) {

				SendTo((TCPSocket *)&sockIt, msg);

			}

		}

	}



};
I also don't think problem may be in client, it's rather in a sending procedure. Additionally, client part of code, does not have a deal with a GameEngine

Hope to get an answer to my issue

Thank you!

Last edited by synergy; 12-21-2005 at 12:10 PM.
synergy is offline   Reply With Quote
Old 12-21-2005, 12:07 PM   #2
Registered User
 
Join Date: Dec 2005
Posts: 3
Sorry for posting to the wrong section
Moderators, could you please move this topic into networking?

thank you
synergy is offline   Reply With Quote
Old 12-21-2005, 12:25 PM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
Well "an error" really helps to narrow down the problem
How about posting some useful information - like does it segfault for example?
Or maybe you catch one of your exceptions.
Salem is offline   Reply With Quote
Old 12-21-2005, 02:03 PM   #4
Registered User
 
Join Date: Dec 2005
Posts: 3
Well, sure..but it does not provide me with any hints about the point of failure :-(

Starting server
Code:
aleks@avalanche:~/devel/trunk$ ./QuizServer
Bind 6666 for listening..

Handling client 127.0.0.1:54322 with thread 3083426736
3083426736: ls
3083426736 has left the server.
Client communication
Code:
aleks@avalanche:~/devel/trunk$ ./QuizClient localhost 6666
Connected
localhost>
localhost>
localhost> data
localhost> Unable to read
aleks@avalanche:~/devel/trunk$
As you can see, nothing is returned..therefore i am really worried about how to figure it out, where is the damned error occurs.
Also i have never had a deal with gdb :-( and i think, it would made the problem more complicated in case i post the memory map

by the way, this piece of code is responsible for the message, returned by cerr << Unable to read

Code:
while (1) {
                if ((*sock >> echoBuffer) <= 0) {
                        cerr << "Unable to read";
                        exit(1);
                }
                cout << echoBuffer;
        }
synergy is offline   Reply With Quote
Old 12-21-2005, 05:39 PM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
> (*sock >> echoBuffer) <= 0
Unless you know something I don't, I was under the general impression that the >> and << stream operators returned a reference to a stream, not the number of characters processed.

Somewhat oddly, you're also passing pointers to streams rather than references to streams.
Or is TCPSocket something completely different and really isn't a stream at all, but you've just overloaded the operators to make it look like it is?

So all you're really doing here is comparing what is essentially an address with 0 (or NULL).

Here's an example
Code:
$ cat foo.cpp && ./a.out
#include <iostream>
using namespace std;
int main ( ) {
  cout << (void*)cin << endl;
  return 0;
}
0x8049988
Perhaps your objects are stored in the 0x8xxxxxxx and upwards addresses, which would make them appear negative to your comparison?

> pthread_t client;
> cout << " with thread " << client << endl;
Is this really just an uninitialised variable?

> catch (SocketException)
> cout << client << " has left the server." << endl;
How about outputting the exception itself?
Does it contain any information apart from "there's a problem"?
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Server carrotcake1029 Windows Programming 2 07-21-2008 11:46 AM
Server Architecture coder8137 Networking/Device Communication 2 01-29-2008 11:21 PM
Where's the EPIPE signal? marc.andrysco Networking/Device Communication 0 12-23-2006 08:04 PM
socket newbie, losing a few chars from server to client registering Linux Programming 2 06-07-2003 11:48 AM
socket, udp,Halflife Server Tolpan C Programming 2 06-21-2002 09:02 AM


All times are GMT -6. The time now is 05:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22