CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: winsock bind ()

  1. #1
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    winsock bind ()

    Iam trying to send a message from my server app to the client app. The client app is connected to the server and the client and the server run on the same machine. What Iam doing is:
    Code:
    	SOCKET s;
    	s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (s == INVALID_SOCKET)
    	{
    		return;
    	}
    	
    	sockaddr_in sendto;
    	sendto.sin_addr.s_addr = inet_addr ("127.0.0.1");
    	sendto.sin_family = AF_INET;
    	sendto.sin_port = htons (nPort);	//we only have the port address of the client
    
    	if (bind (s, (sockaddr *)&sendto, sizeof (sendto)) == SOCKET_ERROR)
    	{
    		return;
    	}
    
    	char buffer [512];
    	sprintf (buffer, "Hi!");
    	if (send (s, buffer, strlen (buffer), 0) == SOCKET_ERROR)
    	{
    		int n = WSAGetLastError ();
    		return;
    	}
    The above code doesnt work and WSAGetLastError () returns 0. Whats wrong with it? Cannt we bind a socket to a particular address and using that send a message?
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: winsock bind ()

    Use bind(..) to bind the server socket to a local ip/port. To connect to a remote ip/port, use connect(...).

    - petter

  3. #3
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: winsock bind ()

    What you are trying to do is a peer-to-peer architecture.
    If you want to have a client-server architecture, your server app has to listen() on a port and wait for a client to connect.
    For peer-to-peer architecture you have to use UDP protocol. Simply change the second parameter of your socket() call to SOCK_DGRAM.
    Please don't forget to rate users who helped you!

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: winsock bind ()

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: winsock bind ()

    I think you posted the server code. If so, then until a connection is established, there's no one to send() to.

    bind() only binds a socket to an address; it's still premature to call send() on the socket. You need to call listen() so that the socket is set to listening mode and listens for incoming connections. You need to wait for a client to call connect() and thereby trigger the incoming connection to the server. Then the server needs to call accept() to accept the connection. Only then is it OK to call send().

    Mike

  6. #6
    Join Date
    Jan 2006
    Posts
    47

    Re: winsock bind ()

    Quote Originally Posted by logan
    Code:
    	SOCKET s;
    	s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (s == INVALID_SOCKET)
    	{
    		return;
    	}
    	
    	sockaddr_in sendto;
    	sendto.sin_addr.s_addr = inet_addr ("127.0.0.1");
    	sendto.sin_family = AF_INET;
    	sendto.sin_port = htons (nPort);	//we only have the port address of the client
    
    	if (bind (s, (sockaddr *)&sendto, sizeof (sendto)) == SOCKET_ERROR)
    	{
    		return;
    	}
    
    	char buffer [512];
    	sprintf (buffer, "Hi!");
    	if (send (s, buffer, strlen (buffer), 0) == SOCKET_ERROR)
    	{
    		int n = WSAGetLastError ();
    		return;
    	}
    If WSAGetLAstError returns 0 in the above code something is wrong. I don't think send returning SOCKET_ERROR and WSAGetLastError returnning 0 is possible.

    Have you initialized the Winsock API with WSAStartup()?

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: winsock bind ()

    Good point. WSAGetLastError should have returned something like WSAENOTCONN.

    Logan, are you certain that it returns 0?

    Mike

  8. #8
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: winsock bind ()

    Thanks for the replies everyone.

    Quote Originally Posted by MikeAThon
    I think you posted the server code. If so, then until a connection is established, there's no one to send() to.
    The code is from the thread which is started by the server for a client. All clients are connected to the server. What Iam trying is that a client sends a message to the server which in turn forwards it to another client.

    I tried a lot before posting here, one of the things that I did was to save the SOCKETS of all clients in a container and then send a message to a client using it's socket and it worked fine. So, my guess was maybe bind () doesn't initialize the socket properly.

    Quote Originally Posted by runesvend
    Have you initialized the Winsock API with WSAStartup()?
    Yes I have.

    Quote Originally Posted by MikeAThon
    Logan, are you certain that it returns 0?
    Yes it does.

    I will try to post the project so that you guys can have a look.

    One more question, what will happen to the message send to a client (which is connected to the server) but is waiting for some user input (like cin or scanf)? Once the client gets that input it starts looking for a message from server (recv is executed)? What I found is that the client gets the message once the input is over but what if the user takes lots of time for the input? Will the message be lost?

    Regards.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  9. #9
    Join Date
    Jan 2006
    Posts
    47

    Re: winsock bind ()

    Quote Originally Posted by logan
    One more question, what will happen to the message send to a client (which is connected to the server) but is waiting for some user input (like cin or scanf)? Once the client gets that input it starts looking for a message from server (recv is executed)? What I found is that the client gets the message once the input is over but what if the user takes lots of time for the input? Will the message be lost?
    If I understand you correctly, you're afraid of losing data on a socket because you're aren't receiving it as soon as it arrives. I can't imagine TCP throwing out data because new data has arrived, the most likely event is that it stops receiving new incoming data because its buffers are full.

    You might want to read incoming data into your own buffer when it comes available. While the user is writing input you can recv() data into this buffer which you can then use later when needed. This would prevent your socket from blocking new incoming data because the TCP buffers are full. I'm not sure if Winsock will copy the data into its own buffers to avoid losing data, but implementing your own buffer is useful anyway IMO.

  10. #10
    Join Date
    Apr 2010
    Posts
    2

    Re: winsock bind ()

    problem is in line:
    sendto.sin_port = htons (nPort); //we only have the port address of the client

    probably nPort is Integer and its problem with conversion, you should try
    sendto.sin_port = nPort;

  11. #11
    Join Date
    Apr 2010
    Posts
    2

    Re: winsock bind ()

    wrong
    nPort should be unsigned int , propably you have integer
    code with:
    unsigned int nPort = 21111;
    sendto.sin_port = htons(nPort);
    works fine

  12. #12
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: winsock bind ()

    TCP Client and Server Interaction:

    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured