Please help! accept function problems in Socket programming


 
Thread Tools Search this Thread
Top Forums Programming Please help! accept function problems in Socket programming
# 1  
Old 02-05-2009
Question Please help! accept function problems in Socket programming

Hi,

I have a client-server socket program. It has been working fine for over a year, but recently it started to show strange behavior.:confused:
After the server program runs for a while, it will show in the top command saying it is using lots of CPU, MEM. I assume it means the server code is processing some requests. But I never sent a request from the client side(from a web page). When this happens, I try to send a request to the server, the server won't response. I looked at the output from the server side, it shows the accept was called before, though I didn't send any connect calls from the client. I guess the server stopped at the read().
Parts of the program is:

void CServer_pth::run_pth(Nabor_pth *Nab, Bool_pth *Bl, BnBayes_pth *Bnb, Focus_pth *Foc, Slice_Accpth *Sac) {

int i, nsid;

pNab = Nab;
pBl = Bl;
pBnb = Bnb;
pFoc = Foc;
pSac = Sac;

//create a stream socket
// Sock sp(AF_INET, SOCK_STREAM, 0);
if (!sp.good()) {
cout<<"sp.good() failed\n";
exit(1);
}

//Bind a name to the server socket
if (sp.bind(host, port) < 0) {
cout<<"sp.bind failed\n";
sp.~Sock();
exit(2);
}

for (i=0; i<NUM_THR; i++) {
if (pthread_create(&srvtid[i], &thread_attr, commsrv_pth,tdata[i])) {
perror("pthread_create");
cout<<"srvtid[i], i="<<i<<endl;
cout<<"error in pthread_create, in commsrv_pth\n";
exit(1);
}
}

sem_init(&smp,0, NUM_THR);

for (;;) {

sem_wait(&smp);
while ((nsid = sp.accept(0, 0)) < 0) {
cout<<"accept failed\n";
exit(1);
}


bot = bot%NUM_THR;
i = mat[bot];
cout<<"i in nsid ----> "<<i<<"\tnsid ---> "<<nsid<<endl;
*tdata[i] = nsid;
bot++;

pthread_mutex_lock(&mthread);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mthread);

}
}

extern "C" void *commsrv_pth(void *argp) {

cmd_Query cmd;
int i, t_nsid;

while (1) {

pthread_mutex_lock(&mthread);
pthread_cond_wait(&cond, &mthread);
pthread_mutex_unlock(&mthread);

pthread_mutex_lock(&mbotp);
botp = botp%NUM_THR;
i = mat[botp];
t_nsid = *tdata[i];
botp++;
pthread_mutex_unlock(&mbotp);

cout<<"i in nsid ----> "<<i<<"\tnsid ---> "<<t_nsid<<endl;
cout<<"wait to read MSG from a client socket\n";

// read MSG1 from a client socket
if (sp.read((char*)&cmd, sizeof(cmd_Query), 0,t_nsid) < 0) {
cout<<"failed in read\n";
return (int*)5;
}
cout<<"after waiting to read\n";

cmd = (cmd_Query)ntohl(cmd);
dispatch_pth(cmd, t_nsid);

close(t_nsid);

pthread_mutex_lock(&mtop);
top = top%NUM_THR;
mat[top] = i;
top++;
pthread_mutex_unlock(&mtop);


sem_post(&smp);
}
return NULL;

}

