sending and receiving string through TCP/IP
I am trying to communicate with my hardware which is sending me data in form of a string "$$,0,0,0,0,0***" on a specific port of my Linode Server.
Now to open port I initially setup a virtual server that enabled my port also I was able to send data to my TCP/IP port but when my Listener (written in PHP) tries to bind port and receive data it gives error that port already in use. Now I understand that because my virtual server is taking the port I am unable to bind it.
Can any one suggest best way to proceed also Linode allows us to use TCP/IP protocol to send and receive strings and not packets?
2 Replies
When you want to bind to a port, you must make sure you have proper permissions. If you use SELinux then that needs to be setup properly. If you are binding to a lower port then that needs root privileges, which higher ports don't (>1024). Then you also need to allow connections from your firewall.
In general, only one program can listen to a port. You can see what programs are listening with the command netstat -lvpnut* run as root. This will show you an address:port and the process ID/name of the program. For example, as seen below I currently have portmap listening for TCP and UDP connections on port 111, and chronyd listening for UDP connections (on both IPv4 and IPv6) on port 123.
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 962/portmap
udp 0 0 0.0.0.0:111 0.0.0.0:* 962/portmap
udp 0 0 0.0.0.0:123 0.0.0.0:* 2123/chronyd
udp6 0 0 :::123 :::* 2123/chronyd
The correct way to accomplish what you are trying to do is to make sure only one program (the one you want to receive this data) is listening to the port in question. If you have a test program or something else listening on that port, you need to kill it or otherwise detach it from that port. Then you will be able to run your listener program and have it listen on that port.
*Technically, this applies to a network address/port/protocol combination, but for the most part just saying "port" is correct.