Solved

Winsock - Can't Bind() to Socket

Posted on 1998-02-13
4
446 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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