• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Smalltalkers: need help with network application...

I am attempting to develop a card game appliction (in SmallTalk) in which 4 players (who will reside on 4 seperate computers) play together. My starting questions are...

The plan is to have a server running which the four player computers will use to communicate the game plays. Is this considered a "webserver"? I have looked into using komhttpserver (a smalltalk commanche server), but not sure if this is the thing I should be using. Or should I write my own "server" appliction that just routes the plays from one of the connections to the other three connections? (I plan on keeping the plays as some type of simple text based protocol. i.e. "PLAYER1:4CLUBS>>DISCARDPILE" would mean player1 discarded the 4 of clubs)

I have been able to establish a TCP socket between two applictions, but the server will have to maintain a connection with up to 4 computers. Do I just specifiy different ports? (for my single connection I used port 8080, and the whole ports thing is confusing me. What ports are valid?)

Any help will be really really appreciated. My networking skills are minimal.

Thanks,
willmrk
0
willmrk
Asked:
willmrk
  • 3
1 Solution
 
itsmeandnobodyelseCommented:
>>>> to develop a card game appliction (in SmallTalk)

I hope you know that you are in the C++ Topic Area here ...

... and am assuming the server should be written in C++


>>>> some type of simple text based protocol

Better use a struct like that:

enum CardColor { Diamonds, Hearts, Spades, Clubs, MaxColor };
enum CardRank { Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two, MaxRank };

struct Card
{
    CardColor cc;
    CardRank  cr;
};

struct GameMsg
{
      int    playerId;
      int    action;                             // discard/draw/..
      int    numCards;                       // number of cards
      Card cards[MAXCARDS];           // cards to play
};

Now you could send/receive GameMsg buffers by clients and servers.

>>>> but the server will have to maintain a connection with up to 4 computers.

The server makes an accept call in an infinite loop. If a client connects the accept would return a new socket that could be used to make server-client communication. That normally is done in a separate thread. After creating the thread the main thread could accept the next client:

       ...
       while (true)
       {
              socket sock = accept(serversock, ..);             // wait for client connection
              if (sock != INVALID_SOCKET)       // ok
              {
                    // start thread
                    beginthread(threadFunc, ...., sock);  // the thread reads/writes from/to the socket
              }
       }

If you don't want to use threads, you need to make sockets non-blocking. Then calls like accept or recv would return immediately - in nearly all cases with an error stating there is nothing to accept or read (WSAE_WOULDBLOCK). So, in your infinite loop you first could try to accept, then for all connected clients check client sockets for messages:

       ...
       // set server socket non-blocking
       long cmd = 1;
       ioctlsocket(serversock, FIONBIO, &cmd);
       while (true)
       {
              socket sock = accept(serversock, ..);             // wait for client connection
              if (sock != INVALID_SOCKET)       // ok
              {
                   // set client socket non-blocking
                    long cmd = 1;
                    ioctlsocket(sock, FIONBIO, &cmd);
                    // add new client socker to client list
                    clientSockets.push_back(sock);
              }
              else if (GetLastError() != WSAE_WOULDBLOCK)
              {
                    errlog();
                    return;
              }

              for (int i = 0; i < clientSockets.size(); ++i)
              {
                   socket sock = clientSockets[i];
                   int nread = recv(sock, ,....);
                   if (nread > 0)
                   {
                        // handle successful read
                   }
                   else if (GetLastError() != WSAE_WOULDBLOCK)
                   {
                       errlog();
                       return;
                   }
                     
              }

              // here write jobs (server -> clients)
              // could be handled using a job queue

              // sleep a little while (10 msec)
              Sleep(10);
       }

Regards, Alex
0
 
itsmeandnobodyelseCommented:
willmrk,

why didn't you give any feedback if you are not satisfied with the answer? A C grade is only appropriate if you have cooperated but got no valid answers.

>>>> Any help will be really really appreciated.

Seems to be a lie..
0
 
willmrkAuthor Commented:
Sorry, but there is no Smalltalk group in EE so that is why I posted the question here. The C++ example wasnt much assistance.

>>>>>... and am assuming the server should be written in C++

My question was clearly for assistance witth SmallTalk (not C++).

I'm sorry that you feel ripped.
0
 
itsmeandnobodyelseCommented:
>>>> I'm sorry that you feel ripped.

If you had told that you need assistance in SmallTalk only, I would have given the advice to get points refund by CS. That is less frustrating than getting a C grade...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now