7 Replies - 1272 Views - Last Post: 27 April 2011 - 02:49 PM Rate Topic: -----

#1 Sinned   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 208
  • Joined: 13-October 10

[Linux] The program exits on thread crash

Posted 27 April 2011 - 06:12 AM

Hello everyone,

I've created a simple c++ socket server program.
When the server accept a client the server creates a new thread for the client.
But when the client falls away his thread on the server falls away either.
And then the server program falls away.

Is there a way to let the program work through, even when the thread falls down?

Thanks,

Sinned

the accept code:
	pthread_t thread1;
	while(true)
	{
		printf("waiting for new accept.\n");
		int client_socket = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
		if (client_socket < 0)
			perror("ERROR on accept");
		else{
			pthread_create( &thread1, NULL, handle, (void*) client_socket);
		}
	}


handle function:
while(true)
{
 char* c = new char[10];
 c = read(client_socket, c, 10);
 printf("from client: %s\n", c);
}


Is This A Good Question/Topic? 0
  • +

Replies To: [Linux] The program exits on thread crash

#2 GWatt   User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 06:34 AM

Have you run it in a debugger?
Was This Post Helpful? 0
  • +
  • -

#3 Sinned   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 208
  • Joined: 13-October 10

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 07:44 AM

Yes, i did.
But then the program falls down when I kill the client.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2156
  • View blog
  • Posts: 4,219
  • Joined: 30-May 10

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 09:16 AM

> printf("from client: %s\n", c);
Well this will crash since read() does not append a \0 to the result.
So printf("%s") will just roam through memory until it finds one or it crashes.

Also, why ARE you calling new each time, and not deleting it?
This is one monstrous memory leak!

Oh wait, I bet you're going to say this isn't your real code, but some facsimile representation of the program.
Was This Post Helpful? 1
  • +
  • -

#5 Sinned   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 208
  • Joined: 13-October 10

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 09:29 AM

That printf problem isn't from the "real" code.

But... that "memory leak" is real... I used to use Java, and Java is doing that for me, so I forget it sometimes in C++.

So Java has:
try{ /*code*/ }catch(Throwable t){}

for catching the whole error and going on.

But try-catch won't work in C++ for all errors.

Now I don't know how to catch/deny an error of a thread.

This post has been edited by Sinned: 27 April 2011 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 09:45 AM

Something like this:
// thread start
char buffer[4096] = { 0 };

ssize_t bytes_read = read(client_socket, buffer, sizeof(buffer) - 1);
while (bytes_read >= 0)
{
    buffer[bytes_read] = 0;
    printf("from client: %s\n", buffer);
    bytes_read = read(client_socket, buffer, sizeof(buffer) - 1);
}

if (bytes_read < 0)
{
    printf("Failed reading from socket\n");
}

// thread end


This post has been edited by JackOfAllTrades: 27 April 2011 - 02:49 PM
Reason for edit:: Fixed for posterity, thanks to Salem_c

Was This Post Helpful? 0
  • +
  • -

#7 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2156
  • View blog
  • Posts: 4,219
  • Joined: 30-May 10

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 02:43 PM

> ssize_t bytes_read = read(client_socket, buffer, sizeof(buffer));
Even this should be
ssize_t bytes_read = read(client_socket, buffer, sizeof(buffer)-1);
otherwise the attempt to append a \0 results in a buffer overflow, should read() actually fill the buffer completely.
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: [Linux] The program exits on thread crash

Posted 27 April 2011 - 02:49 PM

Damn, you're right. I get that and fgets mixed up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1