Hi,
well, the best thing you can do in this case is a google lookup for standard Server-Client code snippets for Windows. However, before doing so, you have to think a little bit about your network setup. As in each two-way network scenario, there must be a server (which runs during all the time in the background and for each incoming requests it starts a new thread for managing the communication) and a client (which would initiate and terminate the communication with the server in the proper way). So, the first thing is, you need to know who is the 'server' and who is the 'client' in your case (unfortunately I don't know neither Processing nor Arduino, so I've no idea of how networking works there). If you're lucky, then they would act as servers, in which case you don't need to take care of the server tasks in Max, you'd just need to implement a simple client.
Here's the code that I'm using in [sadam.udpSender]. Feel free to use it and extend it according to your needs. This is not a trivial task, however - you'd need to understand what you're doing anyway.
The ERR_* macros are just error messages that I define earlier, you can ignore them. The rest of the definitions are in the header files that you need to include (in the WIN_VERSION and MAC_VERSION blocks):
#ifdef WIN_VERSION
#include
#endif
#include
#include
#include
#include
#include "ext.h"
#include "ext_obex.h"
#include "ext_globalsymbol.h"
#include "ext_systhread.h"
#include "sadam.stream.h"
#ifdef MAC_VERSION
#include
#include
#include
#include
#endif
And here is the function that actually sends the data:
void UDPSender::sendData ( std::vector < unsigned char > & data ) {
struct sockaddr_in localAddress;
struct sockaddr_in serverAddress;
struct hostent * hPrt;
t_symbol * localHost;
unsigned char * buffer;
unsigned char * iterator;
unsigned long localPort;
unsigned long size;
long result;
#ifdef WIN_VERSION
SOCKET socketDescriptor;
WSADATA wsaData;
#else
int socketDescriptor;
#endif
if ( data.empty ( ) ) {
return;
}
localHost = host;
localPort = port;
// Create socket
#ifdef WIN_VERSION
if ( ( result = WSAStartup ( MAKEWORD(1, 1), & wsaData ) ) != 0 ) {
if ( verbose ) {
object_error ( ( t_object * ) this, ERR_WSA );
}
outlet_int ( outlet, result );
return;
}
socketDescriptor = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if ( socketDescriptor == INVALID_SOCKET ) {
WSACleanup();
#else
socketDescriptor = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if ( socketDescriptor < 0 ) {
#endif
if ( verbose ) {
object_error ( ( t_object * ) this, ERR_SOCKET, localHost->s_name );
}
outlet_int ( outlet, errno );
return;
}
// Bind port
localAddress.sin_family = AF_INET;
localAddress.sin_addr.s_addr = htonl ( INADDR_ANY );
localAddress.sin_port = htons ( 0 );
if ( bind ( socketDescriptor, ( struct sockaddr * ) & localAddress, sizeof ( localAddress ) ) != 0 ) {
if ( verbose ) {
object_error ( ( t_object * ) this, ERR_BIND, localPort, localHost->s_name );
}
outlet_int ( outlet, errno );
#ifdef WIN_VERSION
closesocket ( socketDescriptor );
WSACleanup();
#else
close ( socketDescriptor );
#endif
return;
}
// Get server address
hPrt = gethostbyname ( localHost->s_name );
if ( hPrt == NULL ) {
result = -1;
if ( verbose ) {
object_error ( ( t_object * ) this, ERR_HOST, localHost->s_name );
}
outlet_int ( outlet, result );
return;
}
serverAddress.sin_family = hPrt->h_addrtype;
memcpy ( ( char * ) & serverAddress.sin_addr.s_addr, hPrt->h_addr_list [ 0 ], hPrt->h_length );
serverAddress.sin_port = htons ( localPort );
// Send data
size = data.size ( );
buffer = new unsigned char [ size ];
iterator = buffer;
for ( std::vector < unsigned char >::iterator i = data.begin ( ); i != data.end ( ); ++ i ) {
* iterator ++ = * i;
}
iterator = buffer;
result = sendto ( socketDescriptor, ( const char * ) iterator, size, 0, ( const struct sockaddr * ) & serverAddress, sizeof ( serverAddress ) );
if ( result < 0 ) {
if ( verbose ) {
object_error ( ( t_object * ) this, ERR_SEND, host->s_name, localPort );
}
outlet_int ( outlet, result );
}
delete [ ] buffer;
outlet_int ( outlet, 0 );
#ifdef WIN_VERSION
closesocket ( socketDescriptor );
WSACleanup();
#else
close ( socketDescriptor );
#endif
}
}