sherwingw
asked on
Failing TCP socket bind in WINAPI
I am trying to create a socket server which will attach requesting clients to their own socket. Eventualy I want each client socket to cause a new thread to spawn which
will interact with the server by sending it text information and by receiving back text representing certain variables
present on the server. I have the following piece of code which fails at the bind call. What am I doing wrong?
SOCKET Server_Listening_Socket; // Server_Listening_Socket for client
connection requests
SOCKET Requesting_Client_Socket; // Server's accepted client requestin
socket
SOCKET Assigned_Client_Socket[20] // Assigned Client Sockets
SOCKADDR_IN Server_Listening_Socket_Ad dress; // Local address and port
number
SOCKADDR_IN Assigned_Client_Socket_Add ress[20]; // Client Local address and
port number
struct sockaddr FAR* List_of_Clients[20]; // The address of the connecting
clients
char Local_HostName[80] ={"adds_server"}; // Servers local host name
WSADATA wsaData;
typedef struct stConnData
{
SOCKET hsock; // Connection socket
SOCKADDR_IN stRmtName; // Remote host address & port
LONG lStartTime; // Time of connect
BOOL bReadPending; // Deferred read flag
int iBytesRcvd; // Data buffered
int iBytesSent; // Data sent from buffer
long lByteCount; // Total Bytes received
char achIOBuf[160] // Network I/O data Buffer
struct stConnData FAR*lpstNext; // Pointer to next record
} CONNDATA, *PCONNDATA, FAR *LPCONNDATA;
LPCONNDATA lpstSockHead = 0; // Head of the list
int Job_Requester_Socket_Serve r()
{
int Client_Socket_Channel = 0;
// We will open a socket to listen for incoming connection requests from
the client pool
if(WSAStartup (2, &wsaDatat))
{
ErrorShow("Unable to initialize Winsock library.");
return(1);
}
Server_Listening_Socket_Ad dress.sin_ family = PF_INET;
Server_Listening_Socket_Ad dress.sin_ port = 8000;
Server_Listening_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( Server_Listening_Socket == 0)
{
ErrorShow("Cannot establish connection");
return(1);
}
// THIS CODE IS FAILING EVERY TIME. FOR SOME REASON bind is not working
if ( bind ( Server_Listening_Socket,
(LPSOCKADDR)"ADDS_Server_L istening_S ocket", 160))
{
ErrorShow("Cannot bind socket");
}
if ( listen ( Server_Listening_Socket, 20))
{
ErrorShow("Cannot Listen");
closesocket ( Server_Listening_Socket );
return(1);
}
while(1)
{
// Each successive client is assigned its own socket from the 20
available
if(Client_Socket_Channel < 20)
{
Client_Socket_Channel++;
}
else
{
Client_Socket_Channel = 0;
}
// Accept connection request form Clients on the
Server_Listening_Socket
Requesting_Client_Socket = accept (Server_Listenting_Socket,
List_of_Clients[Client_Soc ket_Channe l],
NULL);
Assigned_Client_Socket_Add ress[Clien t_Socket_C hannel].si n_family =
PF_INET;
Assigned_Client_Socket_Add ress[Clien t_Socket_C hannel].si n_port = 8100
+ Client_Socket_Channel;
Assigned_Client_Socket[Cli ent_Socket _Channel] = socket(PF_INET,
SOCK_STREAM, IPPROTO_TCP);
if ( Asasigned_Client_Socket[Cl ient_Socke t_Channel] == 0
{
ErrorShow("Cannot establish client connection");
return (1);
}
if ( Bind ( Assigned_Client_Socket[Cli ent_Socket _Channel],
(LPSOCKADDR)...........
.
.
.
Bind is still failing with 0 argument in the socket call. I suspect that the problem is with the second argument to bind(). I do not understand what it is. Can you give me a minimal example that opens a server socket? I am sure that once I understand this, the client side will be easy.
will interact with the server by sending it text information and by receiving back text representing certain variables
present on the server. I have the following piece of code which fails at the bind call. What am I doing wrong?
SOCKET Server_Listening_Socket; // Server_Listening_Socket for client
connection requests
SOCKET Requesting_Client_Socket; // Server's accepted client requestin
socket
SOCKET Assigned_Client_Socket[20]
SOCKADDR_IN Server_Listening_Socket_Ad
number
SOCKADDR_IN Assigned_Client_Socket_Add
port number
struct sockaddr FAR* List_of_Clients[20]; // The address of the connecting
clients
char Local_HostName[80] ={"adds_server"}; // Servers local host name
WSADATA wsaData;
typedef struct stConnData
{
SOCKET hsock; // Connection socket
SOCKADDR_IN stRmtName; // Remote host address & port
LONG lStartTime; // Time of connect
BOOL bReadPending; // Deferred read flag
int iBytesRcvd; // Data buffered
int iBytesSent; // Data sent from buffer
long lByteCount; // Total Bytes received
char achIOBuf[160] // Network I/O data Buffer
struct stConnData FAR*lpstNext; // Pointer to next record
} CONNDATA, *PCONNDATA, FAR *LPCONNDATA;
LPCONNDATA lpstSockHead = 0; // Head of the list
int Job_Requester_Socket_Serve
{
int Client_Socket_Channel = 0;
// We will open a socket to listen for incoming connection requests from
the client pool
if(WSAStartup (2, &wsaDatat))
{
ErrorShow("Unable to initialize Winsock library.");
return(1);
}
Server_Listening_Socket_Ad
Server_Listening_Socket_Ad
Server_Listening_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( Server_Listening_Socket == 0)
{
ErrorShow("Cannot establish connection");
return(1);
}
// THIS CODE IS FAILING EVERY TIME. FOR SOME REASON bind is not working
if ( bind ( Server_Listening_Socket,
(LPSOCKADDR)"ADDS_Server_L
{
ErrorShow("Cannot bind socket");
}
if ( listen ( Server_Listening_Socket, 20))
{
ErrorShow("Cannot Listen");
closesocket ( Server_Listening_Socket );
return(1);
}
while(1)
{
// Each successive client is assigned its own socket from the 20
available
if(Client_Socket_Channel < 20)
{
Client_Socket_Channel++;
}
else
{
Client_Socket_Channel = 0;
}
// Accept connection request form Clients on the
Server_Listening_Socket
Requesting_Client_Socket = accept (Server_Listenting_Socket,
List_of_Clients[Client_Soc
NULL);
Assigned_Client_Socket_Add
PF_INET;
Assigned_Client_Socket_Add
+ Client_Socket_Channel;
Assigned_Client_Socket[Cli
SOCK_STREAM, IPPROTO_TCP);
if ( Asasigned_Client_Socket[Cl
{
ErrorShow("Cannot establish client connection");
return (1);
}
if ( Bind ( Assigned_Client_Socket[Cli
(LPSOCKADDR)...........
.
.
.
Bind is still failing with 0 argument in the socket call. I suspect that the problem is with the second argument to bind(). I do not understand what it is. Can you give me a minimal example that opens a server socket? I am sure that once I understand this, the client side will be easy.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The above solution applies to the second socket(...) call as well.
Aviad.
Aviad.
ASKER
Aviad: The bind is still failing. Can I think argument 2 is
the culprit. can you give me a simple 20 or so line example including declarations and descriptions. I do not understand what the second argument of bind is supposed to be.
the culprit. can you give me a simple 20 or so line example including declarations and descriptions. I do not understand what the second argument of bind is supposed to be.
ASKER
Edited text of question
ASKER
Edited text of question
ASKER
Aviad:
I had also submitted this problem to Microsoft. They have
directed me to a sample application called Wsock, which is included in the VC++ 5.0 distribution. It does everything I need Thankyou for you help
Gary
I had also submitted this problem to Microsoft. They have
directed me to a sample application called Wsock, which is included in the VC++ 5.0 distribution. It does everything I need Thankyou for you help
Gary
ASKER