Solved

Python - How to extract a PORT from a joining client

Posted on 2014-09-27
9
194 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How To Loop - Python 19 104
Python filter object attributes 2 54
error with datetime python 1 57
troubleshoot a python script 8 113
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

734 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