Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Reading Perl related blogs recently, I ran into Games in Perl/Tk. At that moment I remembered I had also created a Tk based game. I searched the hard disk and web and found out it was ten years ago when I created chalaxy (I created a git repository for it few days ago). I wrote it for 5.6.1 we were running at the university.

The game had a small bug. It rarely manifested itself, so I was not able to fix it (at that time): If there were several players and the game board was larger, sometimes a player did not get all the information from the server. See poorly documented behaviour of readline() and IO::Select for a similar problem - and even for my reply about the solution I implemented: I just added a one second sleep after each line written to the server, which was usually enough for the server to read the line.

After reading the blog post about Perl games, I decided to fix the game. I spent several days searching the internet and trying different solutions, but I was not able to make it work.

There are two phases of the communication between the server and the clients:

  1. The server waits for new connections. Once accepted, each client sends its "name" to the server and waits for further instructions (phase 2). Once all the players are connected, the second phase is started.
  2. Clients send messages to the server and the server sends messages to clients. There is no order, the GUI should not freeze waiting for a message.

Most examples you can find around the Internet deal with a simpler model: the server waits for connections, once a client connects, it handles its requests, then the client is disconnected and the server continues. In my scenario, the clients do not disconnect before the game ends.

The server used IO::Select to detect incoming messages, the clients used Tk::fileevent. When I removed print and readline from the code and used syswrite and sysread instead, the code worked fine on MS Windows, but it still did not run correctly on Linux: it seemed as if sysread was blocking on Linux but not on Windows when the handle was empty. I still do not understand what the exact problem was, but I was able to work around it.

I had to write my own buffering (because I did not want to change the logic). Because of that, the fileevent was not enough and I had to add a Tk::repeat fetching from the buffer. The solution is not yet correct because I do not buffer on the writing side, but the messages are rather short and the game works.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to A Game Using TCP Sockets by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found