예제 #1
0
/**
  Performs an HTTP GET operation to the path at the address / port specified.

  The data returned from the web server (up to maxresponse bytes) is written into the given buffer.

  @param hostname The name of the host to connect to.
  @param path The path on the server to connect to.
  @param port The port to connect on - standard http port is 80
  @param response The buffer read the response back into.
  @param maxresponse An integer specifying the size of the response buffer.
  @param headers (optional) An array of strings to be sent as headers - last element in the array must be 0.
  @return the number of bytes received, or < 0 on error.

  \b Example
  \code
  #define BUF_LENGTH 100
  char myBuffer[BUF_LENGTH];
  int justGot = webclientGet("www.makingthings.com", "/test/path", 80, myBuffer, BUF_LENGTH, 0);
  \endcode
  Now we should have the results of the HTTP GET from \b www.makingthings.com/test/path in \b myBuffer.
*/
int webclientGet(const char* hostname, const char* path, int port, char* response, int maxresponse, const char* headers[])
{
    int s = tcpOpen(networkGetHostByName(hostname), port);
    if (s > -1) {
        // construct the GET request
        int len = sniprintf(webclientBuf, WEBCLIENT_BUFFER_SIZE, "GET %s HTTP/1.1\r\n%s%s%s",
                            path,
                            (hostname != NULL) ? "Host: " : "",
                            (hostname != NULL) ? hostname : "",
                            (hostname != NULL) ? "\r\n" : ""  );
        tcpWrite(s, webclientBuf, len);

        if (headers != NULL) {
            for ( ; *headers != 0; headers++) {
                tcpWrite(s, *headers, strlen(*headers));
                tcpWrite(s, "\r\n", 2);
            }
        }

        if (tcpWrite(s, "\r\n", 2 ) < 0) { // all done with headers...just check our last write here...
            tcpClose(s);
            return -1;
        }

        // read the data into the given buffer until there's none left, or the passed in buffer is full
        len = webclientReadResponse(s, response, maxresponse);
        tcpClose(s);
        return len;
    }
    return -1;
}
예제 #2
0
/**
  Performs an HTTP POST operation to the path at the address / port specified.
  A buffer with the data to be POSTed is passed in, and up to maxresponse bytes of the response
  from the server are written back into the same buffer.
  @param hostname The name of the host to connect to.
  @param path The path on the server to post to.
  @param port The port to connect on - standard http port is 80
  @param data The buffer to write from, and then read the response back into
  @param data_length The number of bytes to write from \b data
  @param maxresponse How many bytes of the response to read back into \b data
  @param headers (optional) An array of strings to be sent as headers - last element in the array must be 0.
  @return The number of bytes written, or -1 on failure.

  \b Example
  \code
  // we'll post a test message to www.makingthings.com/post/path
  int bufLength = 100;
  char myBuffer[bufLength];
  int datalength = siprintf(myBuffer, "A test message to post"); // load the buffer with some data to send
  webclientPost("www.makingthings.com", "/post/path", 80, myBuffer, datalength, bufLength, 0);
  \endcode
*/
int webclientPost(const char* hostname, const char* path, int port, char* data, int data_length, int maxresponse, const char* headers[])
{
    int s = tcpOpen(networkGetHostByName(hostname), port);
    if (s > -1) {
        int len = sniprintf(webclientBuf, WEBCLIENT_BUFFER_SIZE,
                            "POST %s HTTP/1.1\r\nContent-Length: %d\r\n%s%s%s",
                            path, data_length,
                            (hostname != NULL) ? "Host: " : "",
                            (hostname != NULL) ? hostname : "",
                            (hostname != NULL) ? "\r\n" : "");
        tcpWrite(s, webclientBuf, len);

        if (headers != NULL) {
            for ( ; *headers != 0; headers++) {
                tcpWrite(s, *headers, strlen(*headers));
                tcpWrite(s, "\r\n", 2);
            }
        }
        tcpWrite(s, "\r\n", 2); // all done with headers

        // send the body...just check our last write here...
        if (tcpWrite(s, data, data_length) <= 0) {
            tcpClose(s);
            return -1;
        }

        // read back the response
        len = webclientReadResponse(s, data, maxresponse);
        tcpClose(s);
        return len;
    }
    return -1;
}
예제 #3
0
int eMoviePlayer::requestStream()
{
	char ioBuffer[512];

	eDebug("[MOVIEPLAYER] requesting VLC stream... %s:%s", server.serverIP.c_str(), server.streamingPort.c_str());

	fd = tcpOpen(server.serverIP, atoi(server.streamingPort.c_str()), 10);
	if (fd < 0)
	{
		eDebug("[MOVIEPLAYER] tcpOpen failed.");
		return - 1;
	}
	strcpy(ioBuffer, "GET /dboxstream HTTP/1.0\r\n\r\n");
	if (tcpRequest(fd, ioBuffer, sizeof(ioBuffer) - 1) < 0)
	{
		eDebug("[MOVIEPLAYER] get stream request failed.");
		close(fd);
		return -2;
	}
	
	if (strstr(ioBuffer, "HTTP/1.0 200 OK") == 0)
	{
		eDebug("[MOVIEPLAYER] 200 OK not received...");
		close(fd);
		return -3;
	}
	else
	{
		eDebug("[MOVIEPLAYER] 200 OK...");
	}
	return 0;
}
예제 #4
0
int eMoviePlayer::sendRequest2VLC(eString command)	// sending tcp commands
{
	char ioBuffer[512];
	int rc = -1;
	int fd = tcpOpen(server.serverIP, atoi(server.webifPort.c_str()), 1);
	if (fd > 0)
	{
		eString url = "GET /" + command + " HTTP/1.1\r\n\r\n";
		strcpy(ioBuffer, url.c_str());
		eDebug("[MOVIEPLAYER] sendRequest2VLC : %d, %s", fd, ioBuffer);
		rc = tcpRequest(fd, ioBuffer, sizeof(ioBuffer) - 1);
		if (rc == 0)
		{
			if (strstr(ioBuffer, "HTTP/1.1 200 OK") == 0)
			{
				eDebug("[MOVIEPLAYER] 200 OK NOT received...");
				rc = -2;
			}
			else 
			{
				eDebug("[MOVIEPLAYER] 200 OK...");
			}
		}
		else 
			rc = -3;
		close(fd);
	}
	
	return rc;
}
예제 #5
0
int Net::serverExecute() {
    int i=0;
    printf("init done!\n");
    int quit = 0;

    ipPeer.resolvehost("NULL", 9999);
    tcpOpen();
    ipPeer.resolveIP();

    printf("main loop\n");
    while (running) {
        if (tcpAccept()) {
            tcpGetPeerAddress();
            fprintf(stdout, "client connected:\n");
            quit = 0;
            while (!quit) {
                if ((SDLNet_TCP_Recv(socketPeer, buffer, DIMBUFFER) > 0)) {
                    fprintf(stdout, "reci:\t%s", buffer);
                    snprintf(buffer, DIMBUFFER, "server123\t%d\n", i);
                    tcpSend(socketPeer, buffer);
                    fprintf(stdout, "send:\t%s", buffer);
                }
                //                if ((strcmp(buffer, "exit") == 0)) {
                //                    fprintf(stdout, "session terminated\n");
                //                    quit2 = 1;
                //                }
                //                if ((strcmp(buffer, "quit") == 0)) {
                //                    fprintf(stdout, "programm terminated\n");
                //                    quit2 = 1;
                //                    quit = 1;
                //                }
                //                SDL_Delay(100);
                i++;
            }
        }
        SDL_Delay(100);
    }
    //    SDLNet_TCP_Close(socketClient);

    cleanup();
    return 0;
}
예제 #6
0
파일: main.cpp 프로젝트: roland-wilhelm/iot
//............................................................................
int main(int argc, char *argv[]) {
    int optChar;
    char comPort[FNAME_SIZE];
    char inpFileName[FNAME_SIZE];
    char outFileName[FNAME_SIZE];
    char savFileName[FNAME_SIZE];
    char matFileName[FNAME_SIZE];
    TargetLink link = NO_LINK;
                                              // default configuration options
    strcpy(inpFileName, "qs.bin");
    strcpy(comPort, "COM1");
    int tcpPort          = 6601;
    int baudRate         = 38400;
    uint8_t quiet        = 0;
    uint8_t tstampSize   = 4;
    uint8_t objPtrSize   = 4;
    uint8_t funPtrSize   = 4;
    uint8_t sigSize      = 1;
    uint8_t evtSize      = 2;
    uint8_t queueCtrSize = 1;
    uint8_t poolCtrSize  = 2;
    uint8_t poolBlkSize  = 2;
    uint8_t tevtCtrSize  = 2;
    outFileName[0]       = '\0';                  // Output file not specified
    savFileName[0]       = '\0';                    // Save file not specified
    matFileName[0]       = '\0';                  // Matlab file not specified

    time_t now = time(NULL);
    printf("QSPY host application %s\n"
           "Copyright (c) Quantum Leaps, LLC.\n"
           "%s\n", QSPY_VER, ctime(&now));

    static char const help[] =
        "Syntax is: qspy [options]          * = default\n\n"
        "OPTION                    DEFAULT  COMMENT\n"
        "----------------------------------------------------------------\n"
        "-h                                 help (this message)\n"
        "-q                                 quiet mode (no stdout output)\n"
        "-o<File_name>                      produce output to a file\n"
        "-s<File_name>                      save the binary data to a file\n"
        "-m<File_name>                      produce a Matlab file\n"
        "-c<COM_port>  *           COM1     com port input\n"
        "-b<Baud_rate>             38400    baud rate selection\n"
        "-f<File_name>             qs.bin   file input\n"
        "-t                                 TCP/IP input\n"
        "-p<TCP_Port_number>       6601     TCP/IP server port\n"
        "-T<tstamp_size>           4        QS timestamp size (bytes)\n"
        "-O<pointer_size>          4        object pointer size (bytes)\n"
        "-F<pointer_size>          4        function pointer size (bytes)\n"
        "-S<signal_size>           1        signal size (bytes)\n"
        "-E<event_size>            2        event size size (bytes)\n"
        "-Q<queue_counter_size>    1        queue counter size (bytes)\n"
        "-P<pool_counter_size>     2        pool counter size (bytes)\n"
        "-B<pool_blocksize_size>   2        pool blocksize size (bytes)\n"
        "-C<QTimeEvt_counter_size> 2        QTimeEvt counter size\n";

    while ((optChar = getopt(argc, argv,
                             "hqo:s:m:c:b:tp:f:T:O:F:S:E:Q:P:B:C:")) != -1)
    {
        switch (optChar) {
            case 'q': {                                          // quiet mode
                quiet = 1;
                break;
            }
            case 'o': {                                         // file output
                strncpy(outFileName, optarg, sizeof(outFileName));
                printf("-o %s\n", outFileName);
                break;
            }
            case 's': {                          // save binary data to a file
                strncpy(savFileName, optarg, sizeof(savFileName));
                printf("-s %s\n", savFileName);
                break;
            }
            case 'm': {                                  // Matlab file output
                strncpy(matFileName, optarg, sizeof(matFileName));
                printf("-m %s\n", matFileName);
                break;
            }
            case 'c': {                                            // COM port
                if ((link != NO_LINK) && (link != SERIAL_LINK)) {
                    printf("The -c option is incompatible with -p/-f\n");
                    return -1;                                      // failure
                }
                strncpy(comPort, optarg, sizeof(comPort));
                printf("-c %s\n", comPort);
                link = SERIAL_LINK;
                break;
            }
            case 'b': {                                           // baud rate
                if ((link != NO_LINK) && (link != SERIAL_LINK)) {
                    printf("The -b option is incompatible with -p/-f\n");
                    return -1;                                      // failure
                }
                if (sscanf(optarg, "%d", &baudRate) != 1) {
                    printf("incorrect baud rate: %s\n", optarg);
                    return -1;                                      // failure
                }
                printf("-b %d\n", baudRate);
                link = SERIAL_LINK;
                break;
            }
            case 'f': {                                          // File input
                if (link != NO_LINK) {
                    printf("The -f option is incompatible with -c/-b/-p\n");
                    return -1;                                      // failure
                }
                strncpy(inpFileName, optarg, sizeof(inpFileName));
                printf("-f %s\n", inpFileName);
                link = FILE_LINK;
                break;
            }
            case 't': {                                        // TCP/IP input
                if ((link != NO_LINK) && (link != TCP_LINK)) {
                    printf("The -t option is incompatible with -c/-b/-f\n");
                    return -1;
                }
                printf("-t\n");
                link = TCP_LINK;
                break;
            }
            case 'p': {                                         // TCP/IP port
                if ((link != NO_LINK) && (link != TCP_LINK)) {
                    printf("The -p option is incompatible with -c/-b/-f\n");
                    return -1;
                }
                tcpPort = (int)strtoul(optarg, NULL, 10);
                printf("-p %d\n", tcpPort);
                link = TCP_LINK;
                break;
            }
            case 'T': {                                      // timestamp size
                tstampSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'F': {                               // function pointer size
                funPtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'O': {                                 // object pointer size
                objPtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'S': {                                         // signal size
                sigSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'E': {                                         // event size
                evtSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'Q': {                                  // Queue counter size
                queueCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'P': {                            // Memory-pool counter size
                poolCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'B': {                          // Memory-pool blocksize size
                poolBlkSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'C': {                             // Time event counter size
                tevtCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'h':                                                  // help
            default: {                                       // unknown option
                printf(help);
                return -1;                                     // error return
            }
        }
    }
    if (argc != optind) {
        printf(help);
        return -1;
    }

    // configure the Quantum Spy
    FILE *outFile = (outFileName[0] != '\0'
                     ? fopen(outFileName, "w")
                     : (FILE *)0);
    FILE *matFile = (matFileName[0] != '\0'
                     ? fopen(matFileName, "w")
                     : (FILE*)0);
    FILE *savFile = (savFileName[0] != '\0'
                     ? fopen(savFileName, "wb")     // open for writing binary
                     : (FILE*)0);
    qsConfig(quiet,
             objPtrSize,
             funPtrSize,
             tstampSize,
             sigSize,
             evtSize,
             queueCtrSize,
             poolCtrSize,
             poolBlkSize,
             tevtCtrSize,
             outFile,
             matFile);

    static unsigned char buf[BUF_SIZE];
    int n;

    switch (link) {
        case NO_LINK:                            // intentionally fall through
        case SERIAL_LINK: {           // input trace data from the serial port
            if (!comOpen(comPort, baudRate)) {
                return -1;
            }
            else {
                printf("\nSerial port %s opened, hit any key to quit...\n\n",
                       comPort);
            }
            while ((n = comRead(buf, sizeof(buf))) != -1) {
                if (n > 0) {
                    qsParse(buf, n);
                    if (savFile != (FILE *)0) {
                        fwrite(buf, 1, n, savFile);
                    }
                }
            }
            comClose();
            break;
        }
        case FILE_LINK: {                      // input trace data from a file
            FILE *f = fopen(inpFileName, "rb");     // open for reading binary
            if (f == (FILE *)0) {
                printf("file %s not found\n", inpFileName);
                return -1;
            }
            do {
                n = fread(buf, 1, sizeof(buf), f);
                qsParse(buf, n);
            } while (n == sizeof(buf));

            fclose(f);
            break;
        }
        case TCP_LINK: {                 // input trace data from the TCP port
            if (!tcpOpen(tcpPort)) {
                return -1;
            }
            else {
                printf("\nTCP/IP port %d opened, "
                       "hit any key to quit...\n"
                       "(the target must be stopped)\n",
                       tcpPort);
            }
            while ((n = tcpRead(buf, sizeof(buf))) != -1) {
                if (n > 0) {
                    qsParse(buf, n);
                    if (savFile != (FILE *)0) {
                        fwrite(buf, 1, n, savFile);
                    }
                }
            }
            tcpClose();
            break;
        }
    }
    if (savFile != (FILE *)0) {
        fclose(savFile);
    }
    if (matFile != (FILE *)0) {
        fclose(matFile);
    }

    printf("\nDone.\n");
    return 0;                                                       // success
}