Changes between Version 7 and Version 8 of Docs/Prog/Manual/DeviceSupport/Network


Ignore:
Timestamp:
02/19/10 10:32:04 (14 years ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/Prog/Manual/DeviceSupport/Network

    v7 v8  
    343343{{{
    344344// TCP/IP Client Example
     345
     346
     347#include <stdio.h>
     348#include <stdlib.h>
     349#include <unistd.h>
     350#include <fcntl.h>
     351#include <string.h>
     352
     353
     354#include <sys/types.h>
     355#include <sys/socket.h>
     356#include <netinet/in.h>
     357#include <netdb.h>
     358
     359#define CHR_STX         0x02
     360#define CHR_ETX         0x03
     361
     362#define IP_ADDR         "90.1.2.202"
     363#define IP_PORT         3000
     364
     365#define START_CHR       CHR_STX
     366#define STOP_CHR        CHR_ETX
     367
     368enum RcvMode { rcvIdle, rcvData };
     369
     370#define RCVMODE_IDLE    0
     371#define RCVMODE_DATA    1
     372
     373/*
     374 * This example connects to the server
     375 * When a properly formatted message <STX>message<ETX> is received from the server
     376 * it is sent back to the server
     377 *
     378 */
     379
     380int main()
     381{
     382        struct sockaddr_in serv_addr;
     383        struct hostent *svr;
     384        char buffer[50], rcv_buf[50];
     385        int sockfd, n, i, rcv_pos = 0;
     386        RcvMode rcvMode = rcvIdle;
     387
     388        sockfd = socket(AF_INET, SOCK_STREAM, 0);
     389        if(sockfd < 0)
     390        {
     391                printf("error\r\n");
     392                return -1;
     393        }
     394
     395        svr = gethostbyname(IP_ADDR);
     396        if(svr == NULL)
     397        {
     398                printf("error getHostbyname\r\n");
     399                return -1;
     400        }
     401
     402        memset(&serv_addr, 0, sizeof(serv_addr));
     403        serv_addr.sin_family = AF_INET;
     404        memcpy(&serv_addr.sin_addr.s_addr, svr->h_addr_list[0], sizeof(serv_addr.sin_addr.s_addr));
     405
     406        serv_addr.sin_port = htons(IP_PORT);
     407        if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
     408        {
     409                printf("error connecting\r\n");
     410                return -1;
     411        }
     412
     413        printf("Connected to server %s port %d waiting for message from server\r\n", IP_ADDR, IP_PORT);
     414        while(1)
     415        {
     416                n = read(sockfd, buffer, sizeof(buffer));
     417                // We are not blocking. You must copy to another buffer to be sure to get a complete message
     418                // For example in one iteration of the loop you might receive:
     419                //                              <STX>This is a
     420                // and the nest iteration of the loop may give you:
     421                //                              test message<ETX>
     422                if(n > 0)
     423                {
     424                        for(i = 0; i < n; i++)
     425                        {
     426                                if(rcvMode == rcvIdle)
     427                                {
     428                                        if(buffer[i] == START_CHR)
     429                                        {
     430                                                rcvMode = rcvData;
     431                                        }
     432                                }
     433                                else
     434                                {
     435                                        if(buffer[i] == STOP_CHR)
     436                                        {
     437                                                rcv_buf[rcv_pos] = '\0';
     438                                                printf("Message received [%s]\r\n", rcv_buf);
     439
     440                                                // Send back the message without the <STX> and <ETX>
     441                                                send(sockfd, rcv_buf, strlen(rcv_buf), 0);
     442
     443                                                rcv_pos = 0;
     444                                                rcvMode = rcvIdle;
     445                                        }
     446                                        else if(rcv_pos < (int)(sizeof(rcv_buf) - 1))
     447                                        {
     448                                                rcv_buf[rcv_pos++] = buffer[i];
     449                                        }
     450                                }
     451                        }
     452                }
     453        }
     454        return 0;
     455}
     456
     457
    345458}}}
    346459