1
Network / Re: "Failed to bind listener socket" when quickly restarting a TCP server
« on: August 03, 2013, 04:40:33 pm »
Okay, thanks for the quick answers.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
class relistener : public sf::TcpListener
{
public:
void reuse() ;
};
void relistener::reuse()
{
char reusec = 1;
setsockopt( getHandle(), SOL_SOCKET, SO_REUSEADDR, &reusec, sizeof( reusec ) );
}
int gameloop()
{
/*...*/
BINDPORT = 53123;
relistener listener;
listener.setBlocking(false);
std::array<sf::TcpSocket,6> socks;
/*...*/
unsigned playercount=0;
listener.reuse();
listener.listen(BINDPORT);
while(mgame.init)
{
if(listener.accept(socks[playercount]) == sf::Socket::Done)
{
std::cout << "accepted player" << std::endl;
playercount++;
socks[playercount].setBlocking(false);
}
handle_commands(mgame,socks); // lets one of the players set mgame.init to false
}
listener.close();
/*...*/
return 0;
}
int main()
{
while(true)
{
gameloop();
}
return 0;
}
sf::Packet pack2;
//....
sf::SocketUDP udp_sock;
if(udp_sock.Send(pack2, server_ip, 3456)==sf::Socket::Done)
{
std::cout << std::endl << "sent UDP Button " << clickpos << " to " << server_ip << ":3456" << std::endl;
}
else
{
std::cout << std::endl << "Sending of UDP Button failed" << std::endl;
}
sf::SocketUDP udp_sock;
if(!udp_sock.Bind(3456))
{
std::cout << std::endl << "Error binding UDP Socket to port " << udp_sock.GetPort() << std::endl;
}
else
{
std::cout<<std::endl<<"Bound Socket to port 3456" <<std::endl;
}
//.....
sf::Packet pack;
sf::IPAddress addy;
unsigned short porty;
if(udp_sock.Receive(pack,addy,porty)==sf::Socket::Done)
{
std::cout << std::endl << "Received UDP Button From " << addy << ":" << port << " with " << pack.GetDataSize() << "B" <<std::endl;
}
else
{
std::cout << std::endl << "Receiving of UDP Button Failed" << std::endl;
}
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Program received signal SIGABRT, Aborted.
0x0012d422 in __kernel_vsyscall ()
Selector.Add(Listener);
#0 0x0012d422 in __kernel_vsyscall ()
#1 0x002ac651 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0x002afa82 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0x0020652f in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
#4 0x00204465 in ?? () from /usr/lib/libstdc++.so.6
#5 0x002044a2 in std::terminate() () from /usr/lib/libstdc++.so.6
#6 0x002045e1 in __cxa_throw () from /usr/lib/libstdc++.so.6
#7 0x00204c5f in operator new(unsigned int) () from /usr/lib/libstdc++.so.6
#8 0x0804aedb in __gnu_cxx::new_allocator<char>::allocate (this=0xbffff370, __n=4294967295)
at /usr/include/c++/4.4/ext/new_allocator.h:89
#9 0x0804a9ac in std::_Vector_base<char, std::allocator<char> >::_M_allocate (this=0xbffff370, __n=4294967295)
at /usr/include/c++/4.4/bits/stl_vector.h:140
#10 0x0804a30a in _Vector_base (this=0xbffff370, __n=4294967295, __a=...) at /usr/include/c++/4.4/bits/stl_vector.h:113
#11 0x08049ed7 in vector (this=0xbffff370, __x=...) at /usr/include/c++/4.4/bits/stl_vector.h:242
#12 0x08049db4 in SocketTCP (this=0xbffff364) at /usr/local/include/SFML/Network/SocketTCP.hpp:46
#13 0x080498ae in DoServer (Port=2435) at main.cpp:72
#14 0x08049c33 in main () at main.cpp:136