LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2011, 05:54 AM   #1
Eben1588
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
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.
 
  


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
socket programming in KERNEL (TCP/IP) omkarlagu Programming 8 07-04-2011 01:42 AM
TCP/IP book for socket programming hubabuba Programming 3 10-17-2005 03:46 AM
filetransfer with socket and tcp biiiep Programming 2 06-11-2004 12:33 PM
tcp socket monitoring xuttux Linux - Networking 1 05-19-2004 08:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:10 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