?
Solved

Winsock - Can't Bind() to Socket

Posted on 1998-02-13
4
Medium Priority
?
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
faster earned 800 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

801 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