Code examples, Connecting with the nexos socket server, Writing messages to the nexos socket server – Grass Valley K2 Edge Protocol Manual v1.0 User Manual

Page 7: Waiting for feedback

Advertising
background image

K2 Edge Protocol Manual – document version 1.0 – Page 7

3.3. Code examples

This section provides a number of coding examples.

Note that the examples below describe a situation in which a POSIX socket API is used (this API is used

on most UNIX systems, including Linux and OSX). Windows systems use a different, but very similar API.

3.4. Connecting with the nexos socket server

To connect as a client with the nexos socket server, perform the following steps:

1. Create a socket with:

int fd = socket( AF_INET, SOCK_STREAM, 0 );

2. Fill a

struct sockaddr_in object with the address info of the remote nexos socket server:

struct sockaddr_in sockAddress;

sockAddress.sin_family = AF_INET;

sockAddress.sin_addr.s_addr = inet_addr( remote-host-dot-quad-string );

sockAddress.sin_port = htons( 5001 );

int addressLn = sizeof( sockAddress );

3. Connect the socket with the nexos socket server with:

result = connect( fd, &sockAddr, addressLn );

Make sure to check result values of both the

socket() and connect() calls. fd should represent a

properly connected socket link with the nexos socket server.

3.5. Writing messages to the nexos socket server

The socket file descriptor can be used to

write(2) messages to the socket server, or alternatively

send(2) can be used.

3.6. Waiting for feedback

If feedback is wanted, feedback messages sent by nexos will be returned over the same socket link. To

receive these messages, the client can use

read(2) or recv(2) on the socket fd to wait and read the

feedback message data.

To test for available feedback data without blocking the current thread, consider non-blocking read,

ioctl(2) or select(2). Or for a more advanced approach, consider using a dedicated thread that

does the reading part and simply blocks on

read( fd ).

Advertising