USUN_TO wrote:
Hi, i got problem
when i bind in this way:
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
local_addr.sin_port = htons(CLIENT_PORT);
i can easly bind port without any SIGSEGV (segmentation fault) but MSDN
wrote:
"If an application does not care what local address is assigned, specify
the manifest constant value ADDR_ANY for the sa_data member of the name
parameter. This allows the underlying service provider to use any
appropriate network address, potentially simplifying application
programming in the presence of multihomed hosts (that is, hosts that
have more than one network interface and address)."
so how write this? What is diference betwean ADDR_ANY and INADDR_ANY?
Thiese lines are wrong cause makes SIGSEGV:
local_addr.sin_addr.s_addr = htonl(ADDR_ANY);
local_addr.sin_addr.s_addr = htons(ADDR_ANY);
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_addr.s_addr = htons(INADDR_ANY);
local_addr.sin_addr.s_addr = INADDR_ANY;
local_addr.sin_addr.s_addr = ADDR_ANY;
Plz help me...
Below is a function extracted from one of my older C++
libs for Windows. Ignore the calls to IsOpen() and Close(),
they reside elsewhere, but the code may still help you
find your problem. Remember to add '#include <winsock.h>'
in your code.
int
ServerIpSocket::BindToPort(int port, int queueSize)
{
SOCKADDR_IN serverSockAddr;
int status;
// if already associated with a port, close
if (IsOpen())
Close();
/* MANDATORY - zero the sockaddr_in structure */
memset (&serverSockAddr, 0, sizeof (serverSockAddr));
/* specify the port portion of the address */
serverSockAddr.sin_port = htons ((u_short)port);
/* specify the address family as Internet */
serverSockAddr.sin_family = AF_INET;
/* specify that the address does not matter */
serverSockAddr.sin_addr.s_addr = htonl (INADDR_ANY);
/* create a socket */
theSocket = socket (AF_INET, SOCK_STREAM, 0);
if (theSocket == INVALID_SOCKET)
{
cerr << "ERROR: socket unsuccessful"
<< endl;
return (-1);
}
/* associate the socket with the address */
status = bind (theSocket,
(LPSOCKADDR) & serverSockAddr,
sizeof (serverSockAddr));
if (status == SOCKET_ERROR)
{
cerr << "ERROR: bind unsuccessful" << endl;
// close theSocket opened by the above socket() call
Close();
return (-1);
}
// allow the socket to take connections.
// specify a waiting connection queue size
status = listen (theSocket, queueSize);
if (status == SOCKET_ERROR)
{
cerr << "ERROR: listen unsuccessful" << endl;
// close theSocket opened by the above socket() call
Close();
return (-1);
}
// NOTE: For this socket class, we are NEVER connected to a client.
// ConnectToClient() will create/return a NEW socket to handle
// each client connection. Therefore we NEVER set "isConn" to
// non-zero for this class (ie: IsConnected() always returns
// zero)
return 0;
}
Regards,
Larry