d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Help For School Project Part 2 > Socket Client/server Cross-platform
Add Reply New Topic New Poll
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Mar 14 2016 08:50pm
Hello again.

The new project idea is to attach a photo diode to the testing board of a Raspberry Pi and detect light. When the lights go out we want to be able to send a signal to computer to run a shutdown script over wifi.
The Pi will be running Raspbian (Debian/Linux).
And the PC will be running Windows.

Need to set up a simple client server connection between the two (I think) with C.

Any help would appreciated. Currently installing Debian on a virtual box and WinSDK.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 14 2016 09:12pm
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
Retired Moderator
Posts: 38,135
Joined: May 27 2006
Gold: 3,835.50
Trader: Trusted
Mar 14 2016 09:27pm
Quote (AbDuCt @ Mar 14 2016 11:12pm)
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;
}


Unlike an Arduino (C), pretty sure Python will work just fine with a Pi.

This post was edited by ArtofApocalypse on Mar 14 2016 09:27pm
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Mar 14 2016 09:33pm
Yes, unfortunately is has to be with C. Requirement of the project.

I found this online and trying to get it to connect to google's ip but no dice yet.

Code
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");


server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );

//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}

puts("Connected");

return 0;
}


/edit

Rookie mistake. Google's Ip has changed. This is working.

This post was edited by ROM on Mar 14 2016 09:38pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 14 2016 09:50pm
You didn't even answer any of the questions in my post, besides the first one regarding the languages. If you want cross platform code you can also check out libBoost since libmill only works on Linux platforms.

Writing to sets of code is bothersome since the only way to get a write-once-compile-everywhere version of your project would be to spam ifdef statements everywhere.

Unless you feel like writing two 100 line projects for each OS.

This post was edited by AbDuCt on Mar 14 2016 09:55pm
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Mar 14 2016 10:01pm
Quote (AbDuCt @ Mar 14 2016 11:50pm)
You didn't even answer any of the questions in my post, besides the first one regarding the languages. If you want cross platform code you can also check out libBoost since libmill only works on Linux platforms.

Writing to sets of code is bothersome since the only way to get a write-once-compile-everywhere version of your project would be to spam ifdef statements everywhere.

Unless you feel like writing two 100 line projects for each OS.


One side has to be windows and the other linux.
This only needs to work on the Pi and to one computer.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 14 2016 10:12pm
Quote (ROM @ Mar 15 2016 12:01am)
One side has to be windows and the other linux.
This only needs to work on the Pi and to one computer.


Ah well carry on. For your purpose googling "Send string over WINSOCK TCP connection" will likely yield all the source code you need to complete the project. Linux stuff is equally as easy.

Some related functions you will want to look up and use might be, strtok, strcmp, snprintf, and then the related send, sendto, recv functions for dealing with the socket itself.

This post was edited by AbDuCt on Mar 14 2016 10:17pm
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Mar 14 2016 10:25pm
Quote (AbDuCt @ Mar 15 2016 12:12am)
Ah well carry on. For your purpose googling "Send string over WINSOCK TCP connection" will likely yield all the source code you need to complete the project. Linux stuff is equally as easy.

Some related functions you will want to look up and use might be, strtok, strcmp, snprintf, and then the related send, sendto, recv functions for dealing with the socket itself.


Thank you sir.
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Mar 18 2016 08:52am
Anyone have any experience writing c code that detects a voltage?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll