I want to send and receive data between raspberry pi and windows PC. If I placed both server and client program on raspberry pi then it works fine. but when client is run from windows pc then it shows an error 'no connection could be made because the target machine actively refuse it'. I am simply connecting pc and pi through a LAN cable. and firewall of my PC is disabled.
server.py
import socket
s=socket.socket()
host=socket.gethostname()
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()
client.py
import socket
s = socket.socket()
host = '192.168.0.3'# ip of raspberry pi
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
If it works on the loopback interface (both scripts on the Raspberry Pi), this means your code is correct.
Why it doesn't work over the network is not really programming related. A few things to check:
As Akash Nil noted, check if you're using a crossover cable. Does the little LED on your LAN port light up when you connect the cable?
Does ping work?
The RPi doesn't have any firewalling active, either, right?
If you're using Windows, put the server on the RPi or be 100% sure you've whitelisted Python in your firewall (or deactivated it).
Does it work if you use netcat as client or as server?
Related
I'm having a trouble to receive UDP packets (SOCK_RAW) from my FPGA board which is sending UDP packets non-stop. This only happens when I disconnect from internet on my receiving machine (using Python3.8 on a PC running Ubuntu 20.04).
import time, socket
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
sock.bind(("10.10.10.1", 4096))
while True:
d = sock.recv(1068)
print(d)
time.sleep(0.1)
This will print packets fine when my PC is connected to the internet via other Ethernet interface, but when I disconnect from internet, it stops receiving packets.
if I set
sock.setblocking(False)
I receive
BlockingIOError: [Errno 11] Resource temporarily unavailable error.
I need both of available Ethernet ports that's why I have to disconnect from internet.
Im using python socket to connect two computers,
it completely works when both systems are connected to one mobile hotspots or LAN,
But when i use two diffrent hotspots or try to connect to my friend's PC, it stops working and client cant find server.
(I find my IP using "ipconfig" command on cmd from "Wireless LAN adapter Wi-Fi: IPV4 Adress")
here are my codes for checking the connection:
#SERVER
import socket
s = socket.socket()
IP, port= MY IP, 8002
s.bind((IP,port))
while 1:
s.listen(1)
a,b=s.accept()
print(a,b)
print("client tried to connect")
#Client
import socket
s=socket.socket()
IP,port=SERVERS IP, 8002
s.connect((IP,port))
Can you guys help me please?
I am using pybluez to develop a bluetooth application on linux in python. I want to know if it is possible to connect to a "localhost" for bluetooth so I can run the client and server on the same machine (as most people do for web development).
If this is not possible, how to most people develop bluetooth applications? Do they just run the client and server on different devices or is there a more clever way to handle this?
Eventually the server will run on a raspberry pi and the client will be any bluetooth enabled device (cell phone, laptop, etc.) but during development it would be great if I could run both on the same machine.
Here is my server:
import bluetooth as bt
socket = bt.BluetoothSocket(bt.RFCOMM)
host = ""
socket.bind((host, bt.PORT_ANY))
port = socket.getsockname()[1]
print("port: " + str(port))
socket.listen(1)
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
# bt.advertise_service(socket, "BTServer", uuid)
print("Listening on " + host + ":" + str(port))
client_sock, addr = socket.accept()
print("Connection accepted from " + addr)
data = client_sock.recv(1024)
print(data)
client_sock.close()
socket.close()
And when I call services = bt.find_service(name=None, uuid=None, address="localhost") on the client it cannot find any services.
Through further research I have found that it is not possible to run a bluetooth client and server on the same device with the same bluetooth adapter. For local testing you can either use two bluetooth enabled computers or get a bluetooth dongle.
It is not possible to run a Bluetooth client and server on the same device.
I used the pybluez python module.
When I run bluetooth.discover_devices(lookup_names=True) in the client code on my machine, it returns all other Bluetooth devices around it except my machine.
As we can not discover the machine, we can not connect to it over Bluetooth and can not use it as a Bluetooth Server.
So you have a basic understanding of the parts im using, I have:
Arduino Uno
Seeed Studio GPRS Shield v2.0 (http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0)
Ultimate GPS for Adafruit V3.3 (https://www.adafruit.com/products/746?gclid=Cj0KEQjw3-W5BRCymr_7r7SFt8cBEiQAsLtM8qn4SCfVWIvAwW-x9Mu-FLeB6hLmVd0PAPVU8IAXXPgaAtaC8P8HAQ)
Here is my problem:
I have tested the Arduino stacked with the GPRS shield, and it works fine with regards to accessing the internet through TCP, sending SMS, etc.. However, my application requires me to send GPS data from the adafruit GPS to a web server that I have already coded with Django and postgresql. The backend is set up.
I need to send the data from the Uno (client) to my laptop (server), which I coded in python (This is just to check whether it is creating a connection):
#!/usr/bin/env python
import socket
# import postgres database functions
TCP_IP = '192.168.1.112'
TCP_PORT = 10000
BUFFER_SIZE = 40
server_address = (TCP_IP,TCP_PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created.'
# Bind socket to TCP server and port
try:
s.bind(server_address)
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket Bind Complete.'
# Start Listening on socket
s.listen(1) # Puts socket into server mode
print 'Listening on port: ', TCP_PORT
# Now Keep Talking with the client
while (1):
# Wait to accept a connection
conn, addr = s.accept() # Wait for incoming connection with accept()
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
if not data: break
print "recieved data: data", data
conn.send(data) #echo
conn.close()
I dont think there is a problem with this. From this I will post data to my postgreSQL database. However, When I try to use AT commands on the SIM900 module to connect to the server using port 10000, I cannot connect:
AT+CIPSHUT
SHUT OK
AT+CGATT?
+CGATT: 1
OK
AT+CIPMUX=0
OK
AT+CSTT="fast.t-mobile.com","",""
OK
AT+CIICR
OK
AT+CIFSR
6.60.94.49
AT+CIPSTART="TCP","192.168.1.112,"10000"
OK
STATE: TCP CLOSED
CONNECT FAIL
I have tried connecting through TCP and replaced the AT+CIPSTART line with the below statement and it worked, so I know TCP works:
AT+CIPSTART="TCP","www.vishnusharma.com", "80"
Is the IP i'm using wrong? I'm new to this, but if it makes a difference, im using Ubuntu 16.04 partitioned on my Mac OSX. I have also checked the APN for T-mobile and it seems fine.
Any help would be greatly appreciated. Thank You!
The IP you're using is inside a NAT since it starts with 192.168. Unless you have a private apn with the mobile operator you're using, you won't be able to reach your Ubuntu from a public IP. Your ISP gives you a public IP address which ir administrated by your router, so if you want this to work, you'll have to do a port forwarding from your router to your Ubuntu.
To do the port forwarding you have to get in the router's configuration page (Typically 192.168.1.1 but depends on the model) an there you'll have to redirect the port XXX to 192.168.1.112:10000. After that you have to obtain your public IP (curl ifconfig.co) and use it to access from the SIM900.
First of all as a suggestion, you can combine the two shields by using SIM908 (unless you are getting more precision on your GPS shield). Since your TCP connection is working, I bet that the port 10000 on your ubuntu is blocked by the firewall. You can first try to turn off your firewall and see if it works. If it did not worked its something else. If it worked, turn on your firewall and then unblock the tcp port using the following command:
sudo ufw allow 10000/tcp
I am trying to set up a Python socket between my Raspberry Pi (running Raspbian) and my Macbook Pro (running Mavericks).
Both devices are connected to the same WiFi network in my appt. I run the server code on my RPi and then the client code on my Macbook (I have also tried the reverse). I think that I am missing a set up step because the code I am using I found on multiple sites, so I assume it works. I also checked that my Macbook has firewall turned off.
Server Code:
from socket import *
host = "127.0.0.1"
print host
port = 7777
s = socket(AF_INET, SOCK_STREAM)
print "Socket Made"
s.bind((host,port))
print "Socket Bound"
s.listen(5)
print "Listening for connections..."
q,addr = s.accept()
data = raw_input("Enter data to be sent: ")
q.send(data)
Client Code:
from socket import *
host = "127.0.0.1"
print host
port=4446
s=socket(AF_INET, SOCK_STREAM)
print "socket made"
s.connect((host,port))
print "socket connected!!!"
msg=s.recv(1024)
print "Message from server : " + msg
I get the error:
Traceback (most recent call last):
File "TCPclient.py", line 9, in <module>
s.connect((host,port))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
My process for executing the code is:
type "python TCPserver.py" into RPi terminal
type "python TCPclient.py into Macbook terminal
Then I receive the error message on my Macbook, no error on the RPi
My questions are:
Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
Does the input for host have to be the same for the client and server code?
Should the RPi and Macbook both be connected to the same WiFi network?
Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
Do you know why I am receiving this error and how to fix it?
Your help is greatly appreciated!!
127.0.0.1 is a special IP address for local machine.
You must set the real IP address (on your LAN) of you mac in the client code.
You should also bind on this IP on the server, or on 0.0.0.0 to bind on all available IP addresses.
You must also use the same port number on both client and server.
And to reply to your questions:
Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
127.0.0.1 is the same than localhost, it means the local machine. This will work if you run the client and the server on the same machine, else you need the real IP address of your mac. Try ifconfig in a console.
Does the input for host have to be the same for the client and server code?
Yes and no. On the server you bind to a port and an address, so you'll wait for connections on this port and address. You can use the IP address, or 0.0.0.0.
Should the RPi and Macbook both be connected to the same WiFi network?
Yes and no. It will work with the same WiFi network, but it will also work with two different WiFi networks if they are connected together directly or with a IP router. Most of the time though they are connected to the internet through a NAT (network address translator), and then it will not work.
Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
I don't know much about the RPi but it looks like standard TCP sockets, that should work out of the box.
Do you know why I am receiving this error and how to fix it?
As I stated at the beginning: You try to connect to the RPi (127.0.0.1) on the wrong port.
You have created a listener on port 7777, then you connected on 4446 !!!!!
just connect on the same port you are listening to =)