1

I want to create a linux service and be able to communicate with it. Something like a database which I can add and get data. Is there any way to communicate with my service without making the service a server? I mean do not use http or https methods to send and get data. The thing that I want is a way to connect to the service without using a URL and port number.

5
  • If the problem is specific to the HTTP protocol, other options are available. For example Docker uses various types of sockets for communication. Is this what you're looking for? Commented Aug 10, 2020 at 15:21
  • When you say "except a URL and port number", do you mean "I want to use a URL/port" or "I don't want to use a URL/port"? Commented Aug 10, 2020 at 17:50
  • @Stewart I mean "I don't want to use a URL/port" , sorry for bad use of words! Commented Aug 10, 2020 at 17:53
  • How would you like to communicate with your service then? Raw ethernet? Serial port? Files? Commented Aug 10, 2020 at 17:55
  • @Stewart I'm not sure, I just know I have to write a service for an embedded device, such as raspberry. I have to be able to do some task with my own computer using this device. I think you'r answer can help me. Commented Aug 10, 2020 at 17:59

3 Answers 3

4

I think the answer you are looking for is to use systemd.socket

Let's say that I have any program which reads stdin and writes to stdout. You can configure systemd to make that accessible via a socket.

Here's an example of how you'd configure systemd to do that:

# /etc/systemd/system/simple.socket [Socket] Accept=yes ListenStream=11111 # /etc/systemd/system/[email protected] [Unit] Description="Simple service" After=network.target simple.socket Requires=simple.socket [Service] Type=simple ExecStart=-cat - StanardInput=socket StandardOutput=socket 

Next start the socket with:

$ sudo systemctl start simple.socket 

Now you can run the service by establishing a socket. I use nc or netcat for this:

$ nc 127.0.0.1 11111 

In ExecStart I choose to use cat - which simply relays stdin to stdout. When we run nc, we will discover that whatever we type into stdin will be echoed back to us via stdout. You probably have an application which is more interesting that you'll want to run.

A few other cool points:

  • cat is not running if you don't have an open socket (the socket is listening but the service is stopped).
  • When you establish a connection, the service starts
  • If you establish another connection, another instance of the service starts.
  • When you close a connection the service stops.
3
  • 1
    The question does say, and this is repeated a further twice in comments, that the questioner does not want to use a port number, and 11111 is definitely a port number here. So it's a puzzle that this is the accepted answer. Commented Aug 10, 2020 at 18:14
  • I wrote this question before I saw there was an alternative way to interpret the question. I asked in the comments and then edited the question to make it clear once I realized what he wanted. Commented Aug 11, 2020 at 5:35
  • That's not the puzzle. That the questioner accepted an answer using port numbers is. (-: Commented Aug 11, 2020 at 7:33
0

Whenever something is receiving connections, you have a server. HTTP/S is just one protocol of many, many. You can connect to a remote database by its own protocol also. If you are looking for a more complex solution, read about SSH tunnelling - forward and reverse. And systemd is in no way a required element in your solution.

9
  • the thing that I want is a way to connect to the service except a URL and port number. is it more clear? Commented Aug 10, 2020 at 15:34
  • @AmirrezaRiahi You would need to use some very unusual network infrastructure to get that. And a tailored OS. Even localhost is a URL. Port can be default though. Commented Aug 10, 2020 at 15:38
  • 1
    @AmirrezaRiahi How do you want to access something without naming its address anyhow? Commented Aug 10, 2020 at 15:39
  • 1
    Maybe a unix domain socket? Commented Aug 10, 2020 at 15:50
  • 1
    @jsbillings That's an address also. Commented Aug 10, 2020 at 15:51
0

If you want to access this on your local machine only, and you want to make simple scripts, use a fifo. If you want other systems on the network to have access as well, look at netcat (nc).

For Python adepts, there is Flask, and for Perl programmers, Dancer2.

In C, you would use socket, bind and listen.

And as @tom remarked: systemd has nothing to do with what you're asking for.

EDIT:

Your addition

The thing that I want is a way to connect to the service except a URL and port number.

makes it completely unclear what you are looking for.

If you want to communicate with a computer, you must know its address. Otherwise, you will never know who you are communicating with. Your database and your client must also have an agreed protocol. Otherwise they will not understand each other. And with that, you have by necessity a URI.

If you do not want to use a port number, like TCP or UDP does, you can use another protocol (https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml) if that suits you. 253 or 254 would then be suitable. But, and please do not take this as patronizing, you do not seem to have the skill yet to make this working.

If you would take recommendations, try to use a fifo on the same system first. If that works, investigate in netcat.

2
  • 2
    I'm not communicating with a computer on network, it's all in my device. I run a service on my device and communicate on my device. Commented Aug 10, 2020 at 17:24
  • Since the questioner has said "how a database works", perhaps answers could suggest things based on the ways in which databases indeed do work, including AF_LOCAL sockets. The idea of a named FIFO is somewhat buried at the end of this answer. Commented Aug 10, 2020 at 18:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.