As you can see in images below i cannot reciveve anything from client and server keeps waiting! I have tried it without firewall and no result.. :(
cmd info
Client
import socket
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.sendto("HOLA",('192.168.0.159',25585))
sock1.close()
del sock1
Server
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(('0.0.0.0',25585))
while True:
data , c = sock.recvfrom(1024);
print data
sock.close()
del sock
Your client and your server are not connected. Try this:
client:
import socket
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.connect(('192.168.0.159',25585))
sock1.sendto("HOLA",('192.168.0.159',25585))
sock1.close()
del sock1
Your server code is good
Otherwise UDP sockets are mostly known for packets dropping
Related
I have a problem where I need to store data on a disk when the TCP socket disconnects for whatever reason that would otherwise be sent over that socket and simultaneously check when a new connection is established and I can't find a solution to this.
What I have is:
def start_server_for_clients(srv, port):
client = None
try:
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(('', port))
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.listen(5)
print('Waiting for clients on port %s' % port)
while True:
client, address = srv.accept()
client.settimeout(10)
print('Connected client %s %s.' % (address[0], address[1]))
t = threading.Thread(target=handle_client, args=(client, address))
t.daemon = True
t.start()
except Exception as e:
print(e)
if client:
client.close()
handle_client is a function where data is processed and sent over the socket every second in a while loop. I need that processed data either way: connected or not and store it.
Now my logic to separate data processing and socket communication into different parts but where does it fit into threading once the connection is back up again because I would need to clear off the stored data first while simultaneously storing/buffering newly accumulated data and only then proceed to instant communication between sockets? Do I need to use select in this thread and spawn another one to for storing and recovering?
I captured packets on individual text files with tcpdump, I want to send back the captured packets, first I extracted the IP and Port, but I have not been able to send the packet.
This is my code:
def client():
packet = open("packet3.txt", "r")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(("192.168.128.1", 80))
while True:
try:
sock.send("packet")
sleep(1)
reply = sock.recv(131072)
if not reply:
break
print "recvd: ", reply
except KeyboardInterrupt:
print "bye"
break
sock.close()
return
client()
I get this error:
reply = sock.recv(131072)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
I am trying to send data continuously from raspberry pi to a windows pc using MQTT,
I am trying to send 5 data to mosquitto, but the mosquitto seems to get only one value
coding in raspberry pi
import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
data={"protocol":"mqtt"}
client.publish("/test",str(data))
coding at the broker to receive data is
import paho.mqtt.client as mqtt
print("attempting to connect...")
def on_connect(client, userdata, flags, rc):
if(rc==0):
print("connection successful broker linked")
elif(rc==1):
print("connection refused - incorrect protocol version")
elif(rc==2):
print("connection refused - invalid client identifier")
elif(rc==3):
print("connection refused- server unavailable")
elif(rc==4):
print("connection refused- bad username or password")
elif(rc==5):
print("connection refused- not authorised")
else:
print("currently unused")
client.subscribe("s/test")
def on_message(client, userdata, msg):
data=eval(msg.payload)
print(data)
client = mqtt.Client()
client.connect("localhost",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
Have you thought about following the answer I posted here?
https://github.com/eclipse/mosquitto/issues/972
You need to make sure the network loop runs for the publishing client as well a the subscriber. The network loop actually handles sending the messages.
The following is the simplest modification to your code.
import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
data={"protocol":"mqtt"}
client.publish("/test",str(data))
client.loop()
There is a server for a single client. Client can connect and disconnect at any time
Here is simplified code
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while true:
a = s.accept()
try:
data = a[0].recv(1024)
except socket.error:
a[0].close()
print 'cought the error'
It works, but socket.error generation takes random amount of time from few seconds up to a minute. May this value be managed?
If the client disconnects, there is no socket.error, rather recv() returns an empty string immediately. Besides, you are not closing the connection in this case.
I am making a IRC Client script to learn a bit about Python, that may evolve down the road... So I can connect with out issue. But I want to get the port that the socket connection is using when I use socket.connect() .
I ask, because I want to compile with the Auth RFC.
So I want to be able to send,
MYPORT, SERVPORT : USERID : Windows : Username
Is it possible to get the port of the socket being used for the out going connection? So something like this:
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect(('irc.rizon.net', 6667))
irc.send( str( irc.getsocketused() ) + ", 6667 : USERID : Windows : Username")
You can use the getsockname method on the socket, for example for IPv4;
>>> irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> irc.connect(('irc.rizon.net', 6667))
>>> addr,port = irc.getsockname()
>>> port
52675 (this will be your local port)