GIO socket client server for multiple clients - sockets

I want to implement a GIO socket client server program where the server can serve up to say 5 clients at the same time. Is this even possible? How can I modify the following standard server to allow multiple clients to connect in a thread safe way? The incoming_callback() function will receive data from a client and do some processing and respond an acknowledgement and this will continue till the client sends an "exit" message at which point the server will close the client connection. I want to do this for a max of say 5 connections at a time for the server. Is it possible and if so how?
int
main (int argc, char **argv)
{
/* initialize glib */
g_type_init();
GError * error = NULL;
/* create the new socketservice */
GSocketService * service = g_socket_service_new ();
/* connect to the port */
g_socket_listener_add_inet_port ((GSocketListener*)service,
1500, /* your port goes here */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
/* listen to the 'incoming' signal */
g_signal_connect (service,
"incoming",
G_CALLBACK (incoming_callback),
NULL);
/* start the socket service */
g_socket_service_start (service);
/* enter mainloop */
g_print ("Waiting for client!\n");
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
return 0;
}

The magic should happen within your incoming_callback, returning as fast as possible and pushing the work into another GThread (or even better, a GThreadPool)

Related

Can I use ZeroMQ sockets to change communication mechanism between two microservices that use REST?

How can we transform cleanly a communication based on HTTP API --> to a message communication using ZMQ library ?
In case you indeed want to do so, one may design a kind of Mediator, using ZeroMQ tools.
ZeroMQ has a set of multi-level abstractions, where AccessPoints, typically have a certain "behaviour" ( a distributed behaviour ) they perform among themselves.
Your indicated target aims at using no such behaviour, but to have some sort of transparent, (almost) wire-level handling of data-flows.
For this very purpose let me direct your kind attention first to the concept:
- ZeroMQ Hierarchy in Less than Five Seconds
and next to a possible tool, feasible to help in the given task:
-ZMQ_STREAM Scalable Formal Communication Archetype ( for an AccessPoint )
A socket of type ZMQ_STREAM is used to send and receive TCP data from a non-ØMQ peer, when using the tcp:// transport. A ZMQ_STREAM socket can act as client and/or server, sending and/or receiving TCP data asynchronously.
When receiving TCP data, a ZMQ_STREAM socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers.
When sending TCP data, a ZMQ_STREAM socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.
To open a connection to a server, use the zmq_connect call, and then fetch the socket identity using the ZMQ_IDENTITY zmq_getsockopt call.
To close a specific connection, send the identity frame followed by a zero-length message (see EXAMPLE section).
When a connection is made, a zero-length message will be received by the application. Similarly, when the peer disconnects (or the connection is lost), a zero-length message will be received by the application.
You must send one identity frame followed by one data frame. The ZMQ_SNDMORE flag is required for identity frames but is ignored on data frames.
Example:
/* Create Context-Engine */
void *ctx = zmq_ctx_new (); assert (ctx);
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM); assert (socket);
int rc = zmq_bind (socket, "tcp://*:8080"); assert (rc == 0);
/* Data structure to hold the ZMQ_STREAM ID */
uint8_t id [256];
size_t id_size = 256;
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
while (1) {
/* Get HTTP request; ID frame and then request */
id_size = zmq_recv (socket, id, 256, 0); assert (id_size > 0);
do {
raw_size = zmq_recv (socket, raw, 256, 0); assert (raw_size >= 0);
} while (raw_size == 256);
/* Prepares the response */
char http_response [] =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello, World!";
/* Sends the ID frame followed by the response */
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
zmq_send (socket, http_response, strlen (http_response), 0);
/* Closes the connection by sending the ID frame followed by a zero response */
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
zmq_send (socket, 0, 0, 0);
}
zmq_close (socket); zmq_ctx_destroy (ctx); /* Clean Close Sockets / Terminate Context */

GTK GIO How to read reply sent by server

