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

Author Topic: idea needed  (Read 8586 times)

0 Members and 5 Guests are viewing this topic.

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
idea needed
« on: April 18, 2015, 10:20:32 am »
Hello !

I need to do a server, but ideas are not comming ...
The server must accept new connections and pair clients 2 by 2.
After the server understands that those 2 clients must be paired, server accept package from one client and send to the other one.

Let's say that client1 connects to my server and client2 connects to my server.
They all send a string so my server could recognise them.Based on that string, my server knows that if client1 sends a pack, then the server must send that pack to client2, and if client2 sends a pack, then it must be send to client1.

Anyone have some ideas on how to build this thing ?
Thank you !  ;)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: idea needed
« Reply #1 on: April 18, 2015, 11:14:23 am »
By programming it? ::)

If you have no idea about networking, go learn it. We won't "verbally" program for you. You already have the idea all that it's missing is you writing code for it.
If you get stuck at something specific, you can always come back and ask.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #2 on: April 18, 2015, 12:52:23 pm »
I can feel your sarcasm... and understand that you must answer alot of dump question and you must be tired, but I also think that this can be a nice topic and everyone can come up with some ideas...it can be as simple as that: How would you do this ?

I remind that this is a post in General discussions, not in Help !
Come on, be friendly ; )

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: idea needed
« Reply #3 on: April 18, 2015, 01:40:00 pm »
The thing is that you know exactly what you want to do, so what do you expect from us? Now go and write it, that should be the funniest part if you're a programmer. If you don't know where to start then take a look at the SFML resources: examples, documentation, tutorials, wiki, forum, ...

Even the friendliest guy here won't know what else to say.
« Last Edit: April 18, 2015, 01:41:50 pm by Laurent »
Laurent Gomila - SFML developer

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #4 on: April 18, 2015, 07:18:57 pm »
I have write this code :


#include <windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    sf::RenderWindow window( sf::VideoMode( 400, 200), "Server" );

   sf::TcpListener listener ;

   sf::TcpSocket peer[2] ;
   peer[0].setBlocking( false );
   peer[1].setBlocking( false );
   int max_peers = 0 ;

   sf::Packet peer_pack ;

   sf::Font font ;
   font.loadFromFile( "Sansation.ttf" );
   sf::Text text ;
   text.setFont( font );
   text.setCharacterSize( 20 );
   text.setColor( sf::Color::White );
   text.setString( "Waiting for peers..." );

   while( window.isOpen( ))
   {
      sf::Event event ;
      window.pollEvent( event );

      if( event.type == sf::Event::Closed )
      {
         window.close( );
      }
      if( max_peers < 2 )
      {
         listener.listen( 31111 );

         if( listener.accept( peer[max_peers]) == sf::Socket::Done )
         {
            text.setString( "Peers conected - " + std::to_string( max_peers+1 ));
            max_peers++ ;
         }
      }
      if( peer[0].receive( peer_pack ) == sf::Socket::Done )
      {
         text.setString( "First peer has send a pack." );
         peer[1].send( peer_pack );
         peer_pack.clear( );
         text.setString( "A pack to the second peer has been send." );
      }
      if( peer[1].receive( peer_pack ) == sf::Socket::Done )
      {
         text.setString( "Second peer has send a pack." );
         peer[0].send( peer_pack );
         peer_pack.clear( );
         text.setString( "A pack to the first peer has been send." );
      }
      window.clear( );
      window.draw( text );
      window.display( );
   }
    return 0 ;
}

The server accept 2 peers, and if one sends a pack, it will be redirected to the other one.
So what do you think ? :)
« Last Edit: April 18, 2015, 08:38:08 pm by mihaineken »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: idea needed
« Reply #5 on: April 18, 2015, 10:24:08 pm »
Why are you writing it as windows specific?

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #6 on: April 18, 2015, 11:06:57 pm »
Just because I am using Visual Studio and the project is Win32Project  ;)

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: idea needed
« Reply #7 on: April 19, 2015, 03:55:27 pm »
Just listen to them. This is not something to have a happy debate on. Making a robust client-server architecture for a game is a well defined practice, that you can learn from internet documentation or books in a straightforward way.

Don't cheap on learning things the right way; Go somewhere and learn how to do this, then do it. You'll notice its not subject for argumentation and there is no point in this topic other than getting people to subtly tell you how to do it. Sure, there are different programmers with different personal preferences on how they design the program, but its all around the same principles.

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #8 on: April 19, 2015, 10:30:07 pm »
Guys please don't take this the wrong way but seems like every time someone ask something is a general thing for you guys to send them away: "Go an learn that !", "Go and search that !".

We are newbees we understand nothing !
Now I am shure that you are great programmers and know alot of things and you study hard.But please don't send us away.

I like programming but is not my job, I do this in my spear time, and who knows.... maybe this thing will bring me someware.

I started this post not because I want you to programm something for me, but I was hopping that someone will come up with some ideea on how to improve what I started.This topic is not that much for you (the big guys that know programming) as it is for the guys like mine who bearly understand something...

I do want that another guy like me will come and say : "How about if we do... "some thing"...it will improve. "
And we start to creat something.This is learning my friends !
Please don't understand me wrong !

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: idea needed
« Reply #9 on: April 20, 2015, 01:23:21 am »
You have your heart in the right place, but you need to understand that people here focus 99% of their efforts into answering sfml related questions well. Since your topic is more of a general programming thing, people disregard it a lot more.

Maybe if you directed topics like this into more suitable communities, you'd get more help. I am sure no one here would deny helping you with SFML specific things, but there's a line where the sfml forum stops being the best place to ask something sfml unrelated.

Don't get me wrong, it doesn't offend or bother anyone, but as you see, the answers are not what you'd expect.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: idea needed
« Reply #10 on: April 20, 2015, 09:50:05 pm »
Well, you were nice enough to post a piece of code and ask for improvements, so I'll at least comment on it.

I didn't test your code, but assuming what you did is working, I can see some problems.

First and foremost, if only one peer connects and sends a pack before the second peer connects, the server will try to send the pack to some uninitialized peer. A simple check on the number of peers before taking packages should fix it.

Then there is the issue that your code only works for 2 peers. Once 2 clients are connected the server stops listening for new clients. Assuming your code is meant for a multiplayer game server, that is not very good.

When you fix those problems you might have to deal with disconnecting peers, etc.

After that I guess it all deppends on what you want to accomplish. You might want to create lobbys to match people instead of simply matching them by the order of connection, for example.

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #11 on: April 25, 2015, 12:47:22 am »
Thank you so much for your reply.
Actualy I did some changes but didn't post ...

#include <windows.h>
#include <list>

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    sf::RenderWindow window( sf::VideoMode( 400, 200), "Server" );

   sf::TcpListener listener ;
   listener.setBlocking( false);

   sf::TcpSocket peer[2] ;
   peer[0].setBlocking( false );
   peer[1].setBlocking( false );
   int max_peers = 0 ;

   std::list <sf::Packet> first_buffer_pack ;
   std::list <sf::Packet> second_buffer_pack ;
   sf::Packet peer_pack ;

   sf::Font font ;
   font.loadFromFile( "Sansation.ttf" );
   sf::Text text ;
   text.setFont( font );
   text.setCharacterSize( 20 );
   text.setColor( sf::Color::White );
   text.setString( "Waiting for peers..." );

   while( window.isOpen( ))
   {
      sf::Event event ;
      window.pollEvent( event );

      if( event.type == sf::Event::Closed )
      {
         window.close( );
      }
      if( max_peers < 2 )
      {
         listener.listen( 31111 );

         if( listener.accept( peer[max_peers]) == sf::Socket::Done )
         {
            text.setString( "Peers conected - " + std::to_string( max_peers+1 ));
            max_peers++ ;
         }
      }
      if( peer[0].receive( peer_pack ) == sf::Socket::Done )
      {
         text.setString( "First peer has send a pack." );
         first_buffer_pack.push_back( peer_pack );
         peer_pack.clear( );
      }
      if( first_buffer_pack.size( ) > 0 && max_peers == 2 )
      {
         if( peer[1].send( first_buffer_pack.front( )) == sf::Socket::Done )
         {
            first_buffer_pack.pop_front( );
            text.setString( "A pack to the second peer has been send." );
         }
      }
      if( peer[1].receive( peer_pack ) == sf::Socket::Done )
      {
         text.setString( "Second peer has send a pack." );
         second_buffer_pack.push_back( peer_pack );
         peer_pack.clear( );
      }
      if( second_buffer_pack.size( ) > 0 && max_peers == 2 )
      {
         if( peer[0].send( second_buffer_pack.front( )) == sf::Socket::Done )
         {
            second_buffer_pack.pop_front( );
            text.setString( "A pack to the first peer has been send." );
         }
      }
      window.clear( );
      window.draw( text );
      window.display( );
   }
    return 0 ;
}


Now the server accept 2 connections and if one peer sends a pack it will remain in the buffer until the next peer will connect.Next I will handle disconnections and after was thinking to put all this code in some class and that way the server will handle multiple peers.As I said I do this for fun but I will return with some updates ;)
Thank you so much !

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: idea needed
« Reply #12 on: April 25, 2015, 12:48:24 am »
This forum offers code tags, please make use of them.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: idea needed
« Reply #13 on: April 25, 2015, 10:32:48 am »
I'm sorry, how do I do that ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: idea needed
« Reply #14 on: April 25, 2015, 11:53:45 am »
To the very right, above the text field, there's a dropdown menu named "Code". There, you can choose the programming language.

Alternatively, you can surround the code manually with  [code=cpp] [/code]  tags.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything