7

I wrote a socket program to send a file and receive a string from socket In which I specified the client(wrote the ip address of client) I want to get that client address dynamically, I tried .getpeername() function but getting error

I tried .getpeername() function but getting error

#host = '10.66.227.181'   # fixed ip of one client only
client_socket = socket.socket()
host = client_socket.getpeername()
print(clientip)
port = 8000
print(host,port)
client_socket.connect(host,port)

clientip = socket.gethostname(client_socket.getpeername()) OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

Lucifer
  • 175
  • 1
  • 1
  • 8
  • 2
    Possible duplicate of [IP of client that connect to server](https://stackoverflow.com/questions/23306732/ip-of-client-that-connect-to-server) – tripleee Feb 07 '19 at 06:31
  • There is no connection and thus no peer yet at the point where you are trying to call this. – tripleee Feb 07 '19 at 06:32
  • this is a part of code as I mentioned in question. Connection is established and file is received ,now I want to send string to client and want ip address of client dynamically for which I used getpeername() but it is giving error – Lucifer Feb 07 '19 at 06:50

3 Answers3

12

If a UDP socket isn't connected there is no peer. So there can't be a peer name. And if it is connected, you already know how you connected it to.

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(host,port)
host, port = client_socket.getpeername()
Free Code
  • 273
  • 2
  • 9
2

Here is the example from the book 'Foundations of Python Network Programming' about udp sockets(both server and client side) and I think this example will be useful for you:

import argparse, socket
from datetime import datetime

MAX_BYTES = 65535

def server(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(('127.0.0.1', port))
    print('Listening at {}'.format(sock.getsockname()))
    while True:
        data, address = sock.recvfrom(MAX_BYTES)
        text = data.decode('ascii')
        print('The client at {} says {!r}'.format(address, text))
        text = 'Your data was {} bytes long'.format(len(data))
        data = text.encode('ascii')
        sock.sendto(data, address)

def client(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    text = 'The time is {}'.format(datetime.now())
    data = text.encode('ascii')
    sock.sendto(data, ('127.0.0.1', port))
    print('The OS assigned me the address {}'.format(sock.getsockname()))
    data, address = sock.recvfrom(MAX_BYTES)  # Danger! See Chapter 2
    text = data.decode('ascii')
    print('The server {} replied {!r}'.format(address, text))

if __name__ == '__main__':
    choices = {'client': client, 'server': server}
    parser = argparse.ArgumentParser(description='Send and receive UDP locally')
    parser.add_argument('role', choices=choices, help='which role to play')
    parser.add_argument('-p', metavar='PORT', type=int, default=1060,
                        help='UDP port (default 1060)')
    args = parser.parse_args()
    function = choices[args.role]
    function(args.p)

source

EntGriff
  • 775
  • 6
  • 14
  • This answer has hardcoded ip address which is not the solution of question – Lucifer Feb 07 '19 at 06:55
  • The solution of getting client ip is the same with remote host/dynamic ip too. Here is the code : https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter02/udp_remote.py – EntGriff Feb 07 '19 at 07:04
0

The above answer is going to be confusing to most people due to the "host" terminology, so here's a better one:

ip_address = my_socket.getpeername()[0]

where my_socket is the handle to your socket you have already setup and connected.

Dave
  • 1
  • 1