Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with socket (unix)

Started by BlueChip Jun 16, 2007 at 8:33 AM 1 replies 650+ views
Original Post
BlueChip
BlueChip
Hi! My goal is code PROXYTEST, an application that takes data from an entering TCP connection, and then sends them from 2 outing TCP connection. I'm studing I/O MULTIPLEXING, but, maybe, I've not undertood it, because in my application, there are some errors... but I don't see them... can you help me??? the error (or errors) is not a syntax error, but a conceptual error...

--omissis--
here there is the code that calls socket, calls bind, calls listening, connect, etc.
now, I've 1 socket in listen mode, and 2 socket in connect mode.
--omissis--
//I prepare the neccesry for SELECT
FD_ZERO(&rdset);
maxfd=0;
for(i=0;i<2;i++) 
{
	if(maxfd<socketfd) maxfd=socketfd;
	FD_SET(socketfd,&rdset);
}
if(maxfd<listenfd) maxfd=listenfd;
FD_SET(listenfd,&rdset);

for(;;)
{	
	do {
		ris=select(maxfd+1,&rdset,NULL,NULL,NULL);
	} while( (ris<0) && (errno==EINTR) );
	if(ris<0) {
		perror("PROXY: select failed: ");
		exit(1);
	} 
	else 
	{
	//THIS WORK FINE.. ACCEPTS THE ENTERING CONNECTION
	if( (listenfdactive) && (FD_ISSET(listenfd,&rdset )) )
	{
		do {
			memset (&Client, 0, sizeof (Client));
			len = sizeof (Client);
			connectedfd = accept( listenfd, (struct sockaddr *) &Client, &len);
			} while ( (connectedfd<0) && (errno==EINTR) );

			if (connectedfd < 0 )
			{	perror("accept() failed: \n"); exit (1); }
			else
			{
				if(maxfd<connectedfd) maxfd=connectedfd;
				FD_SET(connectedfd,&rdset);
				connectedfdactive = 1;
			}
			close(listenfd);
			listenfdactive=0;
			FD_CLR(listenfd,&rdset);
		}

		//THIS DOESN'T WORKS VERY WELL  :(
		if( (connectedfdactive) && (FD_ISSET(connectedfd,&rdset )) )
		{
			nread=LENMSG;
			memset(&msg,0,LENMSG);
			
			//READ LENMSG BYTES, I.E A COMPLETE MESSAGE
			do {
				ris=read(connectedfd, (char*)&msg,nread);
			} while ( (ris<0) && (errno==EINTR) );
			if(ris<0) 
			{	perror("PROXY:read failed: ");
				fflush(stdout);
				return (0);
			} 
			else if(ris==0) 
			{
				//ALL DATA READ
				close(connectedfd);
				FD_CLR(connectedfd, &all);
				connectedfdactive = 0;
				fflush(stdout);
			}
			else
			{
				//A MESSAGE IS READ, SO I SEND IT THROUGH 1st 
                                //or 2nd OUTING SOCKET. NOW I USE ONLY ONE SOCKET
				nwrite=LENMSG;
				n = write(socketfd[0], &(msg), nwrite);
				if (n != nwrite)
				{
					printf ("Writen() failed \n");
					fflush(stdout);
					return (1);
				}
			}


this code seems tha read all entering bytes, but write few outing bytes, and then give a write error. I've tried to add this code after for(;;), instead of to put it after the reading operation, but seems that (FD_ISSET(socketfd,&rdset ) is never true. Select function doesn't wake up connected socket file descriptor if there are data in msg buffer.

for(i=0;i<2;i++) 
{
	if( (socketfdactive) && (FD_ISSET(socketfd,&rdset )) )
	{
		nwrite=LENMSG;
		n = Writen(socketfd, &(msg), nwrite);
		if (n != nwrite)
		{
			printf ("Writen() failed \n");
			fflush(stdout);
			return (1);
		}
		fflush(stdout);
	}
}


can you see what I wrong?? thaks!! :)
BlueChip
BlueChip
ok... I'll do something like this..
FD_ZERO(&temp);maxfd=0;for(i=0;i<2;i++) {	if(maxfd<socketfd) maxfd=socketfd;	FD_SET(socketfd,&temp);}if(maxfd<listenfd) maxfd=listenfd;FD_SET(listenfd,&temp);for(;;){		rdset=temp;


but the 'writer code' is better put it after the reading operation, or under a its if guardian like this if( (socketfdactive) && (FD_ISSET(socketfd,&rdset )) ) ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.