Modbus TCP Client does not connect - pymodbus3

Background info:
I am trying to read data from my energy meter. I have connected a gprs modem to the meter(via rs485) port. This modem sends data via tcp-ip protocol to a specified server and port. On the server(aws ec2 instance) I can see a connection from the device.
Question:
When i try to connect to this device using PyModbus, i am unable to open connection.
from pymodbus.client.sync import ModbusTcpClient as Modbusclient
client= Modbusclient(host=<internal ip of the connection>, port=5025)
if client.connect():
print("Connected")
else:
print("Not Connected")
The response is always "Not Connected".
I have ensured that a) the port is open. b) ip address is whitelisted on the server
Why can't i connect? What am i missing here?

In my particular case, the gprs modem was acting as a serial-over-tcp gateway.So i had to create a serial port and then connect to it. What i ended up doing was:
Create a pair of serial ports (e.g. p1 and p2) on Linux using socat command
Create a simple tcp listener (using sockets library in python)
Forward all data from tcp port to p1
Import ModbusSerialClient from pymodbus.client.sync
connect to P2 using Modbus Serial Client.

Related

Client and Server connection with different port numbers?

I have set up a client and a server using sockets in python where my client sends data to the server, servers performs an operation, then returns some data to the client. Originally both the client and server were to have the same port number (9999). My issue currently is that I have to change the port of the server to 19999, and when I try to run, it does not work. The client is able to send data if its port is also changed to 19999, but it does not work if client is 9999 and server is 19999, which is what I need. New to networking systems so would appreciate any useful links to information or advice.
pic of client (left) and server (right)
Normally, clients will use an ephemeral port for its local port, and connect to the server port. Your client code is attempting to connect to port 9999, which is not the server port, which explains why it is not working. You need to connect to port 19999, since that is the port the server is listening on.
Using an unbound socket causes the client to choose an ephemeral port for its local address when making a connection. If you want the client to bind to a specific port, use bind before you call connect.
# bind locally to 9999
local_addr = (host, 9999)
s.bind(local_addr)
# connect to 19999
remote_addr = (host, 19999)
s.connect(remote_addr)

How to let client application outside your netowrk connect to your server

I'm new to socket programming. I have developed fundamental/simple client and server application where client successfully communicates with server. Currently, both the server is on my system (local host) and client is also my system.
Now I want to somehow allow clients outside the network ( network where my system belongs to) to communicate with the server but i have no idea what to do and how to proceed. Any help would be appreciated.
Here's a smaple code taken from here
server.py
enter code here
import socket
# next create a socket object
s = socket.socket()
print "Socket successfully created"
port = 12345
s.bind(('', port))
print "socket binded to %s" %(port)
s.listen(5)
print "socket is listening"
while True:
# Establish connection with client.
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
client.py
import socket
s = socket.socket()
port = 12345
s.connect(('127.0.0.1', port))
print s.recv(1024)
s.close()
You don’t really need to change anything in your code, except for the IP that the client connects to. It needs to be the server PC’s public Internet IP instead of 127.0.0.1.
If the server PC is connected directly to the Internet modem, then you are done.
Otherwise, if the server PC is behind a router or proxy, then you need to configure port forwarding on that router/proxy to forward traffic from its public WAN IP/port to the server PC’s LAN IP/port. Consult your router/proxy’s documentation for how to do that configuration.
If the router/proxy has uPNP enabled, your server code can dynamically forward the WAN IP/port to itself at runtime. See Python: Open a Listening Port Behind a Router (upnp?)

Client socket and Server Socket bound to the same port

I have an IoT device (most likely across NAT), which should communicate with server (on internet), and the server should be able to send commands to the device. Here's the high level communication details:
IoT device will send KeepAlive packet from port 9000 (say) to the server on a port 8000 (say). The server now has the port and IP (say Cp and Cip) which it could use to start a connection back with IoT device if needed.
Server is obviously listening for connections on port 8000. So Server has a server-socket to listen to IoT device's keepalives
The KeepAlive packets are sent periodically, and the connection are closed once the server returns OK. This is to ensure a socket is not kept open, saving resources on both server and IoT device.
Now, say I want to send some command to the IoT device, in that case, I want to open a socket connection from the Server on port 8000, and send packet to Cip, Cp.
The IoT device should receive this packet on its port 9000. For that to happen, the IoT device should open a server socket to accept these connections.
As we can see, both client and server have to open the server socket on a particular port, and occasionally there's a requirement to create a client socket on that port to send some data.
Would TCP work with above scenario? I'm just confused if Client socket and Server Socket could be bound to the same port?
Please suggest
EDIT 1:
I deliberately have chosen above design since keeping IoT device to server connection open all time does not scale. Say I have millions of IoT devices on the field, I'll need to load balance the connections now.
The server to IoT device (backward) connection is only to ensure the commands are immediately send. The forward connection (IoT device to server) is only created to send the keepalives. It would be done periodically (may be 5-10 mins apart)
EDIT 2:
The reason for a fixed port (8000) is this: The NAT opens up hole for reverse communication only from the destination port where the keepalive packet was received. So the reverse packet can be sent from a port X on server to IoT device only if a packet had been received from IoT device on that port.

TCP/IP Socket Programming Static Web IP between Dynamic IP

I am confused about TCP/IP Socket Programming. I know the internet protocols but in theory...
I am explaining my problem (what i need )
I have a server working on X.X.X.X IP Adress. And it always listens to Y PORT.
When i want to connect that server on MY PC, i have no problem because i wrote the ip adress (X.X.X.X) and the port (Y) and my PC connects..
When i connect to server server keeps my ip adress and my local ip adress.. After that connection is end.
Here is my problem starting...
As i sad my server knows my local PC informations. How can i connect to my local PC on server my web server and sent TCP or UDP packets ? I did port forwarding on my modem but i dont want it. When i did port forwarding there is no problem but i dont want it ...
Thanks for replies and sorry for my engislih if i have mistakes .
If a router/NAT sits between your server and an outside client, you MUST use port forwarding on the router. The outside client will NOT be connecting to your server's private LAN IP/Port directly, it will be connecting to the router's public WAN IP/Port instead. So the router needs to know to forward inbound packets to that IP/Port to the server's private LAN IP/Port.
A client running on your local PC is able to connect to the server because they are both on the same LAN side of the router, so the connection is direct and does not go through the router's WAN. That is not the case for clients that are on the WAN side of the router.

TCP/IP using Socket Test 3.0 software

I have 'Socket test v-3.0' software installed on two different PCs.
Using it can create a TCP server on 1st PC and a TCP client on 2nd PC.
When I connect both the PCs on LAN I can establish a connection & can communicate between the Server & Client.
But, When I connect both the PCs to individual Internet connections (Using dongles having different ISPs), I couldn't establish the connection between the Server & Client.
How can I do that?
Please help me out..
The private address 192.168.x.x is address that can be used for direct connection only in your private network. If you want to connect to your machine from public internet you have two options:
Get a public IP address
Configure a port forwarding
Public IP address could be get from your internet provider but it is usually requires some extra payment dependent on your service provider policy.
Port forwarding can be configured at the device at the border between your private network and internet. The device does the NAT (network address translation) between your private network and intenet. Use google if you are not sure about NAT.
Such a device can be your own device like an ADSL modem or a set top box. But such a device could be in the internet provider network. If you own the device then you can configure the port forwarding yourself. Modems usually have a web interface where you can easily configure port forwarding. See the documentation to your modem or whatever you have.
If the NAT device is in internet provider network then you have to ask it to configure port forwarding for you. Before you ask please read something about port forwarding so you are sure what you want to configure.