Information exchange between two computers connected over wireless - c

I would like to get some ideas on this topic which is new for me and i am interested to learn more on this.
I have developed a voice controlled application which is written in C program which should control some operation. I want to control these operations on another computer connected over wireless network.
I want to do the following,
when my voice is recognized at Computer A with a "String A", It should send this "String A" to COmputer B, Computer B sends a request to acknowledge to computer A, when computer A send Acknowledge string "Yes" to B, Computer B will execute a operation (for example open the notepad).
PS: Computer A and B, both are Linux, Ubuntu machines
How to start doing this Information exchange? Also provide some useful links along with your suggestions. I am sure it will help me a lot in completing this.

basically you can use socket communications. Write client and server sockets on both the machines. One to Receive command and other one to send commands. Upon Getting the sockets ready (the whole dance of bind, listen etc.,), you kickoff the voice2text module. OnVoice2TextConverted, send the message to other machine via socket. OnReceive at other machine, prepare another message (ack) to send it to request initiator. Define a message structure for mode communication between these two machines(see if you have to serialize or use XML/JSON). when you shout 'enough', detect the voice through your module and tear down the sockets. Intimate this to other party before tear down. Perform clean up.

Related

UEFI networking edk2 simple example

I am looking for the simplest/easiest way to implement some sort of networking communication using edk2 in a UEFI application. I do not care what type of protocol is being used, but it shall not be UDP.
I have read similar questions, tried a bit on my own but failed, and my problems are;
-A good test environment(Using a VM currently, running UEFI 2.5, not sure if sufficient/is a good way)
-What protocols/solutions are good?
What I need to accomplish is;
uefi application loads, sends a packet containing "Hello" to Server(IP can be known, no need to resolve IP from url)
Server sends "Hello" to the Client, and the UEFI application will print the reply. It is very simple, but this has been overwhelming and the information out there is limited from what I have seen.
All help/insight/information is appriciated.
There is a complete HTTP sample program in the UEFI specification (Chapter 29.6.2.1).
There is also a complete UDP sample on stackoverflow, if you want to use TCP you just need to connect to the server before sending data. The method and field names are nearly the same in the TCP and UDP protocols.
All higher level network protocols work in the same way.
Create a child instance, search for the ServiceBinding protocol and call CreateChild
Get the protocol from the child instances handle
Configure the child instance (XYZ->Configure(...))
TCP only: Create a Connect Token
TCP only: Call XYZ->Connect(...) and wait till it completes
Create a Transmit/Request Token
Call XYZ->Transmit(...)/XYZ->Request(...) and wait till it completes
Create a Receive/Response Token
Call XYZ->Receive(...)/XYZ->Response(...) and wait till it completes
Go back to 6 or 8 if you need to transmit/receive more data
TCP only: Create a Close token
TCP only: Call XYZ->Close(...) and wait till it completes
Destroy the child instance (ServiceBinding->DestroyChild(...))
You may call XYZ->Poll(...) while you are waiting for the network operations to complete.

Linux serial port (tty) redirection

I have a question linked to Linux and serial port.
I want to be able to receive and send messages to a dedicated serial port and to redirect it to another port (/dev/tty).
For the first part, I’m able to dialog with my hardware equipment without any problem, but I’m just wondering if it’s possible to intercept and redirect message coming from a serial port #1 to another port #2.
To give more context, I had used a GPS Antenna and NTP open source software for years.
Since 2018, the new GPS antenna protocol has modified the order of bytes in the message used by NTP to steer and now it’s not working anymore.
So my idea is to put a simple C program (middleware) which fixes this byte ordering; but I’m wondering if I have to build a kernel-specific module or if it can be done in another way. The NTP software uses the symbolic link to dialog.
Thanks for your help.
You can probably use a simple redirect, look here:
Pipe One Serial Port to Another in Linux
If the ports are in different rates you can use stty or perhaps screen to adjust: https://unix.stackexchange.com/a/117064
If you need it to be in c program to manipulate it you can use the following: https://stackoverflow.com/a/6947758/8901188
Using c it will need to run in an infinite loop so it can constantly read, manipulate and write the data.

In the following scenario am I the server or the client?

So I have a PC connected to a micro-controller via a serial cable and an Ethernet cable. Initially the PC sends a byte across the serial cable to the micro-controller. This results in the micro-controller sending back a UDP datagram via the Ethernet cable.
I want to know whether the code running on my PC should be a server or a client?
Per Wikiepdia Client/Server:
The server component provides a function or service to one or many
clients, which initiate requests for such services
And Master/Slave:
Master/slave is a model of asymmetric communication or control where
one device or process controls one or more other devices or processes
and serves as their communication hub
The above scenario looks like the Master/Slave. In the initial, 'idle' case, there is no "SERVER" that is waiting ("listening") for requests. Only when the PC activate the micro-controller they will start communication (via UDP).
You could use either term depending on what you were talking about. As other people have noted, client and server are terms used to describe how distinct parties are involved in a service. The terms can be useful in some situations (e.g. a web server and the browser as a client) but in other situations it's a less useful term (e.g. peer-to-peer protocols).
Presumably you're on stackoverflow because you're dealing with code.
In this case it's useful to be more precise and I'd suggest using terms to match whatever primitives are exposed by your language. Most will use/expose Posix sockets as their standard API, and hence you'd want to talk about/use connect or accept (potentially after binding first). Note that these calls work across TCP and UDP (except accept), but the semantics of sending and recving on the resulting connected sockets will obviously be different.

Local Communication - 127.0.0.1 vs. IPC

I am not clearly understanding the difference between using TCP socket with client connecting to 127.0.0.1 server address and other IPC such as message queues. Since both are used for communication within the same host, why at all someone would go for socket approach leaving the message queue one, as in this case, sockets will cause more overhead compared to the queues.
The differences that I am seeing:-
In case of sockets we can see the contents in wireshark, in queues there is no such way.
The point of the loopback interface / address is not that you write programs to use it specifically.
The point is that it lets you talk to network services running on the local computer in the same way that you would talk to network services running on a remote host. For instance, if I'm developing a website, I can start up a test instance of its server on my local computer and then point my browser at http://127.0.0.1/ and there it is. I don't have to modify the code of my browser to talk over AF_UNIX sockets or whatever first. Similarly, if I am writing an application that needs a database, I might start out with the database running on the same computer as the application, talking to it over loopback, but then later when the database gets bigger I can move it to a dedicated host and I don't have to change anything other than the connection configuration.
You are absolutely correct that local IPC has lower overhead, and should be used when the two processes that need to communicate will always be on the same machine.
TCP and IPC both approach we use for inter process communication in distributed architecture. If processes are running in same machine we will go for message queue but surely not TCP. But suppose one application is running in one box and another application is running in a different box definitely we have to go for TCP for inter process communication. Even web services also internally implement TCP for communicating to a remote application.
But still we need a TCP base communication in the same machine between two process where synchronize communication is must. For example if you send a request for an account information of a client and waiting for the response you need this approach. But if you just need to send a client information to a server to store it in a table and you don't need an answer from that server whether your records has been stored successfully or not you just go for a queue only to drop the message.

Arbitrary two-way UNIX socket communication

I've been working on a complex server-client system in C and I'm not sure how to implement the socket communication.
In a nutshell, the system is a server application which communicates with a database and uses a UNIX socket to communicate with one or more child processes created with fork(). The purpose of the children is to run game servers. The process of launching a game server is like this:
The server/"manager" identifies a game server in the database that is to be made. (Assume database communication is already sorted.)
The manager forks a child (the "game controller").
The game controller sets up two pipe pairs, then forks, replacing its child's stdin with a pipe, and it's stdout and stderr with another pipe.
The game controller's child then runs execlp() to begin running the actual game server executable.
My experience with sockets is fairly minimal. I have used select() on a server application before to 'multiplex' numerous clients, as demonstrated by the simple example in the GNU C documentation here.
I now have a new challenge, as the system must be able to do more: the manager needs to be able to arbitrarily send commands to the game controller children (that it will find by periodically checking the database) and get replies, but also expect incoming arbitrary commands/errors from them and send replies back.
So, I need a sort-of "context" system, where sockets are meaningful only between themselves. In other words, when a command is sent from the manager to the game controller, each party needs to be aware of who is asking and know what the reply is (and, therefore, which command it is a reply to).
Because select() is only useful for knowing when we have incoming data, and a thread should block on it, would I need another thread that sends data and gets the replies? Will this require each game controller, although technically a 'client', to use a listening socket and use select() as well?
I hope I've explained the system and the problem concisely; I will add more detail if required. Thanks!
Ok, I am still not really sure I understand exactly where your trouble is, so I will just spout off some things about writing a client/server app. If I am off track, just let me know.
The way that the server will know which clients corresponds to which socket is that the clients will tell the server. Essentially, you need to have a log-in protocol. When the game controller connects to the server, it will send a message that says "Hi, i am registering as controller foo1 on host xyz, port abc..." and whatever else the server needs to know about its clients. The server will keep a data structure that maps sockets to client metadata, state, etc. Whenever it gets a new message, it can easily map from the incoming host/port to its metadata. Or your protocol can require that on each incoming message, the will client send the name it registered with as a field.
Handling the request/response can be done several ways. First lets deal with the networking part of it on the server side. One way to manage this, as you mentioned, is by using select (or poll, or epoll) to multiplex the sockets. This is actually usually considered the more complicated way to do things. Another way is to spawn off a thread (or fork a process, which is less common these days) for each incoming client. Each spawned thread can read its own assigned socket, responding to messages one at a time without worrying about the fact that there are other clients besides the own it is dealing with. This simple one to one thread to socket model breaks down if there are many clients, but if that is not the case, then it is worth consideration.
Part 2 really covers only the client sending the server a message, and the server replying. What happens when the server wants to initiate communication? How does it do it and how does the client handle it? Also, how do you model the model the communication at the application level, meaning assuming we have the read/write part down, how do we know what to send? You will probably want to model things in terms of state machines. There is also a lot more to deal with like what happens when a client crashes? What about when the server crashes? Also, what if you really have your heart set of using select, perhaps because you expect many client? I will try to add more to this answer tomorrow.

Resources