Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:scripts:tcpclient [2015/05/05 15:04] (current)
Line 1: Line 1:
 +====== SDK Script tcpclient.are ======
 +<code c tcpclient.are>​
 +/* DESC: This script sends a message to a TCP server.
 + * Copyright (C) 2012 NetModule AG, Switzerland
 + */
 +
 +void usage() ​
 +{
 +    printf("​usage:​ tcpclient.are <​server>​ <​port>​ <​msg>​\n"​);​
 +    exit(1);
 +}
 +
 +if (argc < 4) {
 +    usage();
 +}
 +
 +server = argv[1];
 +port = (int) argv[2];
 +msg = "";​
 +for (i = 3; i < argc; i++)  {
 +   msg = sprintf("​%s %s",​msg,​ argv[i]); ​
 +}
 +
 +
 +sock = socket(AF_INET,​ SOCK_STREAM,​ IPPROTO_TCP);​
 +if (sock < 0) {
 +    print("​unable to open socket\n"​);​
 +    exit(1);
 +}
 +
 +if (connect(sock,​ server, port) < 0){
 +    printf("​Could not connect to %s (port %d)\n",​ server, port);
 +    close(sock);​
 +    exit(1);
 +}
 +
 +sent = send(sock, msg);
 +if (sent == -1) {
 +    printf("​Failed to send message\n"​);​
 +    close(sock);​
 +    exit(1);
 +}
 +
 +printf("​Sent %d bytes\n",​ sent);
 +
 +exit(0);
 +
 +</​code>​