473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Server/Client infinite loop problem

I'm testing simple server/client codes on linux. just server can wait
for client's connect sign and accept, and client can't connect to
server, this is all.
There's no problems just for this objects.

I'm testing for the situation.. after the connection between server
and client is done, if server or client hits the "ctrl+c" key.

The problem is either case for server or for client hits the "ctrl
+c"key, they goes infinite loop. I thought select() will be blocked
and there'll be no more looping.

and I thought if there's possible error in the select() after getting
"ctrl+c" key, and tried to catch it, but "errno" printing code just
displayed "0" for varible errno :(

I wanna know what I should fix not to be looping after getting "ctrl
+c"key from server/client.

codes below..................................
///////server.c/////usage : server [10000 or any huge port #]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>

void errquit( char *mesg )
{
perror(mesg);
exit(1);
}

char* START_STRING = "Connected to server...\n";

int main( int argc, char* argv[] )
{
if( argc != 2)
{
printf("usage : %s port\n", argv[0]);
exit(0);
}

//creating connection socket
int sd, listen_sock, accp_sock;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd == -1 )
errquit("socket fail");

//init servaddr
struct sockaddr_in servaddr;
bzero((char*)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY);
servaddr.sin_port = htons( atoi(argv[1]) );

//bind
if( bind( sd, (struct sockaddr*)&servaddr, sizeof(servaddr) ) < 0 )
errquit("bind fail");

//lisetn
listen(sd, 2);
listen_sock = sd;

//setting read_fds and select()
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(listen_sock, &read_fds);
int maxfdp1;
maxfdp1 = listen_sock+1;
puts("waiting for client");
if( select(maxfdp1, &read_fds, NULL, NULL, NULL ) < 0 )
errquit("select fail on listen");

//bind
struct sockaddr_in cliaddr;
int addrlen;
addrlen = sizeof(struct sockaddr_in);
if( FD_ISSET( listen_sock, &read_fds ))
{
accp_sock = accept( listen_sock, (struct sockaddr*)&cliaddr,
&addrlen );
if( accp_sock == -1 )
errquit("accept fail");
send( accp_sock, START_STRING, strlen(START_STRING), 0 );
puts("Client connected...");
}

//select()
FD_ZERO(&read_fds);
FD_SET(accp_sock, &read_fds);
maxfdp1 = accp_sock+1;
if( select(maxfdp1, &read_fds, NULL, NULL, NULL ) < 0 )
errquit("select fail on before read");

//checking errno for select()#1
printf("errno after select#1: %d\n", errno);
if( errno == ECONNRESET )
printf("select err#1\n");
while(1)
{
FD_ZERO(&read_fds);
FD_SET(accp_sock, &read_fds);
maxfdp1 = accp_sock + 1;

if( select(maxfdp1, &read_fds, NULL, NULL, NULL ) < 0 )
errquit("select fail on before read");

//checking errno for select()#2
printf("errno after select#2: %d\n", errno);
if( errno == ECONNRESET )
printf("select err#2\n");

if( FD_ISSET( accp_sock, &read_fds ) )
{
//read()
int nbyte;
char buf[100];
nbyte = recv( accp_sock, buf, 99, 0 );

//checking errno for read()
printf("errno after read: %d\n", errno);
if( errno == ECONNRESET )
printf("read err\n");
}
}

return 0;
}
///////////////////////////////// end of server.c


////////client.c////usage : client [server IP] [server port]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>

#define MAXLINE 1000

int main( int argc, char* argv[] )
{
if( argc != 3 )
{
printf("usage : %s server_ip port \n", argv[0]);
exit(0);
}

//socket()
int s;
if( (s = socket( PF_INET, SOCK_STREAM, 0 )) < 0 )
return -1;

//init servaddr
struct sockaddr_in servaddr;
bzero( (char*)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton( AF_INET, argv[1], &servaddr.sin_addr );
servaddr.sin_port = htons( atoi(argv[2]) );

//connect()
if( connect( s, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0 )
return -1;
else
puts("Connected to server...");
//loop, select, read
int maxfdp1, count=0;
char* buf[MAXLINE];
maxfdp1 = s+1;
fd_set read_fds;

FD_ZERO( &read_fds );
while(1)
{
printf("%d ", count);
count++;
//setting fd_set
FD_ZERO( &read_fds );
FD_SET( s, &read_fds );

//casting select()
if( select(maxfdp1, &read_fds, NULL, NULL, NULL ) < 0 )
{
perror("select fail");
exit(1);
}
//if message arrived from server
if( FD_ISSET( s, &read_fds ))
{
int nbyte;
nbyte = recv(s, buf, MAXLINE, 0);
if( errno != 0 )
perror("error after recv");

if( (nbyte 0 ) )
{
buf[nbyte] = 0;
printf( "%s \n", buf );

}
}
}

return 0;
}

///////////////end of client.c///


Oct 24 '08 #1
3 7157
On Oct 24, 2:18*pm, Hukkky <pinkbu...@nate.comwrote:
I'm testing simple server/client codes on linux. just server can wait
for client's connect sign and accept, *and client can't connect to
server, this is all.
There's no problems just for this objects.

I'm testing for the situation.. after the connection between server
and client is done, if server or client hits the "ctrl+c" key.

The problem is either case for server or for client hits the "ctrl
+c"key, they goes infinite loop. I thought select() will be blocked
and there'll be no more looping.

and I thought if there's possible error in the select() after getting
"ctrl+c" key, and tried to catch it, but "errno" printing code just
displayed "0" for varible errno :(
Why not just install your own signal handler for SIGINT?

Oct 24 '08 #2
Why not just install your own signal handler for SIGINT?

I inserted this code before while(1)
signal(SIGINT, foo);
but still looping :(

void foo()
{
perror("exit by foo");
exit(1);
}
Actually i was tring is... recognize the errno and if there's errno
change, just printing "what error is about" and exit. But select() or
read() after getting "ctrl_c" don't make errno change.
Oct 24 '08 #3
Hukkky <pi*******@nate.comwrites:
I'm testing simple server/client codes on linux. just server can wait
for client's connect sign and accept, and client can't connect to
server, this is all.
There's no problems just for this objects.
[...]

Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
2
by: duane | last post by:
Dear Experts: I am trying to measure a HTML page download time on the client side and store the value into a textfile located in the server. I have successfully measured the page download time...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
3
by: ferbar | last post by:
Hello all, This may sound pretty basic stuff.. but I'm working on a socket example whose client seems to work fine, but the server doesn't send to the client the expected result. The problem is...
1
by: Amjad | last post by:
Hi, I'm trying to make a TCP Server application that continuously listens to all ASCII data coming through a specific port and save them in a text file. I got it working for one receive...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
1
by: Amjad | last post by:
I want my TCP client to be able to to tell if there is a TCP Server running at the specified IP address and port number without creating exceptions, because this code will be put in an infinite...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
0
by: cmrhema | last post by:
Hi, We have an asynchronous server socket program, which works fine BUT, when due to some circumstances, I receive null data or blank data, the server goes to infinite loop Here is the code ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.