0

I am testing a device (a GPS tracking) that is sending data (the GPS position) to a given IP on a given port. I don't know what kind of data format it sends, is there a way to listen on my CentOS server for incoming data on a given port and maybe print it on screen ?

adrianTNT
  • 3,671
  • 5
  • 29
  • 35
  • You should be able to echo the characters coming in on a certain port with almost any terminal program - don't even need CentOS – Floris Nov 30 '13 at 05:33
  • But how is it done? Do I use some command like `netstat` ? I only have experience with PHP programming and html requests but I am unsure how these port communications should be read. – adrianTNT Nov 30 '13 at 20:18
  • 1
    There is a simple python script at http://ilab.cs.byu.edu/python/socket/echoserver.html - could easily modify this to print to screen (console) rather than echo back to client. If you turn off CentOS and set port to 80 I think you are 90% there. – Floris Nov 30 '13 at 20:32

2 Answers2

1

Expanding on the comment I left earlier (at the time I could not test): if you have access to Python on your machine, you can create the following simple script - e.g. in a file you call myServer.py (attribution: http://ilab.cs.byu.edu/python/socket/echoserver.html - you will find much more information about Python network programming there; well worth a visit):

#!/usr/bin/env python
import socket
host = ''       # your IP address
port = 50000    # pick a port you want to listen on. 
backlog = 5     # number of clients you could handle at once
size = 1024     # max size of data packet

# create socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host / port:
s.bind((host,port))
# start listening:
s.listen(backlog)

# stay in this loop until ctrl-c is typed at console:
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        print data          # echo data to console
        client.send(data)   # echo data to client (may confuse your application, great for browser)
    client.close()

When I ran this on my Mac by typing

chmod 755 myServer.py       # make it executable
./myServer                  # run the script

And then went to my browser where I typed

http://localhost:50000/?hello=world

I got the following in my browser:

GET /?hello=world HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

And my console looked like this (I hit ctrl-c after servicing the request):

[myMac:~myCodePath] myName% ./myServer.py
GET /?hello=world HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive


^CTraceback (most recent call last):
  File "./srv.py", line 13, in <module>
    client, address = s.accept()
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
KeyboardInterrupt

I think this should be a reasonable way for you to take a look at the traffic from the GPS. It should be easy to see how you can change the response back to the client - right now it's just the request being echoed, but you can obviously send anything with

client.send('anything')

Let me know if you have any further questions about this. Note I used port 50000 - I was guessing it would be available. The way my machine was set up I could not use port 80 (the default html port) but that's mostly a question of firewall configuration etc.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Thanks for the very complex answer. I do have python on my server, I tested and the above works very well. The command `nc -l {PORT}` also prints the data but your Python script will allow me to handle the data better. – adrianTNT Nov 30 '13 at 23:56
  • is that "data" variable a certain type other than string? I cannot seem to use it, I am trying to make an URL request and it sends a blank value: `urllib2.urlopen('http://www.site.com/?data='+data).read()` – adrianTNT Dec 03 '13 at 18:12
  • There may be "illegal" characters in the data that your device sends - you should at the very least take a look at it and maybe `urlencode` it to be safe. Never stick something at the end of a URL without knowing what it is... You could have I intended side effects. See http://xkcd.com/327/ – Floris Dec 03 '13 at 22:08
  • Data looks like this, I tried to URL encode it, I don't know what I am missing but I will try some more, the `print data` outputs fine. `#355488020131816##1#0000#AUT#01#2260012b9cbb47#3112.703000,E,4245.754120,N,0.07,0.00#031213#184625.000##` – adrianTNT Dec 04 '13 at 02:11
  • OK, I was not url-encoding it properly before sending it as an URL variable. I encoded it with `urllib.quote(data)` and then worked fine – adrianTNT Dec 16 '13 at 23:50
  • Very glad to hear you got it figured out - and thanks for returning here to let me know. – Floris Dec 17 '13 at 01:51
1

I found a program/command netcat that seems to do what I needed. I installed it with Yum, then I can type this to listen on port 333 for example:

nc -l 333

And it constantly prints incoming data on port 333.

adrianTNT
  • 3,671
  • 5
  • 29
  • 35