ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 )
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.
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'.
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.