English 中文(简体)
我如何在MacOSX/Ubuntu再编一册? 第二次
原标题:How do I re-bind a socket in MacOSX/Ubuntu? A second time

我有以下法典:

if ( ( m_mainSocket = ::socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 )
{
    throw Exception( __FILE__, __LINE__ ) << "Unable to create socket";
}

int on( 0 );

if ( setsockopt( m_mainSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof( on ) ) )
{
    throw Exception( __FILE__, __LINE__ ) << "Can t make server socket resusable.";
}

sockaddr_in addr;
memset( &addr, 0, sizeof( addr ) );

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl( INADDR_ANY );
addr.sin_port = htons( p_localPort );

if ( ::bind( m_mainSocket, reinterpret_cast< sockaddr * >( &addr ), sizeof( addr ) ) < 0 )
{
    throw Exception( __FILE__, __LINE__ ) << "Failed to bind the server socket";
}

当国际铁路运输中心服务器(停机)使用时,我可以在同一港口再次打开服务器。 为什么? 我需要改变港口设施备用设施系统。 只是在乌兰巴托和马西锡发生。 在Windows I中,没有问题。

错误出现在“综合()”功能中。 请允许我再提一下两件事。

我如何重新组合?

最佳回答

你们需要将1,而不是0,送到REUSEADDR。

http://www.un.org/Depts/DGACM/index_french.htm

   Socket Options
       These socket options can be set by using setsockopt(2)  and  read  with
       getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:

      ....

       SO_REUSEADDR
              Indicates  that  the rules used in validating addresses supplied
              in a bind(2) call should allow reuse of  local  addresses.   For
              AF_INET  sockets  this means that a socket may bind, except when
              there is an active listening socket bound to the address.   When
              the listening socket is bound to INADDR_ANY with a specific port
              then it is not possible to bind  to  this  port  for  any  local
              address.  Argument is an integer boolean flag.
问题回答

暂无回答




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签