ddebevec 0 Newbie Poster

I have a client/server socket program example that I am modifying in order to pass a parameter to a called program on the server side. The problem that I’m encountering is trying to build a command line string that will execute the called program from a “system()” function. I’m testing with the dos echo command to make sure I’m able to build the string. When I run program and print the contents of the variable “recvMsgSize”, I get the correct valve. When I print the contents of “echobuffer”, I get the value passed and 3 or 4 additional characters (see Output1). I tried shortening the buffers to be equal to the size of the data passed, the data looked almost correct (see Output 2), but Windows abended the server program. I’ve included the program code that I’m trying to work with. It does contain “print()” functions that I using for diagnostics. I tried building the string via “sprintf()” function and by moving the data, byte by byte to a buffer. Can someone steer me in the right direction for this issue?

Thanks,

David


Output 1
recvMsgSize 4
echobuffer dave╨²‼
echobuffer dave╨²‼
PDLSmsg dave╨²‼
daveDy‼
echobuffer e╨²‼

Output 2

recvMsgSize 4
echobuffer dave▼
echobuffer dave▼
PDLSmsg dave▼
dave▼
echobuffer dave▼


Program Code

/* CHANGES FROM UNIX VERSION                                                   */
/*                                                                             */
/* 1.  Changed header files.                                                   */
/* 2.  Used closesocket() instead of close().                                  */

#include <winsock.h>    /* for socket(),... */
#include < stdio.h>

#define RCVBUFSIZE 32    /* Size of receive buffer */

void DieWithError(char *errorMessage);  /* Error handling function */

void HandleTCPClient(int clntSocket)
{
    char echoBuffer[RCVBUFSIZE];        /* Buffer for echo string */
    int recvMsgSize;                    /* Size of received message */
    char* PDLSmsg;
    char PDLSexec[8];
    int i = 1;

    /* Receive message from client */
    if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
        DieWithError("recv() failed");
	printf("recvMsgSize %d\n",recvMsgSize);
	printf("echobuffer %s\n",echoBuffer);
	char *x = (char *) malloc(recvMsgSize*sizeof(char));
	memset(x, 0, recvMsgSize*sizeof(char));

    /* Send received string and receive again until end of transmission */
    while (recvMsgSize > 0)      /* zero indicates end of transmission */
    {
        /* Echo message back to client */
        if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
            DieWithError("send() failed");

		/* See if there is more data to receive */
        if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE,0)) < 0)
            DieWithError("recv() failed");

		x[i] = echoBuffer[i];
		++i;
		//printf("x[%d] = %d\n", i, x[i]);

    }
	//	memset(PDLSexec, 0, 32*sizeof(char)); /* Zero out structure */
		printf("echobuffer %s\n",echoBuffer);
		printf("x %s\n",x);
		PDLSmsg = echoBuffer;
		printf("PDLSmsg %s\n",PDLSmsg);
		sprintf(PDLSexec,"echo %s",echoBuffer);
		system(PDLSexec);
		printf("echobuffer %s\n",echoBuffer);

			/*
	system("pdls.exe -c dest -A -r enabled WSDP110")

	returned -> WSDP110: enabled = true
	*/
    closesocket(clntSocket);    /* Close client socket */
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.