C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2009, 12:00 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 12
Client-server system with input from separate program

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!
robot-ic is offline   Reply With Quote
Old 01-16-2009, 06:48 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Why don't you just use an identical program on both the local and remote machine (lets call that "server to server") and then just use a socket, rather than a pipe, since you aren't having any problems with sockets and they are much better for this anyway? Then the local server will have two connections -- one to the remote machine, and one to the local program that gathers the information.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 01-16-2009, 08:38 AM   #3
Registered User
 
Join Date: Jan 2009
Posts: 12
First, thanks for replying.

Do you mean using the program that gets the original input on two computers? If so, that unfortunately isn't an option based on what we're trying to accomplish.

I was trying to implement my part of the system with minimal editing of the guy I'm working with's program, but now I'm thinking that maybe I should try making my client program separate thread in the original program. Do you think that would work?
robot-ic is offline   Reply With Quote
Old 01-16-2009, 03:30 PM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Post

Well, the only thing I've done with sockets that involves both ends of a client/server operation, beside some exercises and experiments, is a forked local unix socket server that's part of a GUI text viewer app. Altho it's all one program, it's not threaded, it's forked, which means the child process operates as a server and maintains a local socket connection to it's parent process, because it must function in it's own loop to accept connections independent of the GUI (which a GUI also runs using a loop, and threads). The GUI loop is no good for "server" type operations -- listening on a socket for connection requests -- but it's fine for monitoring an established connection. When the application is first invoked, it checks a to see if a unique socket (a socket only ever used by that application) exists, and if so, checks to see if there is a running server accepting connections on the socket. If so, it passes it's command line arguments on and exits without ever starting the GUI. Your web browser probably works this way, so if it's running and you type:

firefox www.cprogramming.com

or click an url in some other program somewhere, rather than start a second instance of the application, the information gets passed along to the first instance by a forked server, which is a child of that instance. If you check your running processes, you will see two of them listed although only one is performing functions directly involving the end-user. They are actually using different amounts of the memory and processor, since the child first shuts down it's copy of the GUI (and other now irrelevent things) before starting the server, while the parent only has to run in client mode.

My point is that when you write "Do you mean using the program that gets the original input on two computers? If so, that unfortunately isn't an option based on what we're trying to accomplish." I can't really assume this is true since I don't know completely what you are trying to accomplish, but based on what you have said so far in this thread I would say the best idea is to write one multi-threaded or forking program that can perform all the necessary performances relative to the context it's operating "in". Since the server process will be listening on an actual internet socket, the parent process which does the local information processing will have to contact it's child (the server) at the address where it is also waiting for a connection request from another instance of the server, running on another machine and connected to a parent that is presenting the information.

That's one program, not two or four or three.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 01-16-2009 at 03:35 PM.
MK27 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chat Program Help? Paul22000 Networking/Device Communication 9 02-10-2009 12:35 AM
Socket Programming Problem!!!! bobthebullet990 Networking/Device Communication 2 02-21-2008 07:36 PM
my server program auto shut down hanhao Networking/Device Communication 1 03-13-2004 10:49 PM
Server Client Messaging Program X PaYnE X Networking/Device Communication 3 01-04-2004 05:20 PM
socket question Unregistered C Programming 3 07-19-2002 01:54 PM


All times are GMT -6. The time now is 08:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22