The above is creating 5(NUM_THR) threads first, then wait the call from clients. I also tried another way which is createing one thread at a time when there's a call from the client. But the same strange behaviour happeded.
void CServer_pth::run_pth(Nabor_pth *pNab, Bool_pth *pBl, BnBayes_pth *pBnb, Focus_pth *pFoc, Slice_Accpth *pSac) {

int i, nsid;

//create a stream socket
Sock sp(AF_INET, SOCK_STREAM, 0);
if (!sp.good()) {
cout<<"sp.good() failed\n";
//return 1;
}

//Bind a name to the server socket
if (sp.bind(host, port) < 0) {
cout<<"sp.bind failed\n";
//return 2;
}

for (i=0; i<NUM_THR; i++) {
tdata[i] = new Thrdata_pth(sp, pNab, pBl, pBnb, pFoc, pSac, i); }

sem_init(&smp,0, NUM_THR);

for (;;) {
while ((nsid = sp.accept(0, 0)) < 0) {
cout<<"accept failed\n";
//return 1;
}

sem_wait(&smp);

bot = bot%NUM_THR;
i = mat[bot];
cout<<"i in nsid ----> "<<i<<"\tnsid ---> "<<nsid<<endl;
tdata[i]->set_nsid(nsid);
if (pthread_create(&srvtid[i], &thread_attr, commsrv_pth,tdata[i])) {
perror("pthread_create");
cout<<"srvtid[i], i="<<i<<endl;
cout<<"error in pthread_create, in commsrv_pth\n";
exit(1);
}
bot++;
}
}

extern "C" void *commsrv_pth(void *argp) {

Thrdata_pth *data = (Thrdata_pth*)argp;
cmd_Query cmd;

cout<<"waiting for reading cmd_Query from a client\n";

// read MSG1 from a client socket
if ((data->t_sp).read((char*)&cmd, sizeof(cmd_Query), 0,data->t_nsid) < 0) {
cout<<"failed in read\n";
return (int*)5;
}

cmd = (cmd_Query)ntohl(cmd);
dispatch_pth(cmd, data);

close(data->t_nsid);

pthread_mutex_lock(&mtop);
top = top%NUM_THR;
mat[top] = data->iz;
top++;
pthread_mutex_unlock(&mtop);


sem_post(&smp);
return NULL;

}

Any advice? Thanks very much for help!!!
# 2  
Old 02-07-2009
Unfortunately your code is not the easiest to understand nor have we access to all the code. However it looks like you are not destroying semaphones or releasing tdata after use. Perhaps you are running out of semaphores?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping problems socket programming in C

Sorry if I posted 2 separate questions. I'm currently doing socket programming on my current task. As you can see below in the client side. I've tried to do a loop so I will be able to get prompt for input over and over again. This is the code. do{ printf("Please your name > ");... (10 Replies)
Discussion started by: aLHaNz
10 Replies

2. Programming

socket accept() keeps looping

I'm using C/ C++ with gcc on Linux. I've a server socket where accept() is called on the socket inside a while() loop. The problem I am facing is that the first call to accept is blocking (i.e., the program waits for the first connection) but as soon as I fork afterwards (so that the child process... (2 Replies)
Discussion started by: jaywalker
2 Replies

3. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

4. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

5. IP Networking

Port number of socket returned by accept()

Hi, I typed a few tcp/ip client/server examples from a book and it works - sort of - but I noticed something strange. When I run my server I set it to use port 3001 and the client uses the same port to connect to server. They succeed, but the server prints something that doesn't really make much... (0 Replies)
Discussion started by: idelovski
0 Replies

6. Programming

connect() function in C++ socket programming

Hello All, I have a problem using connect(...) function in C++. I am using SSH from my windows system to connect it to linux server. The program works fine if I run it directly in Linux machine but I need it to run through windows machine. The function returns -1 and so my program terminates. ... (3 Replies)
Discussion started by: smdhd3
3 Replies

7. UNIX for Dummies Questions & Answers

Socket programming : Accept return 0.

I have a server and client code. My server says accept and my client says a connect. After accept when i print inet_ntoa(cin.sin_addr) by running the client on same machine i get 127.0.01 but accept returns a zero. Now when i try to do a recv from the client the recv never waits for a send. It... (7 Replies)
Discussion started by: abc.working
7 Replies

8. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

9. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies

10. Programming

Socket programming

Suppose i am writing a C program which is going to use Socket calls. I want to use a Unix port for my Socket. How can i determine a port which is not already in use? (1 Reply)
Discussion started by: Nadeem Mistry
1 Replies
Login or Register to Ask a Question