Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python and Network Datagrams

Posted on 2014-01-16
7
Medium Priority
?
470 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
Industry Leaders: 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!

 

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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