Solved

Python and Network Datagrams

Posted on 2014-01-16
7
394 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
Comment Utility
Or, should I do the server in Python also, to keep it contained?
0
 

Author Comment

by:beavoid
Comment Utility
I have changed my work and will do python server and client
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:beavoid
Comment Utility
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
Comment Utility
> 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
Comment Utility
Thanks

My major concern is the code for client and server
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now