Click here to Skip to main content
15,889,820 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C
#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
	SOCKET SocketDescripter,AcceptSocket;//,ListenSocket;	
	WSADATA wsadataValue;
	WORD wData=MAKEWORD(2,2);
	WSAStartup(wData,&wsadataValue);
	DWORD dvalue;

	fd_set *readfds;

//	struct timeval *timevalue;
	/*a=10;	
	timevalue->tv_sec=a;
	
	timevalue->tv_sec=30;*/

	SocketDescripter=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(SocketDescripter==SOCKET_ERROR)
	{
		printf("error");
		WSACleanup();
		return 0;
	}
	else
	{
		printf("\n Sucess\n");
	}
	sockaddr_in sockap;
	sockap.sin_family=AF_INET;
	sockap.sin_port=htons(10000);
	sockap.sin_addr.s_addr=inet_addr("127.0.0.1");
	bind(SocketDescripter,(sockaddr*)&sockap,sizeof(sockap));
	listen(SocketDescripter,2);
	unsigned long lValue=1;
		if(0==ioctlsocket(SocketDescripter, FIONBIO,&lValue))
		{
			printf("the io control is sucessfully done\n");
		}
		FD_ZERO(&readfds);
		
		FD_SET(SocketDescripter,&readfds);
	
		dvalue=select(NULL,readfds,NULL,NULL,NULL);
		
		if(FD_ISSET(SocketDescripter,&readfds)==0)
		{
			int size=sizeof(SocketDescripter);
			AcceptSocket=accept(SocketDescripter,(sockaddr*)&SocketDescripter,&size);
			//ioctlsocket(AcceptSocket,FIONBIO,&lValue);
			char buffer[1024];
			printf("i am here");
			recv(AcceptSocket,buffer,sizeof(buffer),NULL);

		}
	return 0;
}


i try like that..

i don't know how to use the select. i studied the msdn website about the basic syntax and try..

can you teach me how to use during a client server application..

i keep on trying that area..

.. please help me..
Posted
Updated 14-Jul-11 21:14pm
v2

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jul-11 3:22am    
It looks like the usage is explained in the referenced article, my 5.
--SA
@BangIndia 15-Jul-11 3:31am    
Thanks you sir..from you help i done the program sir..


my program is::


// select.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
SOCKET SocketDescripter,AcceptSocket;//,ListenSocket;
WSADATA wsadataValue;
WORD wData=MAKEWORD(2,2);
WSAStartup(wData,&wsadataValue);
DWORD dvalue;
TIMEVAL timeout;

fd_set *readfds;

// struct timeval *timevalue;
/*a=10;
timevalue->tv_sec=a;

timevalue->tv_sec=30;*/
int iListenvalue;
SocketDescripter=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(SocketDescripter==SOCKET_ERROR)
{
printf("error");
WSACleanup();
return 0;
}
else
{
printf("\n Sucess\n");
}

sockaddr_in sockap,clientcon;
sockap.sin_family=AF_INET;
sockap.sin_port=htons(10000);
sockap.sin_addr.s_addr=inet_addr("127.0.0.1");

if(SOCKET_ERROR==bind(SocketDescripter,(sockaddr*)&sockap,sizeof(sockap)))
{
printf("\bBinding error\n");
return 1;
}

iListenvalue = listen(SocketDescripter,2);

if(iListenvalue != 0)
{
printf("The socket listening error\n");
closesocket(SocketDescripter);
WSACleanup();
}


unsigned long lValue=1;
if(0==ioctlsocket(SocketDescripter, FIONBIO,&lValue))
{
printf("the io control is sucessfully done\n");
}
while(1)
{
FD_ZERO(&readfds);

FD_SET(SocketDescripter,&readfds);
timeout.tv_sec=30;
timeout.tv_usec=0;
dvalue=select(4,readfds,NULL,NULL,&timeout);
if(dvalue<0)
{
printf("select error\n");
}
else
{
printf("\nSelect is sucessful\n\n");
}
if(FD_ISSET(SocketDescripter,&readfds))
{
printf("waiting for client\n");
int size=sizeof(clientcon);
AcceptSocket=accept(SocketDescripter,(struct sockaddr*)&clientcon,&size);
if(SOCKET_ERROR == AcceptSocket)
{
printf("\nSocket accept Error\n");
closesocket(SocketDescripter);
WSACleanup();
return 0;
}
lValue=0;
ioctlsocket(AcceptSocket,FIONBIO,&lValue);
char buffer[1024];
printf("i am here");
recv(AcceptSocket,buffer,sizeof(buffer),NULL);

}
}
return 0;
}


..


problems is :
its not waiting the for the client connection .. it continuously running sir.. what can i do now.. please help sir
R. Erasmus 15-Jul-11 4:03am    
wild guess... remove the while(1) maybe? Increase the timeout.
@BangIndia 15-Jul-11 5:31am    
no sir.. its not working.. i tried sir..


@BangIndia 15-Jul-11 5:18am    
ok.. i will do then i will post the program responce
MSDN help on this API provides a whole article explaining its usage: http://msdn.microsoft.com/en-us/library/ms740141%28v=vs.85%29.aspx[^][^]. It's hard to add anything else to it.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900