LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2014, 07:34 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
c linux socket server locks up


Hi All,
I hope someone can advise on this. I written test code for a socket server and client. It appeared to work. So I could start the server , then start up the client send a "hello server" massage and the server would replay with "hello client". However what I've discovered is if I try to send a message twice to the server the server does not replay. If I rerun the client without stopping the server the server will always replay to the first message. So I guess it's my client end. Also if in my client code I do a reset_connection and then init_socket I can send a message. However I need to reset and init for every message from the client.

What I'm doing on the client is
init socket
write message
read reply
process reply.

and the for the server.

init server
wait for message ( server hangs here on second message )
Code:
void WaitForClientToConnect( void )
{

/*accept*/
Io_Server.read_socket = accept( Io_Server.socket, ( struct sockaddr * ) 0, 0  );

}
process message
reply



I think issue is shown in below code when it reads a reply from the server message code
Code:
rval = recv( Io_Client.socket, buff, sizeof( buff ), 0 );
if( rval < 0 )
{
	#ifdef IO_SERVER_DEBUG
	cout<<"reading stream meassage error "<<endl;
	#endif
	exit( 1 );
}
else if( rval == 0 )
{
	#ifdef IO_SERVER_DEBUG
	cout<<"client disconected"<<endl;
	#endif
}
when I read the first time I can see rval has the number of bytes sent. When I send a second message rval shows 0. My debugger still shows that socket id still exists eg in first message IO_Client.socket is set to 8 in second read it's set to 8 as well, so I'm confused.
Can anyone advise why my sock would appear to get lost? Thanks in advance.

Last edited by knobby67; 08-07-2014 at 07:37 AM.
 
Old 08-07-2014, 07:54 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In recv, rval==0 means close on the remote side (aka EOF), so the logical action is closing the socket, and go back to 'accept'. Also don't forget that in TCP there are no 'messages', only 'byte-streams'.
 
Old 08-07-2014, 08:31 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Hi Sorry to ask more,
if I understand you you're saying on the server side has closed the connection so on the client side I should close the client connection, then re init it ?
eg close
[code]
close( Io_Client.socket );
[code]
reinit
Code:
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(NULL, IO_CLIENT_PORT, &hints, &servinfo)) != 0) 
{
	#ifdef IO_SERVER_DEBUG
	fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
	#endif
	exit( 100 );
	return 1;
}


// loop through all the results and connect to tIo_Clienthe first we can
for(p = servinfo; p != NULL; p = p->ai_next) 
{
	if ((Io_Client.socket = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) 


ETC.....
is there anyway I can keep the connection on the server side open so I don't need to close re init for every group of bytes.
 
Old 08-07-2014, 09:02 AM   #4
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Solved it. server ran inside a do while loop but I with while loop == true, but for some stupid reason I'd set the loop bool loop =false so it always drops out after the first read. day wasted...
 
Old 08-07-2014, 09:23 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> is there anyway I can keep the connection on the server side open so I don't need to close re init for every group of bytes.

The socket don't close itself automagically; you get 0 from 'recv' if the remote process performs 'close' (or 'shutdown (SD_WRITE)'), or exits.
 
Old 08-07-2014, 09:27 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
I found the example code here:
http://beej.us/guide/bgnet/output/ht...entserver.html

to be very useful when I was developing a server/client pair recently.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tcp/ip socket server and client program - Redhat linux server 5(eclipse CDT) parvathi reddy Linux - Newbie 2 03-16-2014 02:51 AM
Client Server Socket TCP/IP program in C Linux over HTTPS fahad.anwar Linux - Newbie 6 05-29-2012 03:59 AM
problem in connecting Multiple clients to socket server in linux M007 Linux - Server 1 10-31-2007 11:52 PM
linux server socket:client IE or firefox athena_1 Programming 3 08-17-2006 08:57 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration