Nanomsg TCP Handshake - c

I also posted this in the Arduino section, but this problem is probably caused more by my ignorance of nanomsg and connections in general rather than an Arduino problem.
I am attempting to communicate with a server that is using nanomsg to communicate via TCP on a port using an Arduino. I have attempted a variety of different configurations (remotely connecting to the arduino and having the arduino connect to the server, using different nanomsg tools).
I can get the Arduino, in server mode (running a very slightly modified version of the WiFiWebServer example) to successfully read text I send using cat
sudo cat texttosend > /dev/tcp/192.168.1.50/80
However in all the configurations, and no matter what text I am trying to send using nanomsg, I always get a string of the same numbers. Printing the bytes as hex from the arduino, they are 0 53 50 0 0 51 0 0. Nanocat (the simple command line tool of nanomsg) hangs instead of sending and shutting down (like it is constantly trying to confirm the connection before sending the data).
I'm assuming this is some kind of handshake the Arduino is failing, because the client connects, reads those bytes, then shuts down and restarts. Using nanomsg on both ends (from my local computer to the server) works fine.
If these numbers I'm getting are a handshake, how do I complete it?
The meat of the loop part of the Arduino code is
client = server.available();
if (client) {
Serial.println("new client");
while (client.connected()) {
while (client.available()) {
byte b = client.read();
Serial.print(b,HEX);
Serial.write(b);
}
}
}
And the nanocat command that hangs when trying to connect is
nanocat --push --connect tcp://192.168.1.50:80 --data thismesadsfsdfg

The following text describes nanomsg protocol for TCP.
In it you can see why you're getting the specified byte stream and what you should write before the text you want to send.

Related

Sending & Receiving small TCP messages in Linux

Setup
I have a TCP server running on a microcontroller, and a client running on a Linux Destop.
What I'm trying to do
I am establishing a TCP connection, and then periodically sending messages with lengths varying from 18 bytes all the way up to 512 bytes, from the microcontroller (server) to the desktop (client).
The messages must be sent in one packet, so the TCP stack is configured to not split up the data in the microcontroller end.
In the Linux Desktop, I am using the TCP_NODELAY option which allows messages to be sent immediately, turning off the buffering algorithm. Good, I want this.
What the issue I'm facing is
I try to set the option SO_RCVLOWAT to 18 as well. My understanding is that when I call recv() it will return when at least 18 bytes have been received, which is the smallest messages I'll receive.
This didn't work as expected, and I don't know if it's because I'm misusing the API, or what I want to do cannot be achieved with the TCP/IP stack of the Linux Kernel.
The problem is more with the small packages, recv() won't return with small buffers.
One last Note
When running the client on another microcontroller, with the same TCP/IP setup, I can send and receive the data, how I want it, with no problems.
How can I achieve what I want to do under Linux?
It's for a real time application so I need the functionality of UDP but with the reliability of TCP. Thanks in advance!

Trying to understand UART packets I'm sending/receiving over a TCP connection

I'm working on a project that a previous engineer started. Basically, we're sending data from a microcontroller to our server using a WiFi module.
Here is how I've interpreted the code so far:
-The module creates an AP, which the user connects to.
-The user then enters an IP/URL, which allows them to select their WiFi network.
-The module now communicates over the selected network.
-The module connects a socket to our TCP server every second and sends a packet.
-If the server has a command for the client, it responds with a command packet.
My confusion here is trying to understand the packet structures.
The packets the client sends out start with [ESC][Z]['Connection ID'][0][0][0][6]. I'm assuming this has something to do with a predefined UART layout, but I have never used UART before.
The server then responds, if there is a command, with a packet that also starts with [ESC][Z]['Connection ID'][0][0][0][6]. However, the client then responds with a packet that starts [ESC][Z]['Connection ID'] followed by the data.
So what is the '0006' heading my packets, and why aren't these values needed in the client responses? I'm assuming that the 'ESC' is necessary to send data outside of the network, and that the 'Z' is a predefined header. However, please correct me if I'm wrong!
The numbers in the header are just the number of bytes left in the packet after the header bytes. ><

How can i connect to memcached via C sys/socket.h?

How can i connect to Memcached via C sys/socket.h and set some text string to key "key"? I can't figure out how can I run .c program which will connect to cashing system via socket. I can connect to it through console by writing smth like this
memcached -l 127.0.0.1 -p 12345 -m 64 -vv
and then
set key 1 0 4
test
but i have to do it using socket in C
It looks like you're missing some knowledge regarding C sockets in general.
As an overview, a socket is a two way communication channel that connects a client with a server, each having their own end of the socket.
What memcached is doing is using the socket mechanism to transfer data between memcached and whoever it is looking for the data.
memcached is using TCP sockets and clear text messages so it is easy to work with.
What you'll have to do:
open a socket and connect it to the memcached server at 127.0.0.1 port 12345 (taken from your example)
Write 'set key 1 0 4\n' to the memcached socket
Read the string from the socket (this is memcached result)
The following read: http://www.thegeekstuff.com/2011/12/c-socket-programming/ provides code snippets and great explanation on sockets and how to use them, and the client code contains 90% of the work you need to do
Feel free to ask if you need further clarifications

UDP Port access

I have a small server program in C which prints a message to the client. This program uses UDP Port for communication.
My question is: Is there a way or application by which I can test the functionality of my program from my windows machine. Example, if I type in some command, I can see the response from my program on my computer.
telnet xx.xx.xx.xx. PortNum, I believe telnet wpuld not work.
Not aware of any existing tools. I assume your server receives a message from the client and sends a response message back. If this is correct, create a basic client program which sends a message (sendto()) and then calls recvfrom() (default is blocking mode on my platform), then print the response message received. This works well for me. Don't have time to ferret around for an example (which is on linux) but you should be able to use an example udp client for windows from the web, I imagine. Let me know if you would like my client program as a template.
I think you may want to use netcat; if it's installed on your machine, it's typically executed by "nc"
netcat can connect to or listen on tcp or udp ports; -u is udp.
nc -u host port # connect to a udp port
nc -u -l 127.0.0.1 1026 # listen on port 1026, in udp mode.
etc.

Are select()/poll() appropriate methods for a simple real time game server in C?

I'm working on a simple multiplayer online text game and I read select() and poll() were popular methods for multiplexing I/O.
I found this example in the GNU C docs, which uses select. I ran it and made 3 Python test clients that look like this:
import socket
import time
port = 5555
test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
test.connect(('localhost', port))
while 1:
test.send('aaaa')
time.sleep(0.5)
Then I ran the clients (from the same computer that's running the server) with different strings (aaaa, 55 and ..).
The output of the server was
Server: got message: `aaaa'
Server: got message: `55aa'
Server: got message: `aaaa'
Server: got message: `55aa'
Server: got message: `..aa'
The strings were getting mixed up.
Is this some kind of silly compilation error, an error in my test clients or the fact that I'm running clients and servers from the same computer? Or is this program telling me I shouldn't use select for this purpose? Considering that I'll use this for a multiplayer game server (which probably gets lots of messages per second), I don't think I can let the messages get mixed up.
The clients and server are completely asynchronous. You don't know which is going to get time on the CPU and in which order. The network also provides no guarantee about order of delivery (though TCP will ensure that the application sees data from a single client in the order it was sent).
Select/poll return a list of sockets that have data to read. Make sure you read them all before calling select/poll again otherwise you will be prioritizing connections on lower-numbered ports.
Note that a read is raw data, not terminated in any way, and so cannot be dumped directly as a string unless the sender included a NUL terminator in the write to its socket.

Resources