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

Author Topic: TCP Server, Wait() + Send  (Read 5036 times)

0 Members and 1 Guest are viewing this topic.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
TCP Server, Wait() + Send
« on: January 16, 2011, 06:36:45 pm »
Hello,
I'm currently exploring the world of network programming. I started with the Selector example in the tutorial and kind of understood it all, except maybe the Wait function. I tried extending the example to be able to send messages from the server to the client but it seems like Wait is blocking anything from running.
Is there a place where I can read more about the uses of Wait and Selector?
I get that the GetSocketReady function returns a socket that has sent something to you but can you loop the selector and send to all sockets who are not currently sending to you?

Any hints on how to extend the example to be able to send from the server when it isn't receiving?

EDIT: Seeing as this is supposed to be a 2 player "match" game, do I even need the selector? There's the server player and the client player.
I could save some headaches, right?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP Server, Wait() + Send
« Reply #1 on: January 17, 2011, 09:06:04 am »
All wait does is wait for at least one socket to be ready. Can be found on the documentation here.

This sort of problem (handling multiple TCP connections) has been discussed before in this thread
For your 2-player situation, you probably don't need a selector.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
TCP Server, Wait() + Send
« Reply #2 on: January 17, 2011, 11:11:14 am »
Quote from: "tntexplosivesltd"
All wait does is wait for at least one socket to be ready. Can be found on the documentation here.

This sort of problem (handling multiple TCP connections) has been discussed before in this thread
For your 2-player situation, you probably don't need a selector.

Yeah that's the thing, it waits for a socket to be ready. It halts the whole "while(true)" loop.
Can I not Send on those sockets who are not ready to be read? e.g.
While Waiting:
 Send to clients.
else (there is something to be received)
 Receive from clients.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP Server, Wait() + Send
« Reply #3 on: January 17, 2011, 10:50:18 pm »
If it halts the whole loop, try looking at threading. Although, it might be a bit unnecessary. Not sure if you can implement the situation you outlined above, sorry. It might be able to be done, I have no idea.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
TCP Server, Wait() + Send
« Reply #4 on: January 20, 2011, 02:48:33 pm »
Quote from: "tntexplosivesltd"
If it halts the whole loop, try looking at threading. Although, it might be a bit unnecessary. Not sure if you can implement the situation you outlined above, sorry. It might be able to be done, I have no idea.

Yeah, I've thought about threading but man, it's a headache.
I was thinking about having 4 player matches and I can't figure out how to do threading in OOP design.
Any tips?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP Server, Wait() + Send
« Reply #5 on: January 20, 2011, 10:02:13 pm »
The easiest way of implementing threads in OOP is to use threads as a base class and overload the Run() method. (This is a bit Java-ish, but of well). There is an example on the SFML Thread tutorial (look down under "Using sf::Thread as a base class")

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
TCP Server, Wait() + Send
« Reply #6 on: January 22, 2011, 09:45:10 pm »
Working with threads right now. I have the "normal main thread" and a client/serverReceiver thread.
Although, I got one problem. I need to share objects, more specifically the socket and/or the clientlist.
Socket in the client, because I send in the "main" thread and receive in a subclass, ClientReceiver, who use the same socket, obviously.
I'm not sure how to do this in a proper way without the mess of billions and billions of pointers, alright that's an exaggeration, two or three.
How do I share a vector created in my Client class with the ClientReceiver class?

pseudo code could look like this:
Code: [Select]

class Server{
    SocketTCP listener;
    ServerReceiver recv;
    vector <SocketTCP > clientlist;

    void runRegularly(){

        sendSometimes;
        recv.Launch(); //RUN THREAD
    }
}

class ServerReceiver : Thread{
    SocketTCP *socketFromServer;
    vector<SocketTCP > *clist;// PROBLEM. HOW DO I STORE A POINTER TO A VECTOR?

    func initialize(SocketTCP s, vector c ){ // PROBLEM. HOW DO I PASS A POINTER TO A VECTOR?
        scoketFromServer = s;
        clist=c;
    }

    func Launch(){
        acceptSockets(putTheSocketInThisSharedVector);
        receive();
    }
}


I'll be using mutexes around the vector and socket just to be safe :roll:
Is the structure even remotely correct? Sharing stuff between threads and classes seem like a thing you shouldn't do.
Main problem right now is the vector.
Without it I can't get the Sockets for the clients in the server. They're "in" the selector (not in the "code" snippet, I know, but that's a problem for another day).
Anyways, it's late and I'm tired as heck and I hope you understand my ramblings.

Any help would be appreciated.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP Server, Wait() + Send
« Reply #7 on: January 23, 2011, 07:53:12 am »
Have your two Classes under a namespace, and use a variable in the scope of the namespace (not in any class).
It it really that bad passing around a pointer or two?

Also, the passing a pointer stuff:
You're initialisation stuff is okay:

Code: [Select]

vector<SocketTCP > *clist;


For taking in a pointer:
Code: [Select]

func initialize(SocketTCP s, *vector c ){   // notice the "*"
  scoketFromServer = s;
  clist=c;
}


And when you call the method (initialize will be a constructor?):
Code: [Select]

ServerReceiver a(some_socket, &some_vector);  // the & means address of


I could be wrong here, but I'm pretty sure it's right. Any doubts, see http://www.cplusplus.com/doc/tutorial/pointers/

 

anything