Does it have to be in C? Using a scripting language like Ruby or Python with an XMLRPC library will allow you to vastly expand functionality quickly and easily. Not to mention it would be cross platform natively and you would not have to fight with two different APIs (winsock api and Linux sys calls).
For instance with Ruby you can do:
Server:
Code
require "xmlrpc/server"
server.add_handler("shutdown.server") do |ip|
#run shutdown script or other code using the variable given to us by the client called ip
end
Client:
Code
require "xmlrpc/client"
client = XMLRPC::Client.new("localhost", "/", 1234)
client.call("shutdown.server", "127.0.0.1")
If you do have to use C does one side have to be Windows or can both be Linux? If both sides can be Linux the LIBMILL library offers a nice abstraction over the TCP related function calls as well as provides co-routines to provide async like functionality.
For example here is a libmill TCP server with async client handling so that the server will not block while waiting on a message from a client:
Code
#include <libmill.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
coroutine void recvmessage(tcpsock accepter) {
char input[256] = {0};
size_t sz = tcprecvuntil(accepter, input, sizeof(input), "\r\n", 2, -1);
input[sz-1] = 0;
//Parse input array for your command.
tcpclose(accepter);
}
int main(int argc, char *argv[]) {
int port = 5555;
ipaddr addr = iplocal(NULL, port, 0);
tcpsock listener = tcplisten(addr, 10);
if(!listener) {
perror("Can't open the listening socket");
return 1;
}
while(1) {
tcpsock accepter = tcpaccept(listener, -1);
go(recvmessage(accepter));
}
return 0;
}
This post was edited by AbDuCt on Mar 14 2016 09:17pm