I am trying to implement socket programming and want to configure the communication port number for both the server and client to a specific port. I specify the same port number on both the the client and server side but still when the program run's it takes a random port number. How do I fix the port number/make it static?
Server Side Code
import socket
s=socket.socket()
port=12345
s.bind(("192.168.0.111",port))
s.listen(5)
while True:
c, addr = s.accept()
print("got connection from ",addr)
sendingMessage = "Thank you for connecting"
c.send(bytes(sendingMessage, 'UTF-8'))
data = c.recv(16)
receivedData=data.decode("utf-8","ignore")
print (receivedData)
c.close()
if receivedData=="stop":
break
Client Side Code
import socket
port=12345
s=socket.socket()
s.connect(("192.168.43.111",port))
sendingMessage = input("Enter your message : ")
s.send(bytes(sendingMessage, 'UTF-8'))
data = s.recv(32)
receivedData=data.decode("utf-8","ignore")
print (receivedData)
s.close
If you want the client side to also use port 12345, you must also bind the client side port number. The port number given in the s.connect is the remote port to which you're connecting. IOW, your code should look something like this in the client:
s = socket.socket()
s.bind(('', port))
s.connect(("192.168.43.111", port))
You can also specify an IP address in the bind but typically you don't need to as the local IP address will be established by the route to the remote host.
Related
I'm new to this whole shazam and I'm a little confused. I want to have a server receive data on my computer, and a friend send data on his own computer. The code for my server is as follows:
import socket
HOST = 'HOST'
PORT = PORT
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
I've blanked out the host ip and the port but I'm not really sure which one I'm supposed to be using for either tbh. The client code goes as follows
import socket
HOST = 'HOST'
PORT = PORT
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
So my server receives it only when I run the client, not my friend. My question is what IP and ports am i supposed to use? Where can i find these numbers? Why does it only work when I run the client and how can I fix this? And if anybody can direct me to some resources about this topic I don't know what to search up :(
Thanks in advance!
The server should bind to the IP address of whatever interface it expects to receive traffic on. If it might receive traffic on multiple interfaces, you could bind to 0.0.0.0, which means 'all interfaces'. Whatever IP you decide on is what you should set for the server HOST value. For the server port, it could be a specific port or any port (port 0). Just be aware that the client will need to know which port the server is listening on.
The client should connect to the IP address or hostname and port of your server whose address is publicly accessible. This really depends on the network setup.
I suggest having your client connect to the same network as your server and trying again. If it doesn't work, make sure you're server is listening on 0.0.0.0.
If you are on different networks, these networks need to be bridged in some way.
I'm trying to make a chat app in Python and I'm having some trouble.
I made a server on which I can connect successfully by using the local IP address. However, when I try to connect to it on an another device with my public IP address, there seems to be a timeout, no errors occur and it's continuously trying to connect.
Edit: I've already set up port-forwarding for my IPv4 address. And the client is using the public IP.
server.py:
import socket
s = socket.socket()
host = socket.gethostbyname(socket.gethostname())
port = 2000
s.bind((host, port))
print("Server started, waiting for incoming connections")
s.listen(5)
connection, address = s.accept()
print("New connection from", address)
while True:
data = connection.recv(1024).decode()
print("received:", data)
ret = data + "+++++++"
connection.send(ret.encode())
client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = #my public ip address from whatsmyip.com
port = 2000
s.connect((host, port))
print("Connected.")
while True:
message = input("msg: ")
s.send(message.encode())
data = s.recv(1024).decode()
print(data)
Well, first of all, is your server in a network with other devices? If you have a router there, the IP you see in whatsmyip.com is the router's, not your computer's, IP. So you'd be trying to connect to it.
You can check that with the command netstat.
I want to create a small TCP server that takes incoming TCP connections from a device that is hooked up via Ethernet to my computer.
The physical port for that has the IP 192.168.1.100 statically assigned to it.
The scripts I use as a client and server are listed at the bottom.
The setup works if I want to send messages between the python scripts. However, I am unable to receive anything from the external device (screenshot from Wireshark capture below). From what I have read I can define an interface to listen to by defining its IP. So I defined the IP of the interface as the host variable. However, I do not receive anything in my script but the messages sent by the other script. I had a similar situation already here on stackoverflow. I thought that defining the correct IP as the host would resolve this issue but it did not.
I am also having a hard time capturing the traffic between the two scripts with Wireshark at all. They did not show up anywhere.
I need to pick up these connections on the eth0 interface with the static IP 192.168.1.100:
tcp_server.py
import socket
# create a socket object
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
# host = socket.gethostname()
host = "192.168.1.100"
port = 9002
# bind to the port
serverSocket.bind((host, port))
# queue up to 5 requests
serverSocket.listen(5)
while True:
# establish a connection
clientSocket, addr = serverSocket.accept()
print("Got a connection from %s" % str(addr))
msg = 'Thank you for connecting' + "\r\n"
clientSocket.send(msg.encode('ascii'))
clientSocket.close()
and this as a client:
tcp_client.py
import socket
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
# host = socket.gethostname()
host = "192.168.1.100"
port = 9002
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes
msg = s.recv(1024)
s.close()
print(msg.decode('ascii'))
I'm trying to make simple Client-Server program but it's only working when I'm running both scripts on same computer, when moving it to other computer - It does not make connection at all.
Server:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host="0.0.0.0" # Bind with everyone
port = 13254 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close() # Close the connection
Client:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host = "192.168.10.4" # Server Ip
port = 13254 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
Can you please help me to figure the problem?
Thanks.
As it works when you run both scripts on the server it is likely your port is closed.
Verify your port is open. Go to this website: http://www.canyouseeme.org/
Enter your port and IP. It is highly likely your port is closed. This will verify that. Assuming your port is open you need to look into your firewall settings as reccomended by dmg in the comment above. Then, it is no longer a python issue!
I made a simple server and a simple client with socket module in python.
server:
# server.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send(b'Thank you for your connecting')
c.close()
and client:
#client.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host, port))
print(s.recv(1024))
I started the server and then started 4 clients and got output in server's console as below:
Got connection from ('192.168.0.99', 49170)
Got connection from ('192.168.0.99', 49171)
Got connection from ('192.168.0.99', 49172)
Got connection from ('192.168.0.99', 49173)
what is the second part in the tuple?
From the socket documentation:
A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.
So the second value is the port number used by the client side for the connection. When a TCP/IP connection is established, the client picks an outgoing port number to communicate with the server; the server return packets are to be addressed to that port number.
Quote from python documentation:
socket.accept()
Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
What address is you can find in same doc from words "Socket addresses are represented as follows".