delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/10/22/03:12:47

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
To: cygwin AT cygwin DOT com
From: Todd Rearick <trearick_news AT cox DOT net>
Subject: Re: Socket read problem on Windows XP Pro & Cygwin
Date: Sat, 22 Oct 2005 07:06:36 +0000 (UTC)
Lines: 200
Message-ID: <loom.20051022T083200-857@post.gmane.org>
References: <20051022035348.RADA2767.eastrmmtao03.cox.net@[172.18.52.8]> <4359C50C DOT 76649CA1 AT dessent DOT net>
Mime-Version: 1.0
User-Agent: Loom/3.14 (http://gmane.org/)
X-IsSubscribed: yes

Brian Dessent <brian <at> dessent.net> writes:

> 
> Your test case does not compile...

...good point...I was just trying to cut down on the length of the post...but 
it really didn't save that much anyway...I'll attach the full code on the end 
of this post in case anyone wants to try it.

> 
> Is there anything that differs between your two machines in regard to
> what's connected to the com port, or the com port settings?  Have to
> tried compiling with -Wall to see if there are any warnings that might
> indicate you forgot a prototype or used an incorrect parameter? 
> (Standard troubleshooting stuff..)

...hmm...yeah maybe I should try -Wall.  Thnx for the suggestion.  The output 
device really isn't relevant for my problem.  The code takes arguments that 
allow it to run in a mode where it doesn't even try to write to the output 
device (which is a COM port BTW)....when run with the options "-x -d" the code 
just reads from the socket and prints to STDOUT so I can debug this problem.  
The only problem I seem to have on the one machine is actually reading data 
from the socket.

> 
> If you're doing serial IO then you probably need calls to tcgetattr()
> and tcsetattr().....

..well...the com port I am writing to is actually a "virtual" com port that 
doesn't really exist...so those settings don't matter (I think)...regardless, 
it's not really the output that is giving me trouble.

> 
> >                 len = recv(infd,buf,1,0);
> 
> You might as well use read() here if you aren't using the flags.

I had that...and put in recv to see if it would make a difference...it 
didn't  :)

Thanks for your input.  Here is the full source code in case anyone wants to 
compile it and try it (of course...you need a open port to connect it 
to...maybe a telnet server somewhere...??)  The options 

program_name -x -d -h=<ip address of server> -p=<port>

...would be the ones to use.

I compiled with -Wall and found I was missing the <memory.h> include.  I added 
this and -Wall is now clean....but I can't try the code until tomorrow (not 
near the problem machine right now).  If anyone wants to try the original 
code, just comment out the include of <memory.h>

//#define __USE_W32_SOCKETS
//#include <windows.h>

#include <stdlib.h>
#include <stdio.h>

#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <memory.h>

#define OUTPUT_DEVICE   "COM4"
#define DEFAULT_PORT    8080
#define DEFAULT_IP      "localhost"

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;

    for (argnum = 1;argnum < argc;argnum++)
    {
        char *arg = argv[argnum];

        if (arg[0] != '-')
        {
            printf("Ignoring invalid arg |%s|\n",arg);
            continue;
        }

        switch (arg[1])
        {
        case 'd':
            debug_mode = 1;
            break;
        case 'x':
            block_output = 1;
            break;
        case 'h':
            srcip = &arg[3];
            break;
        case 'p':
            sscanf(&arg[3],"%hd",&server_port);
            break;
        case 's':
            outdev = &arg[3];
            break;

        }
    }

    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);

}




--
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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019