Solved

Python and Network Datagrams

Posted on 2014-01-16
7
428 Views
Last Modified: 2014-03-16
Hi
In my continued RTS questions, how does Python do in Datagrams? I will have  a Java datagram RTS sever, with Python clients.
Acceptable?
0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:beavoid
ID: 39799048
Or, should I do the server in Python also, to keep it contained?
0
 

Author Comment

by:beavoid
ID: 39880129
I have changed my work and will do python server and client
0
 
LVL 29

Expert Comment

by:pepr
ID: 39881879
You may find answers at Pygame.org site. Just pick one of the open source rts games and have a look http://www.pygame.org/tags/rts

There is a tutorial http://www.youtube.com/watch?v=_PfZovDNLZU, and you will probably find more.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:beavoid
ID: 39896723
Thanks.
This code below is what I gleaned from the above, and it works, at least, the datagram messaging :

The client Join loop is begun.
Just got to keep on plowing ahead.
It will be an Authoritative RTS server (respect my authority) . . . the server and client messages below.
Looks promising?
How do I specify in the client the size of the byte array in the message, 1024?
It is a coincidence that I also use 1024 as the PORT
Is 1024 the right byte[] size for an RTS? or is there a join byte[] size and an in-game byte[] size?

Server.py
import threading, socket

class Server(threading.Thread):
    def __init__(self, numclients):
        threading.Thread.__init__(self) # call parent class's __init__ 
        print('Server constructor: ')
        self.numClients=numclients
        print(self.numClients)
        print(' clients')


    
        
        
    def run(self):
        print('Server run() . . . client join:')

        clientJoinMode=True
        
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        port = 1024
        bufferSize = 1024
        s.bind(("", port))

        
        while clientJoinMode :   # CLIENT JOIN LOOP
            
            print ('JOIN waiting on port: ', port)
            
            data, addr = s.recvfrom(bufferSize)
            print ('s received from address ', addr, ': ')

            
        


serverThread=Server(2)
serverThread.start()

Open in new window


Client.py

import socket

UDP_IP = "x.x.x.x"  # server IP
UDP_PORT = 1024
MESSAGE = bytearray([1,2,3,4,5])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

   
   

Open in new window

0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 39898622
> It is a coincidence that I also use 1024 as the PORT...?

Yes, this is just a coincidence. It is not related. 1024 is just 2^10 which means rather "nice" value when using binary base (similar to 1000 when thinking in decimal).

The socket module is a wrapper for the native (C) implementation of the functionality, and the buffer size is needed for allocating the buffer in advance. The Python doc says...
Note

For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096.
(see http://docs.python.org/3/library/socket.html#module-socket)

This way, the 1024 for the buffer size is just four times smaller. It coud be enough. The same way, you can change it to 4096. But if it is for short texts, then 1024 bytes means 1024 characters (when using 8-bit charset encoding) and it is about 12 lines of text with 80 chars at each line).

For the bytearray with certain size, you can initialize it with any sequence of that size -- say with the list of integers. You can use the * operator like this:
>>> ba = bytearray([0] * 100)
>>> ba
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00')

Open in new window

It means that you give it a list with the single element (here zero) that will be repeated 100 times. (If the list was initialized with three elements, you would get three times longer list.) Then the list is convertet to the byte array, which is a mutable sequence of small ingegers. The bytes is immutable sequence of the same kind -- you can convert the bytearray to bytes:
>>> bytes(ba)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00'

Open in new window


Anyway, the socket.recvfrom() returns bytes and the address. Python allocates the space for you.
0
 

Author Comment

by:beavoid
ID: 39898707
Thanks

My major concern is the code for client and server
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question