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

Author Topic: I have a little problem with socket  (Read 2239 times)

0 Members and 1 Guest are viewing this topic.

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
I have a little problem with socket
« on: June 26, 2012, 09:16:23 pm »
my problem is that the server is stopped, put the code here.
I do not want the server to stop
Video of the problem
http://www.youtube.com/watch?v=CA4zsZoiV3I&feature=youtu.be
SERVER
#include <iostream>
#include <SFML/Network.hpp>
using namespace std;
// Puerto 25569

int main(int argc,char *argv[]){
    sf::SocketTCP Conexion;
    sf::SocketTCP Client;
    sf::IPAddress ClientAddress;
    sf::Packet Paquete;
    int t;
    Conexion.Listen(25569);
    Conexion.SetBlocking(false);
    for(int i=0;;i++){
        for(int j=0;j<100000000;j++);//this is for do a little pause
        if(Client.Receive(Paquete)!=sf::Socket::Done){
           Conexion.Accept(Client,&ClientAddress);
        }
        else {
            Paquete  >> t;
            cout << "Client: " <<  t<< endl;
            Conexion.Close();
        }
        cout << i<< "-juan es gilipollas"<<endl;
    }
    return 0;
}

CLIENT
#include <iostream>
#include <SFML/Network.hpp>
#include <cstdio>
using namespace std;
// Puerto 25569
struct Suma{
    int n1;
    int n2;
};
int main(int argc,char *argv[]){
    sf::SocketTCP Conexion;
    sf::Packet Paquete;
    while(Conexion.Connect(25569,sf::IPAddress("localhost")) !=sf::Socket::Done)cout << "Buscando servidor..."<<endl;
    cout << "Servidor encontrado." <<endl;
    //char Datos[128];
    Suma s;
    while(1){
        cout << "Escribe el primer numero: ";
        cin >> s.n2;
        Paquete  << s.n2;
        Conexion.Connect(25569,sf::IPAddress("localhost"));
        if(Conexion.Send(Paquete) != sf::Socket::Done)cout << "no se pudo mandar el mensaje";
        Paquete.Clear();
    }
    return 0;

}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I have a little problem with socket
« Reply #1 on: June 26, 2012, 09:20:21 pm »
This is not how listener sockets work. They are only meant to receive new connections; data is then exchanged on the connected socket.
Laurent Gomila - SFML developer

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: I have a little problem with socket
« Reply #2 on: June 26, 2012, 09:38:02 pm »
Laurent please could you tell me how to do it without the server to stop?
ty

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: I have a little problem with socket
« Reply #3 on: June 26, 2012, 09:56:36 pm »
You should start with the "Socket" example of the SFML SDK, which is a complete (and working) example of socket communication.

You should also switch to SFML 2 if you can.
Laurent Gomila - SFML developer

exafi

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: I have a little problem with socket
« Reply #4 on: June 26, 2012, 11:40:03 pm »
ok thanks i will try do it

 

anything