Sending data using UDP

I've been trying to send data through UDP from the server to the client, but for some reason, it returns a -1 error (10057 error - socket not connected).
How ever, just before I'm sending a packet, I'm also recieving a packet..

Here is my code (Server side) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
SOCKET ServerOn() // Getting the server socket
{
	SOCKET ListenSocket;
	WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR)
	{
        exit(0);
    }

    // Create a SOCKET for listening for
    // incoming connection requests.
    ListenSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (ListenSocket == INVALID_SOCKET) 
	{
        WSACleanup();
		exit(1);
    }

    // The sockaddr_in structure specifies the address family,
    // IP address, and port for the socket that is being bound.
    sockaddr_in service;
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr("0.0.0.0");
    service.sin_port = htons(2583);

    if (bind(ListenSocket,(SOCKADDR *) & service, sizeof (service)) == SOCKET_ERROR) 
	{
        closesocket(ListenSocket);
        WSACleanup();
        exit(2);
    }

	return ListenSocket;
}


and here is my code with the sockets :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
	SOCKET ClientSocket = ServerOn();

	sockaddr_in service;
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr("0.0.0.0");
    service.sin_port = htons(2583);

	char *recvbuff = new char[1000];
	std::string data;
	int size = 0;

	while(true)
	{
		recv(ClientSocket, recvbuff, strlen(recvbuff), NULL);

		std::cout << "New Connection!" << std::endl;

		* Some small calculations that takes o(1) time *

                 * Theres some data stored in std::string data
                  send(ClientSocket, data.c_str(), data.length(), NULL); // Sending the data
		iResult = WSAGetLastError(); // iResult turns 10057
                 Which means the socket isnt connected.		
	}
}


So, what is my problem in the code?
I used to have another recv function after the line of the send function, so it couldent be that I had lost the connection. 2 recv functions one after one works fine, but the send one allways fails and returns -1... Why is that?
This is the server side BTW.

Thanks!
The server can bind to INADDR_ANY, to indicate that it's listening to all addresses, but you can't send to that address.

You need to send to actual IP address of the server. If it's on the same computer, try a localhost address like 127.0.0.1.
If it isnt on the same computer, how can I send to the connected target ?
(I got the SOCKET of "ClientSocket", how can I get It's IP and I guess I need to use SendTo right ?)
If it isnt on the same computer, how can I send to the connected target ?

Use it's IP address.
I meant, how can I retrieve the IP adress?
On Windows, run ipconfig /all on the server.

That will show you the ip addresses of all interfaces. Usually the correct interface is easy to spot.

Once you've identified the address, from the client, run ping <server ip address> to prove that there's a route from the client to the server.
You dont understand.
As a server ,I do not know who will connect to me, so I need to figure out their IP adress from ClientSocket, to send data to them, right ?
In a client-server network, the server listens for connections. When using the sockets library, a server must bind to an address/port endpoint. This allows clients to find the server by communicating with that address/port.

So, in your example, you created a UDP server and you've bound to address INADDR_ANY / port 2583. It is not uncommon for a computer to have multiple network interfaces; for example, the loopback adapter, wifi and an ethernet adapter. You server will listen for connections on all these interfaces, and port 2583.

Now, how do you reach it?

Let's assume the wifi interface is active and connected to a home router and was assigned address 192.168.1.3.

Let's assume that the client computer is also connected to the same home router.

Then the client computer ought to be able to ping the computer running your server program. So your should be able to run:
ping 192.168.1.3

The ping proves that your network is working and the client can reach the server.

Then, run your client. The client must used sendto/recvfrom rather than send/recv. The address is sends to is 192.168.1.3:2583.

It makes no sense for the client to:
1. use send/recv
2. use address 0.0.0.0:2583

You can't write network software without learning a little about networking. After all, if you don't understand it, how can you create instructions for it?
Thank you so much!
I've just took a look at the sendto and recvfrom functionds at MSDN, and had an idea maybe I need to use em instead of the normal send/recv functions

(I thought about that JUST before I checked ur last comment! I swear haha)

But now that you're saying it, it clears it..

Thank you so much!
Topic archived. No new replies allowed.