Thread: Server fails while posting to socket

  1. #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.

  2. #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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #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;
            }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (*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"?

Popular pages Recent additions subscribe to a feed

Similar Threads

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