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

Author Topic: Problem with Listener.accept  (Read 2774 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
Problem with Listener.accept
« on: December 08, 2016, 04:59:34 am »
There seems to be a problem with Listener.accept(socket). The program does not seem to execute past the conditional statement checking if the socket is accepted. For example, the program does not execute the
Code: [Select]
system("Pause") command.

Code: [Select]
int main()
{
 const unsigned ServerPort = 139;
 sf::TcpListener Listener;

 if(Listener.listen(ServerPort) != sf::Socket::Done)
  cout << "Error! an error occurred" << endl;
 else
  cout << "Success! Listening at " << ServerPort << endl;

 sf::TcpSocket socket;
 if(Listener.accept(socket) != sf::Socket::Done)       <-- Program just pauses
  cout << "Error! Could not set socket" << endl;

 system("Pause");     <-- Does not execute
 return 0;
}

What could cause this problem?
« Last Edit: December 08, 2016, 11:49:27 am by Student555 »

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Problem with Listener.accept
« Reply #1 on: December 08, 2016, 07:35:12 am »

http://www.sfml-dev.org/tutorials/2.4/network-socket.php

"The accept function blocks until a connection attempt arrives (unless the socket is configured as non-blocking)."



Student555

  • Guest
Re: Problem with Listener.accept
« Reply #2 on: December 08, 2016, 11:48:39 am »
The above code is the server code on a different machine. In the server code I added the following line
Code: [Select]
socket.setBlocking(true);
Below is the client code on my machine
Code: [Select]
const unsigned ServerPort = 139;

sf::IpAddress ipAddress = sf::IpAddress::getLocalAddress();
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("192.168.1.251", ServerPort);

cout << ipAddress.getLocalAddress().toString() << endl;

if(status != sf::Socket::Done)
cout << "Error! Client did not connect to server!" << endl;
else
cout << "Success! Server " << socket.getRemoteAddress().toString() << endl;

Still not getting a response on the server code.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Problem with Listener.accept
« Reply #3 on: December 08, 2016, 12:19:00 pm »
Well does the client connect successfully?

Use Wireshark to see what your traffic is really doing.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: Problem with Listener.accept
« Reply #4 on: December 09, 2016, 09:33:55 pm »
It turns out all I needed to do was change my port number. Though, I'm curious as to what ports are best for networking for an application. I've heard that anything past 2000 is good. Is that true?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Listener.accept
« Reply #5 on: December 10, 2016, 10:06:22 am »
Consider that anything < 1024 is reserved. Use high numbers to be safe (like 50000).
Laurent Gomila - SFML developer

 

anything