SFML community forums
Help => Network => Topic started by: yumi_cheeseman on July 02, 2011, 12:24:20 am
-
I'm completely new to Networking, and have been attempting to send myself UDP packets, but I'm just not receiving them.
Sending to 127.0.0.1
gets to binding socket but cannot receive.
I have done it both with and without port forwarding (I assumed it wouldn't make a difference, but tried anyway).
int main ()
{
sf::IPAddress mycomp("127.0.0.1");
sf::SocketUDP mycompSocket;
char buffer[] = "hello computer";
std::cout << "sending data" << std::endl;
if (mycompSocket.Send( buffer, sizeof(buffer), sf::IPAddress::LocalHost, 4567) != sf::Socket::Done)
{
std::cout << "error sending" << std::endl;
}
std::cout << "data sent" << std::endl;
sf::SocketUDP receiver;
char thingo[128] = "";
std::size_t receivedsize;
sf::IPAddress sender;
unsigned short port;
receiver.SetBlocking(false);
if (!receiver.Bind(4567))
{
std::cout << "could not bind socket" << std::endl;
}
std::cout << "port boundedededed: " << receiver.GetPort() << std::endl;
std::cout << "socket bound" << std::endl;
if (receiver.Receive(thingo, sizeof(thingo), receivedsize, sender, port) != sf::Socket::Done)
{
std::cout << "could not receive" << std::endl;
}
std::cout << "the IP Address: " << sender << std::endl;
std::cout << "received: " << thingo << std::endl;
mycompSocket.Close();
receiver.Close();
}
Any help would be greatly appreciated!
-
http://www.sfml-dev.org/documentation/2.0/classsf_1_1UdpSocket.php
There is an example there, try that out : o
-
Try separating the send and recieve in to 2 different apps, also, fix your error handling.
if (mycompSocket.Send( buffer, sizeof(buffer), sf::IPAddress::LocalHost, 4567) != sf::Socket::Done){
std::cout << "error sending" << std::endl;
}else{
std::cout << "something went wrong" << std::endl;
}
Or better yet make a switch out of the returntype.