LQ Newbie
Registered: Sep 2011
Posts: 3
Rep:
|
Issue with tcp socket programming
Hi All,
I am facing an issue with socket programming. I have two sample source code files(one is a server and the other is for the client).
Scenario 1(No issues observed):
-------------------------------
Disconnect the network cable between the two devices.
Behavior: the recv call returns and the following statement is printed.
Scenario 2:
-----------
Disconnect the network cable between the two devices.
Type something from the client and press enter immediately
Behavior: the recv call is not coming out after that
Do i have to set any other socket options to make this work???
I tried using SO_RCVTIMEO option, but the receive comes out even if there is no messages received from the peer.(Even though it is connected)
Please find the sample source code below,
///////////////////////////////////////////////////////////////////////
tcpserver.c:
/* tcpserver.c */
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <netinet/tcp.h>
#define COMMN_PORT 10000
int connected;
/**************************************
** Tries to create a tcp/ip server socket and returns 0 if successful
** or -1 if not successful
**************************************/
char create_server_socket(int *sock)
{
struct sockaddr_in server_addr;
int socket_param = 1;
if (((*sock) = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\nSocket Creation Failed");
return -1;
}
printf("\nSocket Creation Successfull(%d)",(*sock));
if(setsockopt((*sock),SOL_SOCKET,SO_REUSEADDR,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option SO_REUSEADDR failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),SOL_SOCKET,SO_KEEPALIVE,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option SO_KEEPALIVE failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_NODELAY,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_NODELAY failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_KEEPINTVL,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_KEEPINTVL failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_KEEPIDLE,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_KEEPIDLE failed");
close(*sock);
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(COMMN_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind((*sock), (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
{
printf("\nBind Error");
close(*sock);
return -1;
}
if (listen((*sock), 5) < 0)
{
printf("\nError listening to a port");
close(*sock);
return -1;
}
return 0;
}
void * thread1()
{
int bytes_recieved;
char recv_data[1024];
while(1)
{
bytes_recieved = recv(connected,recv_data,1024,0);
if(bytes_recieved < 1)
{
printf("\nCommunication failed with the peer");fflush(stdout);
break;
}
recv_data[bytes_recieved] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
//close(connected);
break;
}
else
{
printf("\nClient Says = %s " , recv_data);
printf("\n\nSEND (q or Q to quit) : ");fflush(stdout);
fflush(stdout);
}
}
}
int main()
{
int sock = 0, bytes_recieved , bytes_sent, true = 1;
struct sockaddr_in client_addr;
char send_data [1024];
int sin_size;
pthread_t tid;
if(create_server_socket(&sock) == -1)
{
printf("\nSocket Creation Failed.. Exiting the code");
exit(1);
}
printf("\nSocket Creation Successfull(%d)",sock);
while(1)
{
printf("\nTCP Server : Waiting for client on port %d",COMMN_PORT);
fflush(stdout);
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
printf("\n Connected to (IP -> %s ,Port -> %d)",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
pthread_create(&tid,NULL,thread1,NULL);
while (1)
{
printf("\n\nSEND (q or Q to quit) : ");
gets(send_data);
if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
{
bytes_sent = send(connected, send_data,strlen(send_data), 0);
if(bytes_sent < 1)
{
printf("\nSend failed - Bytes sent(%d)",bytes_sent);fflush(stdout);
break;
}
close(connected);
break;
}
else
{
bytes_sent = send(connected, send_data,strlen(send_data), 0);
if(bytes_sent < 1)
{
printf("\nSend failed - Bytes sent(%d)",bytes_sent);fflush(stdout);
break;
}
}
}
}
close(sock);
return 0;
}
///////////////////////////////////////////////////////////////////////
tcpclient.c
////////////////////////////////////////////////////////////////////////
/* tcpclient.c */
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <netinet/tcp.h>
#define COMMN_PORT 10000
int sock;
/**************************************
** Tries to create a tcp/ip client socket and returns 0 if successful
** or -1 if not successful
**************************************/
char create_client_socket(int *sock)
{
int socket_param = 1;
if (((*sock) = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\nSocket Creation Failed");
return -1;
}
printf("\nSocket Creation Successfull(%d)",(*sock));
if(setsockopt((*sock),SOL_SOCKET,SO_REUSEADDR,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option SO_REUSEADDR failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),SOL_SOCKET,SO_KEEPALIVE,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option SO_KEEPALIVE failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_NODELAY,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_NODELAY failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_KEEPINTVL,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_KEEPINTVL failed");
close(*sock);
return -1;
}
if(setsockopt((*sock),IPPROTO_TCP,TCP_KEEPIDLE,&socket_param,sizeof(int)) < 0)
{
printf("\nSet socket option TCP_KEEPIDLE failed");
close(*sock);
return -1;
}
return 0;
}
void * thread1()
{
int bytes_recieved;
char recv_data[1024];
while(1)
{
bytes_recieved = recv(sock,recv_data,1024,0);
if(bytes_recieved < 1)
{
printf("\nCommunication failed with the peer");fflush(stdout);
break;
}
recv_data[bytes_recieved] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
//close(connected);
break;
}
else
printf("\nClient Says = %s " , recv_data);
printf("\n\nSEND (q or Q to quit) : ");fflush(stdout);
fflush(stdout);
}
}
int main()
{
int bytes_recieved,bytes_sent,true = 1;
char send_data[1024],recv_data[1024];
struct sockaddr_in server_addr;
pthread_t tid;
if(create_client_socket(&sock) == -1)
{
printf("\nError Creating socket... Exiting");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(COMMN_PORT);
inet_aton("10.78.34.253", &server_addr.sin_addr);
printf("\nServer IP Address : %s",inet_ntoa(server_addr.sin_addr) );
while(1)
{
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) < 0)
{
printf("\nConnect to server socket failure.. Retry after 10 secs");fflush(stdout);
sleep(10);
continue;
}
break;
}
/*struct timeval tv;
int timeouts = 0;
tv.tv_sec = 2;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof (tv)))
{
printf("setsockopt - rcvtimeo failed");
}*/
pthread_create(&tid,NULL,thread1,NULL);
while(1)
{
printf("\n\nSEND (q or Q to quit) : ");fflush(stdout);
gets(send_data);
if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
{
bytes_sent = send(sock,send_data,strlen(send_data), 0);
if(bytes_sent < 1)
{
printf("\nSend failed - Bytes Sent(%d)",bytes_sent);fflush(stdout);
break;
}
}
else
{
bytes_sent = send(sock,send_data,strlen(send_data), 0);
if(bytes_sent < 1)
{
printf("\nSend failed - Bytes Sent(%d)",bytes_sent);fflush(stdout);
break;
}
close(sock);
break;
}
}
printf("\nBefore thread join");fflush(stdout);
pthread_join(tid, NULL);
printf("\nAfter thread join");fflush(stdout);
return 0;
}
////////////////////////////////////////////////////////////////////////
Thanks in advance.
|