Click to See Complete Forum and Search --> : 2 questions ...


BlackSun
December 21st, 2004, 05:29 PM
Hello

I have 2 questions now that may be impossible to answer without seeing any code, but I'll ask them anyway.

1. In my app I have a server and a client. I use the same code as I used in a small testapp, and there it all worked, but now it doesn't. The problem is that after the client is connected to the server the server doesnt seem to recieve any traffic from the client, until the client is disconnected. Then some traffic somes through ( I think it is the last message sent), is there any possibility that this is a usual newbie-mistake that has a general solution ?

2. My server is a simple while-loop that runs the whole time the app is running. Now it has started to take upp to 98% of my CPU power. Any way this is related to the communication or something involved in it ? Cause if I just make a simple app with a loop that runs it doesn't take that much power.

Hope you can make anything out of my ramble, would be much appreciated.
TIA
BlackSun

Andreas Masur
December 21st, 2004, 05:47 PM
As you guessed correctly, it would basically more than helpful to see the actual code...without it one could only guessing around...

BlackSun
December 21st, 2004, 08:20 PM
yeah I was afraid so.. was hoping that it would be some usual rookiemistake, but heres the code ...

Well, these are the serverside functions I think are relevant, .... The server is started in a thread, hence the pDlg pointer ..
Since I have to translate this before I post I hope I haven't missed too much...
Things like nPeople, and place_used I hope are selpexplanatory, and person[i].sock is just a socket in a struct with person info I've set up ..

UINT MyClass::Server(LPVOID pParam)
{
// Link to mainwin
PServerDlg* pDlg = (PServerDlg*) pParam;
// set up the socket, works fine
while(true)
{
CheckForConn(pParam);
CheckMess(pParam);
}
return 99;
}

int MyClass::CheckForConn(LPVOID pParam)
{
MyClass* pDlg = (MyClass*) pParam;
int s;
int nfds;
fd_set conn;
int cli_len= sizeof(person->my_addr);

pDlg->timeout.tv_sec=0;
pDlg->timeout.tv_usec=0;

if ( pDlg->nPeople < 5 )
{
FD_ZERO(&conn);
FD_SET(pDlg->listening_sock, &conn);
// Not completely sure about this...
nfds = pDlg->listening_sock+1;
s = select(nfds, &conn, NULL, NULL, &timeout);

if (s > 0)
{
for (int index = 0 ; index < 5; index++)
{
if (pDlg->place_used[index]== false)
{
pDlg->person[index].sock = accept(pDlg->listening_sock, (struct sockaddr *)&pDlg->person[index].my_addr, &cli_len);

pDlg->nPeople++;
pDlg->place_used[index] = true;
break;
}
}
}
}
return 1;
}

void MyClass::CheckMess(LPVOID pParam)
{

MyClass* pDlg = (MyClass*) pParam;
int s;
int nfds;
fd_set in_set, exc_set;

pDlg->timeout.tv_sec=0;
pDlg->timeout.tv_usec=0;

FD_ZERO(&in_set);
FD_ZERO(&exc_set);
nfds = 0;

for (int i = 0; i < pDlg->nPeople; i++)
{
FD_SET(pDlg->person[i].sock, &in_set);
FD_SET(pDlg->person[i].sock, &exc_set);
nfds++;
}
s = select(nfds,&in_set,NULL,&exc_set,&timeout);

if (s > 0)
{
// I never get in here ...
for (int i=0; i < pDlg->nPeople; i++)
{
if (FD_ISSET(pDlg->person[i].sock ,&exc_set))
{
// Do some error handling
}

if ((FD_ISSET(pDlg-person[i].sock, &in_set)))
{
HandleMess(i);
}
}
}
}



And then from the server I just send like this ..aöso in a thread

UINT MyClientClass::Client(LPVOID pParam)
{
MyClientClass* pDlg = (MyClientClass*) pParam;
// set up and connect, works fine
// This send gets there ok ....
send(clientsocket, mess, sizeof(mess), 0);

while (true)
{
// since this is in a thread it doesn't disturb the other functions of MyClientClass
TheClientsCheckMessage();
}
return 0;
}

// and this code I try to use to send messages ..
void MyClientClass::OnSendMess()
{
// Fix the message
send(clientsocket, mess, mess.GetLength(), 0);
}



There you have it, I hope it is understandable.