Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Issues with receiving  (Read 3054 times)

0 Members and 1 Guest are viewing this topic.

Joshu145

  • Newbie
  • *
  • Posts: 10
    • View Profile
Issues with receiving
« on: April 18, 2016, 05:52:56 am »
I'm having trouble figuring out why my receiving functions won't work at all.

http://pastebin.com/8aBQDrwt

The code is all in one file. It's pretty sloppy code at the moment but i was just trying to throw something together quick to get an idea of how the networking works. I understand that the receiving function may block usage to the socket1 when opened in another thread. Also when the program is supposed to output "client connected" it does so on the person sending the connection request not the person being connected to. any suggestions? I didn't want to use the receiver in the same loop as the sending function because i didn't want the program to pause until someone sends a message.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11045
    • View Profile
    • development blog
    • Email
Re: Issues with receiving
« Reply #1 on: April 18, 2016, 09:06:51 am »
"Won't work at all" is not a problem description. What is your code trying to do? What do you expect to happen? What does actually happen?

I only quickly opened the code and wonder what this should do:

    mutex1.lock();
    if(listenertcp.accept(socket1) == sf::Socket::Done)
    {
        mutex1.lock();

Calling lock on the same mutex twice, seems wrong.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Godsend72

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Issues with receiving
« Reply #2 on: April 18, 2016, 09:40:36 am »
I think the problem is that you are listening to incoming connections in client. Split the code in to server and client. In my opinion, in this case threads are overkill, put socket in non-blocking mode. Also in receive function, check if "socket1.receive(...) == sf::Socket::Done", because currently it is printing empty string all the time. I have modified your code a little. http://pastebin.com/DwqDjnE8 

Joshu145

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Issues with receiving
« Reply #3 on: April 18, 2016, 03:45:37 pm »
I think the problem is that you are listening to incoming connections in client. Split the code in to server and client. In my opinion, in this case threads are overkill, put socket in non-blocking mode. Also in receive function, check if "socket1.receive(...) == sf::Socket::Done", because currently it is printing empty string all the time. I have modified your code a little. http://pastebin.com/DwqDjnE8

The reason i didn't put an if statement is because i thought that receiving was blocking. so it wouldn't continue running until it had actually received something.

Godsend72

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Issues with receiving
« Reply #4 on: April 18, 2016, 04:26:55 pm »
Even if that line would block the program until it receives something, you created another thread for it. So your program would still run on the first socket.

 

anything