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

x
?
Solved

Python - How to extract a PORT from a joining client

Posted on 2014-09-27
9
Medium Priority
?
207 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
[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
  • 4
9 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 40348557
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
ID: 40348570
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
ID: 40348737
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

Author Comment

by:beavoid
ID: 40349168
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 40356543
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
ID: 40360921
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 2000 total points
ID: 40361520
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
ID: 40361533
thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

704 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