-
February 24th, 2006, 10:47 AM
#1
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!!!
-
February 24th, 2006, 11:18 AM
#2
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
-
February 24th, 2006, 12:04 PM
#3
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!
-
February 24th, 2006, 01:16 PM
#4
-
February 24th, 2006, 04:08 PM
#5
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
-
February 24th, 2006, 06:33 PM
#6
Re: winsock bind ()
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()?
-
February 24th, 2006, 07:03 PM
#7
Re: winsock bind ()
Good point. WSAGetLastError should have returned something like WSAENOTCONN.
Logan, are you certain that it returns 0?
Mike
-
February 25th, 2006, 04:58 AM
#8
Re: winsock bind ()
Thanks for the replies everyone.
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.
Originally Posted by runesvend
Have you initialized the Winsock API with WSAStartup()?
Yes I have.
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!!!
-
February 25th, 2006, 08:55 AM
#9
Re: winsock bind ()
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.
-
April 15th, 2010, 08:55 AM
#10
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;
-
April 15th, 2010, 09:12 AM
#11
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
-
April 17th, 2010, 11:51 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|