Hello,
I wrote the following program to send commands to a camera over TCP. However so far I have only been able to receive a welcome message from this camera.
The camera expects a command in the following format:
4 bytes of a Hex (0xBA12A20C)
4 bytes integer (value=16),
4 bytes integer (body integer)
4 bytes integer (footer integer)
So i made a 16 byte array and initialized it with 4 bytes array for each part. Converted integers to byte array.
With no success on camera actually performing any of the commands, I am now wondering if my approach is incorrect or I am doing something wrong. Any comments/suggestions will be really appreciated.
thanks!
Nancy.
-----------client---------------------
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <cstring>
#include <stdarg.h>
#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;
#define RCVBUFSIZE 32 /* Size of receive buffer */
typedef unsigned char byte;
byte header[]={0xBA,0x12,0xA2,0x0C}; /*first part of the command*/
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort = 41000; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
byte echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
int sendCommand(int body, int footer)
{
byte byteArray[16];
// copy header into byteArray's first 4 bytes
memcpy(byteArray,header,4);
int length=16;
//convert length,body & footer into byteArrays of 4 bytes each
//and initialize byteArray
if ((argc < 1) || (argc > 2)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> \n",
argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
fprintf(stderr,"socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
fprintf(stderr, "Connection failed");
/* Receive message from server*/
totalBytesRcvd = 0;
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
fprintf(stderr,"\n\nRecv failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes recv*/