This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Socket read problem on Windows XP Pro & Cygwin


I've created a simple program that is supposed to do the following:

1.) Open a socket to port 8080 on the local machine "localhost"

2.) Open a COM port

3.) Read data that comes in on the socket and write the same data out the COM port

...great performance is not a requirement...so I did the most brain-dead simple thing I could (the source code is included at the end of this post).  The problem is, this works fine on my laptop...but not on the machine it is SUPPOSED to work on.  They are both running the same version of cygwin.  On the machine it fails on, the read always exits with an errno 119 (Operation now in progress).  The read exits as soon as the first character is transmitted (but it blocks until the first character is transmitted).  After that...it doesn't even block...it just returns immediately with errno=119 for ever more...

I'm assuming I'm doing something wrong here...and I'm just lucky to have it work right on my laptop...can anyone help?

Here's the source:

int main(int argc,char *argv[])
{
int infd,outfd;
struct sockaddr_in ipaddr;
struct hostent *hp;
char *srcip = DEFAULT_IP;
unsigned short server_port = DEFAULT_PORT;
int argnum;
int debug_mode = 0;
int block_output = 0;
char *outdev = OUTPUT_DEVICE;
    
    printf("Connecting to %s:%d\n",srcip,server_port);
    printf("Redirecting output to %s\n",outdev);

    infd = socket(AF_INET,SOCK_STREAM,0);
    if (infd < 0)
    {
        printf("Unable to create socket\n");
        printf("errno = %d,infd = %d\n", errno,infd);
        printf("This means %s\n", strerror(errno));
        exit(1);
    }
    
    if ((hp = gethostbyname(srcip)) == NULL) {
        printf("Error: cannot get the host information about ip %s!\n",srcip);
        exit(1);
    }

    memset(&ipaddr, 0, sizeof(ipaddr));
    ipaddr.sin_family = hp->h_addrtype;
    bcopy((char*)hp->h_addr, (char*)&ipaddr.sin_addr, hp->h_length);

    ipaddr.sin_port = htons(server_port);

    if (connect(infd, (struct sockaddr*)&ipaddr, sizeof(ipaddr)) != 0)
    {
        printf("Error: cannot connect!\n");
        printf("errno = %d\n", errno);
        printf("This means %s\n", strerror(errno));
        exit(1);
    }
    
    if (block_output == 0)
    {
        outfd = open(outdev,O_RDWR | O_BINARY,S_IREAD | S_IWRITE);

        if (outfd < 0)
        {
            printf("Error opening %s\n",outdev);
            exit(-1);
        }
    }

    while(1)
	{
	char buf[80];
	int len;

		len = recv(infd,buf,1,0);

		if (len < 1)
		{
			printf("len = %d\n",len);
            printf("errno = %d\n",errno);
            printf("This means %s\n", strerror(errno));
            sleep(1);
		}
        else
        {
            if (debug_mode)
            {
                printf("%c",(buf[0] & 0x7f));
                if (((buf[0] & 0x7f) == 0x0d) || ((buf[0] & 0x7f) == 0x05))
                {
                    printf("\n");
                }
                fflush(stdout);
            }
        }
	

        if (block_output == 0)
        {
            write(outfd,buf,len);
        }

    }
	close(outfd);
	close(infd);

}

Sorry for the long post..and thanks in advance.

Todd



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]