Article View: borland.public.cppbuilder.internet.socket
Article #1910Re: select returns 10022 invalid argument error
From: Remy Lebeau (TeamB)
Date: Thu, 28 Sep 2006 18:34
Date: Thu, 28 Sep 2006 18:34
72 lines
3688 bytes
3688 bytes
"jer" <jer_bred_guy@yahoo.com> wrote in message
news:1159444802.345644.119460@m7g2000cwm.googlegroups.com...
> Yeah, somehow I've managed to hang TCP/IP. I wonder if there's a
> way to restart or reset TCP/IP without rebooting windows?
Nope.
> This is the code I use to set up the socket for sending data
> The parameters that are passed into the function are global,
> and are used in the send thread, see below.
Are you protecting your globals from multi-threaded access properly?
> if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
Why are you calling WSAStartup() for each new socket? You do not even need
to call it for each individual thread. You should only be calling it once
at program startup.
> //Set the address of the computer you want to send data to
> he = gethostbyname(ip_addr);
You are not checking for errors in resolving the remote computer.
gethostbyname() will return a NULL pointer if it fails.
> (*their_addr).sin_family = AF_INET; // host byte order
> (*their_addr).sin_port = htons(send_port); // short, network byte
> order
> (*their_addr).sin_addr = *((struct in_addr *)he->h_addr);
> memset(&((*their_addr).sin_zero), '\0', 8); // zero the rest of the
> struct
Use the '->' operator instead of the '*' operator:
their_addr->sin_family = AF_INET; // host byte order
their_addr->sin_port = htons(send_port); // short, network byte order
their_addr->sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr->sin_zero, '\0', 8); // zero the rest of the struct
> if (strcmp(ip_addr, "10.100.4.200") == 0)
> {
> my_addr.sin_addr.s_addr = inet_addr("10.100.4.201");
> }
> else if (strcmp(ip_addr, "10.100.4.201") == 0)
> {
> my_addr.sin_addr.s_addr = inet_addr("10.100.4.200");
> }
That is not a very generic way to handle IPs. Since the calling code
already knows which IP it wants to send to, it should pass in another
parameter to the function to specify the local IP to bind to. Then the
socket code doesn't need these special case operations anymore.
> if((bind(*sockfd, (struct sockaddr *)&my_addr, sizeof(struct
> sockaddr)))==SOCKET_ERROR)
> {
> return error_status = -3;
> }
You are not closing the socket before exiting when an error occurs.
> sel_value = select(2, NULL, &writefds, NULL, &tv);
The first parameter is never used and should be set to 0:
sel_value = select(0, NULL, &writefds, NULL, &tv);
Gambit
Technical Details
Message-ID:
<451c1657@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!out03b.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<1159392295.406227.295400@e3g2000cwe.googlegroups.com> <451b09bf$1@newsgroups.borland.com> <1159444802.345644.119460@m7g2000cwm.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,65957817ea9a74e3 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <1159392295.406227.295400@e3g2000cwe.googlegroups.com> <451b09bf$1@newsgroups.borland.com> <1159444802.345644.119460@m7g2000cwm.googlegroups.com> Subject: Re: select returns 10022 invalid argument error Date: Thu, 28 Sep 2006 11:34:28 -0700 Lines: 74 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 69.169.4.232 Message-ID: <451c1657@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1159468631 69.169.4.232 (28 Sep 2006 11:37:11 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!out03b.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail