Hi..I am writing a server code for TCP/IP. The code is:

#pragma comment(lib,"user32.lib");

/*Function definitions for communication over TCPIP*/

/*Parameters for TCP/IP Communication*/
WSADATA wsaData;
WORD version;
int error = 0;
SOCKET server_addr;
SOCKET client_addr;
struct sockaddr_in Start_Server;
int length = 0;
char tcp_ip_recv[20];
char *at_mmi_command;

/*Function initializes winsock*/
int Ta_Ut_tcpip_winsock_init()
	version = MAKEWORD( 2, 0 );
	/*initiate use of the Winsock DLL by a process*/
	/*Arg1-highest version of Windows Sockets specification*/
	/*Arg2-pointer to WSA data structure that is to receive details of the Windows Sockets implementation*/
	error = WSAStartup( version, &wsaData );

	/* check for error */
	if ( error != 0 )
		/* error occured */
		printf("\nError in WSAstartup\n");

	/* check for correct version */
	if ( LOBYTE( wsaData.wVersion ) != 2 ||
		 HIBYTE( wsaData.wVersion ) != 0 )
		/* incorrect WinSock version */
		return FALSE;

	/* WinSock has been initialized */

/*Function creates a socket*/
void Ta_Ut_tcpip_create_socket()
	server_addr = socket( AF_INET, SOCK_STREAM, 0 );
	client_addr = socket( AF_INET, SOCK_STREAM, 0 );

/*Function starts the server*/
void Ta_Ut_tcpip_start_server()
	/*Start the Server*/
	/*Specify end-point address using sockaddr_in structure*/
	Start_Server.sin_family = AF_INET;
	Start_Server.sin_addr.s_addr = INADDR_ANY;	//receive packets from all interfaces
	Start_Server.sin_port = htons( 5000 );	//htons converts a u_short from host to TCP/IP network byte order

	/*Bind to the socket*/
	if ( bind( server_addr, &Start_Server, sizeof ( Start_Server ) ) == SOCKET_ERROR )
		/* could not start server */
		printf( "Binding Error with error %d\n", WSAGetLastError() );

/*Funtion enables server to listen to the client*/
void Ta_Ut_tcpip_listen_client()
	/*Server listens for the client*/
	while ( listen( server_addr, SOMAXCONN ) == SOCKET_ERROR )
		printf( "Error Listening with error %d\n", WSAGetLastError() );

/*Function enables server to accept client connection*/
void Ta_Ut_tcpip_accept_client()
	length = sizeof (Start_Server);
	client_addr = accept( server_addr, &Start_Server, &length );

/*Function sends data over TCP/IP*/
void Ta_Ut_tcpip_send(at_mmi_command)
	if(( send( server_addr,at_mmi_command , sizeof at_mmi_command, MSG_OOB) == SOCKET_ERROR ))
		printf( "Error sending with error %d\n", WSAGetLastError() );

/*Function receives data over TCP/IP*/
char *Ta_Ut_tcpip_receive()
	memset( tcp_ip_recv, 0, sizeof tcp_ip_recv );
	if(( recv( server_addr, tcp_ip_recv, sizeof tcp_ip_recv, MSG_OOB) == SOCKET_ERROR ))
		printf( "Error receiving with error %d\n", WSAGetLastError() );
	return tcp_ip_recv;

/*Function closes socket to terminate communication over TCPIP*/
void Ta_Ut_tcpip_close_socket()
	/*Close socket*/
	closesocket( server_addr );

/*Reset TCP/IP connection*/
void Ta_Ut_tcpip_reset()
	memset( tcp_ip_recv, 0, sizeof tcp_ip_recv );
	int error = 0;
	int length = 0;

The functions used above are declared in a header. Now I have to set a timeout for receiving i.e. I have to stop waiting to receive data after say 60 sec..how exactly do I go about that? Is there a specific function for that as in serial communication? I have looked around but have not been able to find good pointers in that direction. Kindly advise. Thanks.

I have not gone through your full code.
But u can implement timeout for sockets using the "setsockopt()" function as below :

struct timeval tv;

tv.tv_sec = 60;  /* 60 Secs Timeout */

setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));

This you can do after your call to "recv()"...

Thanks for the reply..will try it out and post the results...

Ok..tested it out..and it worked fine..thanks a lot..I'm marking this thread as solved now..thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.