Solved

Python and Network Datagrams

Posted on 2014-01-16
7
414 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
  • 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 28

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 28

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

825 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