I wanted to write a client server thing using gio socket in gtk and I found a sample code to send data to server but, the more thing i want is to read the data/reply sent by the server. The below is sample code
#include <glib.h>
#include <gio/gio.h>
int main (int argc, char *argv[])
{
/* initialize glib */
g_type_init ();
GError * error = NULL;
/* create a new connection */
GSocketConnection * connection = NULL;
GSocketClient * client = g_socket_client_new();
/* connect to the host */
connection = g_socket_client_connect_to_host (client,
(gchar*)"localhost",
1500, /* your port goes here */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
else
{
g_print ("Connection successful!\n");
}
/* use the connection */
GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
g_output_stream_write (ostream,
"Hello server!", /* your message goes here */
13, /* length of your message */
NULL,
&error);
/* don't forget to check for errors */
if (error != NULL)
{
g_error (error->message);
}
return 0;
}
The above code works fine for the sending data to server but when i try to read it form input stream it goes in to block state. My read message function look like this
void readMessage()
{
char buffer[2048];
GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM(connection));
gssize bytes;
bytes = g_input_stream_read(istream, buffer, sizeof buffer, NULL, NULL);
buffer[bytes] = '\0';
g_print ("%"G_GSSIZE_FORMAT" bytes read: %s\n", bytes, buffer);
}
g_input_stream_read() is documented as blocking until it receives as many bytes as you request (in this case, 2048), or until the connection is closed. Presumably, neither of those things are happening. How big is the reply from the server? Does it close the connection after sending its reply?
Bear in mind that g_socket_client_connect_to_host() opens a TCP connection, so you should expect to be doing stream-based I/O here, rather than message-based I/O. If you expect to be sending messages to and from the server, you will need a framing protocol within TCP.

ios - Determine if a certain address with a certain port is reachable

I want to know if my server is online via my ios application. Here's what I'm doing:
Boolean result;
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFDataRef)(serverIPAddress)); //serverIPAdress = "10.10.10.100:5010"
if(hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
}
if (!result) { //This means that the host was unreachable
return ;
}
My server is online and I can access it later on in the code(meaning that my connection to the server works perfectly fine). However, I want to be able to detect if my server, on a certain port, is reachable.
Also, if I remove the ":5010" from the ip address, it detects that my server is online (it doesn't go in the "!result" condition) and detects that my server is offline if I put "10.10.10.253" which corresponds to no ip address on my network.
How can I manage to determine if my server is online or not ?
I've looked at this question : Reachability with Address - Server AND Port - iOS 5 but it doesn't work since it always return that it is reachable no matter what ip address I enter
Thanks in advance
One approach could be to open a socket connection to a specific port to see if you get any response back. If not, then the destination is unreachable. For example
#include <arpa/inet.h> //for PF_INET, SOCK_STREAM, IPPROTO_TCP etc
CFRunLoopSourceRef gSocketSource;
void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
UInt8 buffer[1024];
bzero(buffer, sizeof(buffer));
CFSocketNativeHandle sock = CFSocketGetNative(socket); // The native socket, used recv()
//check here for correct connect output from server
recv(sock, buffer, sizeof(buffer), 0);
printf("Output: %s\n", buffer);
if (gSocketSource)
{
CFRunLoopRef currentRunLoop = CFRunLoopGetCurrent();
if (CFRunLoopContainsSource(currentRunLoop, gSocketSource, kCFRunLoopDefaultMode))
{
CFRunLoopRemoveSource(currentRunLoop, gSocketSource, kCFRunLoopDefaultMode);
}
CFRelease(gSocketSource);
}
if (socket) //close socket
{
if (CFSocketIsValid(socket))
{
CFSocketInvalidate(socket);
}
CFRelease(socket);
}
}
void ConnectSocket()
{
//socket
CFSocketContext context = {0, NULL, NULL, NULL, NULL};
CFSocketRef theSocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketConnectCallBack , (CFSocketCallBack)ConnectCallBack, &context);
//address
struct sockaddr_in socketAddress;
memset(&socketAddress, 0, sizeof(socketAddress));
socketAddress.sin_len = sizeof(socketAddress);
socketAddress.sin_family = AF_INET;
socketAddress.sin_port = htons(5010);
socketAddress.sin_addr.s_addr = inet_addr("10.10.10.253");
gSocketSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, theSocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), gSocketSource, kCFRunLoopDefaultMode);
CFDataRef socketData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&socketAddress, sizeof(socketAddress));
CFSocketError status = CFSocketConnectToAddress(theSocket, socketData, 30); //30 second timeout
//check status here
CFRelease(socketData);
}
Basically, if the server is unreachable at that port, you will most likely get a kCFSocketTimeout for CFSocketError status. If you are looking to parse a specific response back from the server to determine if the server is ready or not, the ConnectCallBack function will be called upon successful socket connection.
This is just a simple example, make sure not to block the UI by calling socket connections on the main thread such as recv()

