H
Hukkky
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///
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///