Solved

Python - How to extract a PORT from a joining client

Posted on 2014-09-27
9
179 Views
Last Modified: 2014-10-04
Hi
My clients connect with my TCP server code, below, but I'd like to obtain the client's PORT number from the joining TCP packet.
Last time I did this, I stored the PORT number in the joining packet's data array bytes.
Is that the correct way to do it?
I've hardcoded it to 8080 now, should I just acquire the PORT number from 2 bytes in the data, not the packet object's networking properties?

 (clientsocket, address) = serversocket.accept()
    print("TCP joined from : ", address)
    
    print("(TCP recv wait:")
    data = clientsocket.recv(JOIN_BUFFER_SIZE)
    print("TCP received")

    # Analyze msg for joining clients

    print("Msg Type = ")
    print(data[0])

    if data[0] == 1 :
        print("..JOIN ")
        clients.append(ClientInfo(address, countingClients,8080)) # already had clients = [] way above, and, countingClients=0
        countingClients++

Open in new window

0
Comment
Question by:beavoid
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
In order to get the client's ephemeral port, you need to use the following code in your case:

clientHost,clientPort=clientsocket.getpeername()
print 'Client port is ', clientPort

Open in new window


HTH :)
0
 

Author Comment

by:beavoid
Comment Utility
Would this port number be the same as if I encoded the client's socket port in the helo message?
0
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
The ephemeral port is the port chosen by the operating system automatically (ephemeral = "short lived"), unless you hard code port to some specific value.  Normally clients don't do this though.

If you are querying the local port in the client, and sending this as the first message, then yes, it would be exactly the same.  I don't see why you need to do this though since there is the functionality I highlighted already provided to enable you to get the peer (which, in the context of the server, is the client) port information, unless you really do have some compelling reason to do so which you haven't elaborated on here...
0
 

Author Comment

by:beavoid
Comment Utility
What I did last time I made an RTS, in Java, and it worked,

was

- The server waits for n clients to join, then starts the game.
- The HELO  TCP message from each client contains the message type, and the four bytes of the client's UDP port.

The game starts with n clients, and the server knows the UDP port numbers of each client's UDP socket from the HELO messages.
The server begins.

Sound reasonable?

What is the Python code to acquire the port number from the client UDP socket?
Thx
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
Hi beavoid

Sorry about the delay.  It sounds like you are getting TCP and UDP confused.  They are two different protocols hence will behave differently at the application level.

You say the client has a "HELO TCP message" which contains the client's UDP port.  Your client will either establish a TCP connection or a UDP connection, there is no intermingling of the two protocols within a single connection.  You can of course have a client make both a TCP and a UDP connection, but they are different connections at the protocol level.

I would like to help you understand this further so you can complete whatever it is you are trying to do, but I need you to clarify exactly what you are trying to achieve in more detail.  Can you please do this?
0
 

Author Comment

by:beavoid
Comment Utility
For an RTS server and client,

The client sends activity packets to the server, which updates the server game-state (via UDP) - so the UDP port is for client->server communication, The Server then sends UDP back to the client to update the client game-state.
But, the TCP port is used to send periodic authority game-state packets to the client to guarantee synchronization, via TCP from server to client.
So from both sockets, I need to acquire their PORT numbers once they are connected.

Thanks
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
Comment Utility
OK, I wanted to be sure I got this right, so here is a simple server I wrote using Python.  Now, since no physical connection is established in a UDP conversation, unlike in TCP, you need to wait for the first packet to be received so you can query from whence it came.  This is what you were doing, except you were hard-coding the client's port into the first message.  You don't need to do this as Python provides APIs for you to obtain this easily, as shown below:

import socket
port=12345
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(("127.0.0.1", port))
print "Server waiting for action on port:", port
while 1:
    data, client = server.recvfrom(1024)
    print data
    print " received from ", client[1]

Open in new window


Incidentally, you can obtain the IP address of the client by using client[0].

I used netcat for Windows to simulate a client.  You can download it from http://nmap.org/dist/ncat-portable-5.59BETA1.zip, then run it using

ncat.exe 127.0.0.1 12345 -u -v

Open in new window


(Of course, if you are on Linux or similar there is an equivalent version available.)  Anything you type into the netcat window now will be directed at whatever is listening on port 12345 on 127.0.0.1, which is your server.

HTH :)
0
 

Author Closing Comment

by:beavoid
Comment Utility
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

762 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

8 Experts available now in Live!

Get 1:1 Help Now