Solved

Failing TCP socket bind in WINAPI

Posted on 1997-06-20
7
693 Views
Last Modified: 2008-02-26
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_Address;      // Local address and port
number
SOCKADDR_IN Assigned_Client_Socket_Address[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_Server()
{
    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_Address.sin_family = PF_INET;
   Server_Listening_Socket_Address.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_Listening_Socket", 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_Socket_Channel],
                                    NULL);

      Assigned_Client_Socket_Address[Client_Socket_Channel].sin_family =
PF_INET;
      Assigned_Client_Socket_Address[Client_Socket_Channel].sin_port = 8100
+ Client_Socket_Channel;
      Assigned_Client_Socket[Client_Socket_Channel] = socket(PF_INET,
SOCK_STREAM, IPPROTO_TCP);

      if ( Asasigned_Client_Socket[Client_Socket_Channel] == 0
      {
         ErrorShow("Cannot establish client connection");
         return (1);
      }

      if ( Bind ( Assigned_Client_Socket[Client_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.
0
Comment
Question by:sherwingw
  • 5
  • 2
7 Comments
 

Author Comment

by:sherwingw
ID: 1251523
Edited text of question
0
 

Accepted Solution

by:
aviad earned 110 total points
ID: 1251524
You should specify 0 as the third argument of the socket(...) call creating the listening socket, instead of IPPROTO_TCP.
You only specify a protocol if your socket is a SOCK_RAW type socket, which is AFAIK not supported by MSVC++ V5.0.

In short change the line:
Server_Listening_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
to:
Server_Listening_Socket = socket(PF_INET, SOCK_STREAM, 0);

Hope this helps.
Aviad.

0
 

Expert Comment

by:aviad
ID: 1251525
The above solution applies to the second socket(...) call as well.

Aviad.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:sherwingw
ID: 1251526
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.
0
 

Author Comment

by:sherwingw
ID: 1251527
Edited text of question
0
 

Author Comment

by:sherwingw
ID: 1251528
Edited text of question
0
 

Author Comment

by:sherwingw
ID: 1251529
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Goodbye, so long 10 92
Which checksum is this? 7 136
Trouble linking program with -lcrypt 3 134
Line meaning 9 77
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now