![]() |
| | #1 |
| Registered User Join Date: Dec 2005
Posts: 3
| Server fails while posting to socket 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);
}
}
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);
}
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);
}
}
}
};
Hope to get an answer to my issue Thank you! Last edited by synergy; 12-21-2005 at 12:10 PM. |
| synergy is offline | |
| | #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 | |
| | #3 |
| and the hat of Jobseeking 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 | |
| | #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. Code: aleks@avalanche:~/devel/trunk$ ./QuizClient localhost 6666 Connected localhost> localhost> localhost> data localhost> Unable to read aleks@avalanche:~/devel/trunk$ 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 | |
| | #5 |
| and the hat of Jobseeking 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
> 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |