Re: Socket "bind" isn't working for me.
Re: Socket "bind" isn't working for me.
- Subject: Re: Socket "bind" isn't working for me.
- From: Justin Walker <email@hidden>
- Date: Sat, 23 Apr 2005 10:34:47 -0700
On Apr 23, 2005, at 0:31, John Draper wrote:
Hi,
Can someone please tell me what I might be doing wrong?
Desired conditions....
I have two remote hosts I want to communicate with by using the UDP
protocol. One host knows a list of other hosts. Looks something
like this.
Main Host (server)
client1
client2
<etc>
client1 and client2 may be behind a NAT. Main host knows both, so
client1 will
ask to be connected to client2. Since Mail Host knows both, it
initiates
connections to both, once this happens, the connections are passed
from
client1 to client2 and vice versa.
client1 will ask Main Host for IP/Port of client2. Client1 will use
only ONE socket
to communicate with Main Host and client2. "sendto" will be used to
send to
either remote host, depending on circumstances. So I'm going to want
to not
only specify buffer and size, but also IP and port.
I want to be able to switch from one IP/Port to another. I think I
can either use
"bind" for that, and use "send", OR I use "sendto" and specify the
IP/Port.
I'm not sure which one to use.
It's not at all clear to me what you want to do, but I don't think you
understand what the calls do.
'bind' is used to give the local endpoint a specific address or port
designation; it is not used to "switch" anything having to do with the
remote endpoint.
You use 'send' if the system (kernel) already knows the remote
endpoint, i.e., if you have called 'connect'. You use 'sendto' if you
are using a datagram protocol, like UDP, and want to send to a number
of endpoints (or if you don't want to 'connect').
So far, neither works of course. So my
approach is flawed, or something is seriously wrong with the EDCommon
code
code I'm using.
I have no idea what the 'EDCommon' code is.
You don't need to be familiar with EDCommon framework, because it uses
the
UNIX style of Socket API's, and listed below, the code is quite
obvious.
- (void)setLocalPort:(unsigned short)aPort andAddress:(NSString
*)addressString
{
struct sockaddr_in socketAddress;
memset(&socketAddress, 0, sizeof(struct sockaddr_in));
socketAddress.sin_family = AF_INET;
socketAddress.sin_addr = EDInAddrFromString(addressString);
socketAddress.sin_port = htons(aPort);
if(bind(EDSOCKETHANDLE, (struct sockaddr *)&socketAddress,
sizeof(socketAddress)) == -1)
[NSException raise:NSFileHandleOperationException
format:@"Binding of socket to port %d failed: %s", (int)aPort,
strerror(ED_ERRNO)];
}
In My application, the "bind" fails and returns a -1. However, I'm
unable to read
"errno" to determine just what the error is. I looked for "errno", a
standard
global variable containing this value, but I have no way to look at
it, because the
"strerror(ED_ERRNO)" is also broken.
None of this stuff rings a bell, so it's impossible for me to guess
what the "common code" is doing under the covers.
I'd hazard the guess that the ED code is broken.
You should be able to examine 'errno' using gdb; at least I have not
had much problem with it:
$ cat > foo.c
#include <stdio.h>
main()
{ int fd;
if ((fd = open("/tmp/frabble", 0)) == -1)
perror("OPEN");
else
printf("It worked???\n");
exit(fd);
}
$ cc -g -o foo foo.c
$ gdb foo
[snip]
(gdb) b main
Breakpoint 1 at 0x1d34: file foo.c, line 6.
(gdb) r
Starting program: /private/tmp/foo
Reading symbols for shared libraries . done
6 if ((fd = open("/tmp/frabble", 0)) == -1)
(gdb) n
7 perror("OPEN");
(gdb) p (int)errno
$1 = 2
(gdb)
Regards,
Justin
Can someone explain how I can examine this "errno" using "gdb"?
Has anyone else reported problems like this? I already scanned the
archives,
and didn't see anyone post a similar problem.
John
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
--
Justin C. Walker, Curmudgeon-At-Large
Institute for General Semantics
--------
Some people have a mental horizon of radius zero, and
call it their point of view.
-- David Hilbert
--------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden