bind: Address already in use

345 views
Skip to first unread message

v.yesin

unread,
Dec 9, 2009, 9:51:57 AM12/9/09
to Lightweight Communications and Marshalling
when i try to start multiple listeners from c-examples (cygwin,
windows)
i see an error messages:

$ ./listener.exe
bind: Address already in use
couldn't reg example_t LCM handler!
bind: Address already in use
bind: Address already in use
bind: Address already in use
bind: Address already in use
bind: Address already in use
bind: Address already in use
bind: Address already in use
bind: Address already in use

in linux all works great!

problem with cygwin?

Albert Huang

unread,
Dec 10, 2009, 12:28:49 AM12/10/09
to lcm-...@googlegroups.com
Hi (v.yesin?),

We've tested multiple listeners with Cygwin before, so there's at least
an existence proof that it has worked before. Unfortunately, I don't
think any of the developers use Cygwin on a normal basis; we've just
done a few cursory checks and declared that it seemed to work. Please
let us know if you're able to pinpoint the problem.

Best Regards,
Albert

Jan Hrbáček

unread,
Dec 10, 2009, 4:49:34 AM12/10/09
to lcm-...@googlegroups.com
Hi,
I can confirm it's running - I've tried now to run cygwin, win and .NET listeners and all executes flawlessly.

Jan
Jan Hrbáček
-----------
Web: http://hrbacek.info
ICQ: 319583822
Skype: j.hrbacek


Albert Huang napsal(a):
--

You received this message because you are subscribed to the Google Groups "Lightweight Communications and Marshalling" group.
To post to this group, send email to lcm-...@googlegroups.com.
To unsubscribe from this group, send email to lcm-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/lcm-users?hl=en.


  

v.yesin

unread,
Dec 11, 2009, 5:39:24 AM12/11/09
to Lightweight Communications and Marshalling
hi!

but we've trying to start listeners from lcm-0.3.1 with cygwin (1.7)
and windows (vista 32bit or server 2008 32bit)

first listener starts with messages:
$ ./listener.exe
Initializing LCM UDPM context...
Local 0.0.0.0
Multicast 239.255.76.67:7667
LCM multicast TTL set to 0. Packets will not leave localhost
LCM: setting multicast packet TTL to 0
registering EXAMPLE handler 0x402340
allocating resources for receiving messages
LCM: setting SO_REUSEADDR
LCM: send buffer is 65535 bytes
LCM: receive buffer is 65535 bytes
LCM: joining multicast group
LCM: conducting self test
registering LCM_SELF_TEST handler 0x405c90
transmitting 13 byte [LCM_SELF_TEST] payload (35 byte pkt)
LCM: self test complete
LCM: self test successful

looks good... when trying to start second... i see:
$ ./listener.exe
Initializing LCM UDPM context...
Local 0.0.0.0
Multicast 239.255.76.67:7667
LCM multicast TTL set to 0. Packets will not leave localhost
LCM: setting multicast packet TTL to 0
registering EXAMPLE handler 0x402340
allocating resources for receiving messages
LCM: setting SO_REUSEADDR
LCM: send buffer is 65535 bytes
LCM: receive buffer is 65535 bytes
bind: Address already in use
couldn't reg example_t LCM handler!
allocating resources for receiving messages
LCM: setting SO_REUSEADDR
LCM: send buffer is 65535 bytes
LCM: receive buffer is 65535 bytes
bind: Address already in use
allocating resources for receiving messages
LCM: setting SO_REUSEADDR
LCM: send buffer is 65535 bytes
LCM: receive buffer is 65535 bytes
bind: Address already in use
...

v.yesin

unread,
Dec 11, 2009, 6:05:26 AM12/11/09
to Lightweight Communications and Marshalling
hm... when i try this in windows xp (32 bit)... all works as expected!

seems problem with cygwin and/or windows vista and server 2008

Albert Huang

unread,
Dec 11, 2009, 11:13:10 AM12/11/09
to lcm-...@googlegroups.com
perhaps try enabling the USE_REUSEPORT code path in lcm/lcm_udp.c?

-albert

On Fri, Dec 11, 2009 at 03:05:26AM -0800, v.yesin wrote:
> hm... when i try this in windows xp (32 bit)... all works as expected!
>
> seems problem with cygwin and/or windows vista and server 2008
>

v.yesin

unread,
Dec 11, 2009, 2:03:59 PM12/11/09
to Lightweight Communications and Marshalling
USE_REUSEPORT not available in cygwin environment...
it's BSD's only feature! 8)

we've performed tests in windows 7 environment... result is the same
as with vista/server2008... failure!

i'll try to localize problem in next week
thanks!

Jan Hrbáček

unread,
Dec 11, 2009, 2:14:10 PM12/11/09
to lcm-...@googlegroups.com
This is strange, I'm running all my tests on a win 7 machine and
experiencing no problems...

Jan Hrbáček
-----------
Web: http://hrbacek.info
ICQ: 319583822
Skype: j.hrbacek


Dne 11.12.2009 20:03, v.yesin napsal(a):

v.yesin

unread,
Dec 15, 2009, 4:05:20 AM12/15/09
to Lightweight Communications and Marshalling
looks like a problem with SO_REUSEADDR exists in CYGWIN for all
windows with support for "Enhanced socket security" (see
http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx)

i'll show some samples to demonstrate this:
=================listener.c========================
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <stdio.h>
#include <stdlib.h>

struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
int datalen;
char databuf[1024];

int main(int argc, char *argv[])
{
struct sockaddr_in sender_addr;
int sender_addr_size = sizeof(sender_addr);

#ifdef __MINGW32__
WSADATA wsadata;

if (WSAStartup(MAKEWORD(2,2), &wsadata) == SOCKET_ERROR) {
printf("can't WSAStartup\n");
exit(1);
}
#endif

/* Create a datagram socket on which to receive. */
sd = socket(AF_INET, SOCK_DGRAM, 0);

if(sd < 0)
{
printf("Opening datagram socket error %d\n", sd);
exit(1);
}
printf("Opening datagram socket....OK.\n");

/* Enable SO_REUSEADDR to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */
{
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof
(reuse)) < 0)
{
perror("Setting SO_REUSEADDR error");
close(sd);
exit(1);
}
printf("Setting SO_REUSEADDR...OK.\n");
}

/* Bind to the proper port number with the IP address */
/* specified as INADDR_ANY. */
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(4321);
localSock.sin_addr.s_addr = INADDR_ANY;
if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
{
perror("Binding datagram socket error");
close(sd);
exit(1);
}
printf("Binding datagram socket...OK.\n");

/* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */
/* interface. Note that this IP_ADD_MEMBERSHIP option must be */
/* called for each local interface over which the multicast */
/* datagrams are to be received. */
group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
group.imr_interface.s_addr = INADDR_ANY;//inet_addr("203.106.93.94");
if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group,
sizeof(group)) < 0)
{
perror("Adding multicast group error");
close(sd);
exit(1);
}
printf("Adding multicast group...OK.\n");

/* Read from the socket. */
datalen = sizeof(databuf);
while(1)
{
printf("Receiving...\n");
if(recvfrom(sd, databuf, datalen, 0, (struct sockaddr *)
&sender_addr, &sender_addr_size) < 0)
{
perror("Reading datagram message error");
close(sd);
exit(1);
}
else
{
printf("Reading datagram message...OK.\n");
printf("The message from multicast server is: \"%s\"\n", databuf);
}
}
return 0;
}
===============================================================
============send.c==============================================
/* Send Multicast Datagram code example. */
#include <sys/types.h>
#ifndef __MINGW32__
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <stdio.h>
#include <stdlib.h>

struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
char databuf[1024] = "Multicast test message!";
int datalen = sizeof(databuf);

int main (int argc, char *argv[ ])
{
#ifdef __MINGW32__
WSADATA wsadata;

if (WSAStartup(MAKEWORD(2,2), &wsadata) == SOCKET_ERROR) {
printf("can't WSAStartup\n");
exit(1);
}
#endif

/* Create a datagram socket on which to send. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
perror("Opening datagram socket error");
exit(1);
}
printf("Opening the datagram socket...OK.\n");

/* Initialize the group sockaddr structure with a */
/* group address of 225.1.1.1 and port 5555. */
memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("226.1.1.1");
groupSock.sin_port = htons(4321);

/* Disable loopback so you do not receive your own datagrams.
{
char loopch = 0;
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch,
sizeof(loopch)) < 0)
{
perror("Setting IP_MULTICAST_LOOP error");
close(sd);
exit(1);
}
printf("Disabling the loopback...OK.\n");
}
*/

/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface. */
localInterface.s_addr = INADDR_ANY;
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)
&localInterface, sizeof(localInterface)) < 0)
{
perror("Setting local interface error");
exit(1);
}
printf("Setting the local interface...OK\n");

/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
/*int datalen = 1024;*/
if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock,
sizeof(groupSock)) < 0)
{
perror("Sending datagram message error");
exit(1);
}
printf("Sending datagram message...OK\n");

#ifdef __MINGW32__
closesocket(sd);
WSACleanup();
#endif

return 0;
}

===============================================================

with cygwin environment i can't run multiple listerers... bind error:
Address already in use!
(
building with cygwin:
gcc -o listener.cygwin.exe listener.c
gcc -o send.cygwin.exe send.c
)

but with mingw all works as expected!
(
building with mingw:
gcc -o listener.cygwin.exe listener.c -lws2_32
gcc -o send.cygwin.exe send.c -lws2_32
or in cygwin environment:
gcc -mno-cygwin -o listener.cygwin.exe listener.c -lws2_32
gcc -mno-cygwin -o send.cygwin.exe send.c -lws2_32
)

yesin

unread,
Dec 16, 2009, 6:01:05 AM12/16/09
to lcm-...@googlegroups.com
yes! where is a known feature of cygwin to drop SO_REUSEADDR calls on
windows which support "enhanced socket security".
(see http://cygwin.com/ml/cygwin/2009-12/msg00508.html)

Corinna Vinschen (Cygwin Project Co-Leader) performed some changes against cygwin networking layer (see CVS for details http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/net.cc.diff?r1=1.267&r2=1.268&cvsroot=src&f=h <http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/net.cc.diff?r1=1.267&r2=1.268&cvsroot=src&f=h>)



v.yesin пишет:

Albert Huang

unread,
Dec 17, 2009, 11:38:16 AM12/17/09
to lcm-...@googlegroups.com
Good to hear! Thanks for tracking it down and letting us know the
results.

-albert

--
MIT CSAIL | 32 Vassar St 331 | Cambridge, MA 02139

Reply all
Reply to author
Forward
0 new messages