Solved

Winsock - Can't Bind() to Socket

Posted on 1998-02-13
4
438 Views
Last Modified: 2013-12-03
I'm trying to send() info from one server socket and then Listen() - Accept() -  Recv() the info on another server socket (same machine). Both sockets need to be bound to  PORT 80 but have different addresses. I connect() and send OK, no errors. Then I create a new socket() and try to bind() to it and the bind() fails with a WSAENOTSOCK error.
Here's the code:

// Creates a socket for listening
if ((SockListen = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
   WSACleanup();
}        

// Set the Server connection information  
ClientIn.sin_family      = AF_INET;
ClientIn.sin_port        = htons (80);
ClientIn.sin_addr.s_addr = htonl (INADDR_ANY);
   
// Binds the address and port information to the socket    
if ((bind_ID = bind (SockListen, (LPSOCKADDR) &ClientIn, sizeof(ClientIn))) == SOCKET_ERROR)
{
   closesocket (SockSend);
   closesocket (SockRecv);
   WSACleanup();
}
 
if ((list_ID = listen (SockListen, MAX_LISTEN_Q)) == SOCKET_ERROR)
{
   closesocket (SockSend);
   closesocket (SockRecv);
   WSACleanup();
}

P.S. I couldn'y get bind() to work for the connect()- send() either so I skipped it.    

 
0
Comment
Question by:squirm
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
faster earned 200 total points
ID: 1411230
You simply can not have two sockets that is on the same port.  Why do you need both to be on 80?  The one listening probably has to be (I guess it is an http server), but the one connect to another server can be on any port.
0
 
LVL 7

Expert Comment

by:faster
ID: 1411231
If your machine is multi-homing, then you should try specify the IP address you want in each bind(), rather than using INADDR_ANY.
0
 

Author Comment

by:squirm
ID: 1411232
So I listen() on port 80 but connect() on any port.  How do I send() information to a web server and then wait for the answer? Don't I have to send() through port 80 for the web server to receive the information?  Also, I didn't bind() before I connnected() but I did connect to 80 and I can send() without an error.  Should I bind()first, is there a benefit?  
0
 
LVL 7

Expert Comment

by:faster
ID: 1411233
So you need to connect to another web server.  The fact that the web server is listening on port 80 does not mean that you have to talk with it on port 80, you can on any port.  Normally, you should bind first, and most of the time when bind(), you specify the port to 0, this means that you don't care which port it is and the system will then select a port for you.

Of course, for your own web server, you have to bind it to 80, that is because it is a server, and without having a fixed port, the clients will not know where to find your server.


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

18 Experts available now in Live!

Get 1:1 Help Now