I am new to socket programming. I was able to communicate between 2 system with socket communication. But suddenly started facing this issue. Connection is getting established and accepted but when cursor reaches conn.receive(1024) it is stuck at this point. I am not getting neither data nor any error. Can anyone suggest what is going wrong? What are the possible system settings might affect the code? Not able to figure out whether issue is at server or client side.
import socket
HOST = ''
PORT = 2048
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
s.bind((HOST, PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')
while True:
conn, addr = s.accept()
# Retrieve message size
data = conn.recv(1024).decode()
I figured out the answer!!!!!!!. Since the client side was in java it was seen that flush() command was not used. Once flush() command added it started working.
Thanks for the hint #mama 🙂
Related
I read some of the Sockets documentation and watched some videos on the subject, I tried to make the sockets connection. First, I tried it with my internal IP, ran it on different endpoints and everything worked. I tried it with my external IP and it failed, I tried to run the same application on different devices, I ran server.py on my PC, and I ran client.py on my cell phone. No error message appeared, but the connection did not start.
Server code:
import socket
s = socket.socket()
print("Socket successfully created")
port = 12345
s.bind(('Meu IP', port))
print("socket binded to %s" %(port))
s.listen(5)
print ("socket is listening")
while True:
c, addr = s.accept()
print('Got connection from', addr )
c.send('Thank you for connecting'.encode())
c.close()
Client code:
# Import socket module
import socket
s = socket.socket()
port = 12345
s.connect(('IP', port))
print (s.recv(1024).decode())
s.close()
I think the problem is the IP, I think I should put the valid external IP instead of the internal IP, but I don't know how to do that, because when I try this, an error appears. I just need to know how to create a socket between two machines on different networks. I have been looking for a solution to this problem for a long time. If any of you can help me, I appreciate it.
I am connecting to a socket to read incoming data from an ADS-B box.
Everything works fine when data is coming in, but whenever there is no data, I get the 'Not Responding' message appear, which I presume is because of the while True statement. My understanding of this is it is because it is waiting for something to happen. I even tried it without a while statement, but the same thing happens.
How can I stop the Not Responding message (along with the mouse hourglass) from appearing please?
Oh yes it is on Win10 with Python 3.8.3
My code is as follows:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 30003 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
data = s.recv(1024)
Thanks
In trying to familiarize myself with the socket library, I have a simple server and client setup. Basically I've stumbled through and am able to set up connection and get the server and client to talk to each other. To make it more interactive, I have client.py able to send text through the command line. Everything appears to be working properly (with the exception of the server side tearing down connection properly if client input is blank), if I type a message from the client side, it spits it right back out to me. In this example, I have it set up for the server side to print the text as well. What I noticed was, that the server side doesn't alway 'register' what it being sent from the client. I am trying to figure out why this is the case. For being a test, it doesn't really affect anything, I just can't figure out what is taking place behind the scenes.
EDIT:
Actually, after playing around with it for a bit, it appears every other message is being printed out to the server console. I've still yet to figure out why this is the case
Server side:
#server.py
import socket
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.bind(('127.0.0.1',5000))
ss.listen(5)
while True:
conn, addr = ss.accept()
with conn:
print ('Connected by', addr)
while True:
data = conn.recv(4096)
print (data)
if not data:
print ("nothing received from client")
ss.close()
break
Client side:
#client.py
import socket
server = 'localhost'
port = 5000
s = socket. socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 5000))
s.send(bytes(b'Client has connected'))
while True:
msg = input()
s.send(bytes(input(msg),'utf-8'))
if not msg:
print ("Server should tear down connection...")
# s.close()
break
In sockets you there are no methods __exit__ implemented, so you can't use the with conn:
you need to remove this part of code.
As context to my question, I am a computing student getting started with Python for the first time. Before this, I've worked mostly with Java and I am most comfortable with Java conventions and practices right now.
Background
An assignment for socket programming asks that we send strings between a server and client locally on the machine. We are provided sample (Python 2) code that instantiates a server and client. Outside of the context of the assignment, I wanted to create a version of this code that also runs in Python 3, but I was having problems getting the client to work the same in Python 3.
Changing server and client
Originally, the server required little changes and I was able to get it working. My code for the server is as follows:
#!/usr/bin/python3
import socket
HOST=''
PORT=5870
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
conn, addr = sock.accept()
print('Connected by ', addr)
conn.sendto("Welcome to the server!", (HOST, PORT))
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
I'm not able to convert the client side to code that runs and functions within Python 3. I've tried digging deeper into the issue, but other online resources are not helpful for me (or at least, at my experience level). My server code is as follows.
#!/usr/bin/python3
import socket
HOST='127.0.0.1'
PORT=5870
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
data = sock.recv(1024)
print('Server sent', data)
sock.sendto("Hello world".encode(), (HOST, PORT))
data = sock.recv(1024)
print("Server sent", data)
sock.sendto("This is the second message".encode(), (HOST, PORT))
data = sock.recv(1024)
print('Server sent ', data)
sock.close()
The actual problem
Originally, this code for both the server and client used sendall() instead of sendto(), but I changed it after getting a TypeError in the client and reading this question. I'm still not exactly sure why this works or why I have to do this (although I would appreciate an explanation).
Now, when I run the client code, I'll get the same TypeError on the server even when I'm using sendto(), but I'm not sure how to resolve this problem in Python 3. The stacktrace I receive for the server as follows (I get a broken pipe on the client):
$ python3 mail_server.py
Connected by ('127.0.0.1', 41866)
Traceback (most recent call last):
File "mail_server.py", line 14, in <module>
conn.sendto("Welcome to the server!", (HOST, PORT))
TypeError: a bytes-like object is required, not 'str'
What am I doing wrong and how am I able to get this working in Python 3? Background context as to why this is would be especially helpful as I think part of my problem is that I'm not seeing why this change is necessary to begin with. Thanks!
Don't use sendto() on a stream socket. Once a socket is connected (and with stream sockets you can't do any data transfer until after connecting), you can't specify the destination, it's always sent to the remote address/port to which it's connected.
So use send() or sendall():
socket.sendall("Hello world".encode());
I've tried to connect two computers with a socket in Python and I don't know why it doesn't work. The files are from internet and it compiles for me but without any results.
The server.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = ''
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
and the client.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = # here I put the ip of the server's laptop
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
What's wrong?
You have to run the server first. Then run the client at the same time with the IP of the server (I used localhost because it was running on one computer, maybe you should try if that works). The code worked fine for me, every time I ran the client, the server printed a message. If it doesn't work for you, maybe your firewall is not letting you open ports.
Just for the future, please always post any error messages you see.
BTW, isn't this the Python Documentation example for sockets?