Hi, this is my first time posting here.

I'm working together with a couple of other people on a project where one program takes input from an external device, and from there the values that program has received are to be transferred over the local network to another computer.

I'm tasked with setting up the communication between the two computers. I set up a tcp-ip client server system to connect the two computers and send the data, and that wasn't much of a problem. What I've been working on since has been getting the data from the program which receives the data into my client program. I decided to try spawning my client program from the original program, then setting up a pipe that would write the values to my client program. I spawn tested the spawn and pipe functions with a simple program that
would take a string, spawn a second program, pipe the string to the second program, and then print the string to the screen from the second program.

My next test was to modify my spawn and pipe example by then having the second program print the test string, then connect to another computer and send the string over the network. Unfortunately, now I have a problem:
Apparently the spawned program is trying to read from the pipe before the parent program writes to it, because it is printing out junk data.

Is there some way I can fix this, maybe to make the program continually read from the pipe at a certain interval, and to print/send data only when a non junk value is read?

Here is my code for the parent program:
Code:
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <io.h>

#define inpipe 11
#define outpipe 12

int main()
{
    int pipes[2];
    char outbuf[512], inbuf[512];
   
    /* Open a set of pipes */
      if( _pipe( pipes, 256, O_BINARY ) == -1 )
          exit( 1 );
          
    _dup2(pipes[0], inpipe);
    _dup2(pipes[1], outpipe);
    strcpy(outbuf, "This is a test of pipes.\n");
    
	
    int pid;
    char *file = "client3.exe";
    printf("About to open child program \n");
    pid = _spawnl( P_NOWAIT,"client3.exe",file);
    
    if (_write(outpipe, outbuf, strlen(outbuf) + 1) == -1) {
            perror("Parent - Write failed");
            exit(EXIT_FAILURE);
        }
//getchar();
return 0;

}
and for my child program:
Code:
#include <stdio.h>
//#include <unistd.h>
//#include <perror.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <io.h>
#include <winsock2.h>


#define inpipe 11
#define outpipe 12
 

int client()

{

  // Initialize Winsock

  WSADATA wsaData;

  int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

  if (iResult != NO_ERROR)

    printf("Client: Error at WSAStartup().\n");

  else

    printf("Client: WSAStartup() is OK.\n");

 

  // Create a SOCKET for connecting to server

  SOCKET ConnectSocket;

  ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ConnectSocket == INVALID_SOCKET)

  {

    printf("Client: Error at socket(): %ld\n", WSAGetLastError());

    WSACleanup();

    return 0;

  }

  else

         printf("Client: socket() is OK.\n");

 

  // The sockaddr_in structure specifies the address family,

  // IP address, and port of the server to be connected to.

  sockaddr_in clientService;
   //hostent* remoteHost;

char* ip;

char* host_name;

unsigned int addr;
char buffer[512];
int len;
 
 printf("Child - Reading from parent\n");
 if ((len = read(inpipe, buffer, 512)) <= 0) {
        perror("Child - Read failed");
        exit(EXIT_FAILURE);
    }
    else {
        printf("Child: %s\n", buffer);
       
    

// User inputs name of host

printf("Input IP of the host: ");

ip = (char*) malloc(sizeof(char*)*128);

fgets(ip, 128, stdin);
//addr = inet_addr(host_name);
 




  
	clientService.sin_family = AF_INET;
	clientService.sin_port = htons(55555);
	clientService.sin_addr.s_addr = inet_addr(ip);


  
  

 

  // Connect to server...

  if (connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)

  {

    printf("Client: Failed to connect.\n");

    WSACleanup();

    return 0;

  }

  else

       printf("Client: connect() is OK.\n");

 

  printf("Client: Connected to server...\n");
   // Send and receive data.

    int bytesSent;

    int bytesRecv = SOCKET_ERROR;

    

    char sendbuf[200]; 
	//= "Client: Sending some test string to server...";

    char recvbuf[200] = "";

	

 

    bytesSent = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);

    printf("Client: send() - Bytes Sent: %ld\n", bytesSent);

 

    while(bytesRecv == SOCKET_ERROR)

    {

        bytesRecv = recv(ConnectSocket, recvbuf, 32, 0);

        if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)

        {

            printf("Client: Connection Closed.\n");

            break;

        }

        else

            printf("Client: recv() is OK.\n");

 

        if (bytesRecv < 0)

            return 0;

        else

            printf("Client: Bytes received - %ld.\n", bytesRecv);

    }


  WSACleanup();

  return 0;

}
Thanks for reading!