GetQueuedCompletionStatus blocks forever

I'm writing a server application and I want to use IOCompletion ports, so I wrote a prototype for the server, but I'm facing a problem with GetQueuedCompletionStatus that it never returns(it blocks). Below is my code:
bool CreateSocketOverlappedServer()
{
WSADATA wsaData;
SOCKADDR_IN sockaddr;
if(WSAStartup(MAKEWORD(2,2,),&wsaData)){
_tprintf(_T("Unable to start up\n"));
return false;
}
SrvSocket = WSASocket(AF_INET,SOCK_STREAM,0,NULL,NULL,WSA_FLAG_OVERLAPPED);
if(SrvSocket==INVALID_SOCKET){
_tprintf(_T("Unable to start socket\n"));
return false;
}
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(10000);
sockaddr.sin_addr.s_addr = INADDR_ANY;
/* now bind the socket */
if(bind(SrvSocket, (SOCKADDR *)&sockaddr, sizeof(SOCKADDR_IN))==SOCKET_ERROR){
_tprintf(_T("Unable to bind socket\n"));
return false;
}
if(listen(SrvSocket, 5)==SOCKET_ERROR){
_tprintf(_T("Error listening\n"));
return false;
}
return true;
}
void WorkerThread(void *arg)
{
bool bret= false;
DWORD dwTransferedBytes=0;
CLIENTS *client;
PPER_IO_OPERATION_DATA data;
/* Just sleep for now */
while(true){
_tprintf(_T("Entering while\n"));
bret = GetQueuedCompletionStatus(hIocp,&dwTransferedBytes,(PULONG_PTR)&client,(LPOVERLAPPED *) &data,INFINITE);
if(!bret){
_tprintf(_T("Unable to process completion port\n"));
}
}
//Sleep(10000);
}
void AcceptClientConnections(void *arg)
{
SOCKET ClientSocket;
CLIENTS *c;
_tprintf(_T("Start accepting client connections\n"));
while(true){
ClientSocket = accept(SrvSocket, NULL,NULL);
if(ClientSocket==INVALID_SOCKET){
_tprintf(_T("Unable to accept connection\n"));
continue;
}
/* do an association with completion port */
c = (CLIENTS *)malloc(sizeof(CLIENTS));
c->sock = ClientSocket;
/* associate with completion port */
if(!CreateIoCompletionPort((HANDLE)ClientSocket, hIocp, (ULONG_PTR)c,0)){
_tprintf(_T("Unable to associate with completion port\n: %d"),GetLastError());
}
}
}
Any idea?
thanks in advance.
You are not using the Completion Port correctly, so it has nothing to do, and thus no status to report. Using a Completion Port with sockets is a two-step process, but you are only doing half of the steps.
Read the following MSDN article for details:
Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports
ONe possibility: Check if you have associated the listener socket with the completion port (made this mistake myself). If you haven't the GetQueuedCompletionStatus() will block forever.

GIO example for opening a server port?

Can you please tell me if there is any example for using GIO Server Socket
(the one which I can open a port and listen on socket requests)?
I would like to use it to 'remote-control' my GTK+ application.
I think you should do something like this:
#define MY_PORT 47110
/* Listener callback, this gets called by GTK+ when
* there's socket activity to handle.
*/
static gboolean cb_listener(GIOChannel *source, GIOCondition condition, gpointer data
{
switch(condition)
{
case G_IO_IN:
/* There's data to be read. */
break;
default:
/* An error has occured, or socket is closed. */
return FALSE; /* This tells GIO to remove the source, might be drastic. */
}
return TRUE; /* This tells GIO that all is fine. */
}
Then elsewhere (in a function, maybe main()):
GSocketListener *listener;
listener = g_socket_listener_new();
g_socket_listener_add_inet_port(listener, MY_PORT, NULL, NULL);
g_io_add_watch(G_IO_CHANNEL(listener), G_IO_IN | G_IO_ERR | G_IO_HUP, cb_listener, NULL);