Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python and Network Datagrams

Posted on 2014-01-16
7
Medium Priority
?
481 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

972 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