2 Replies - 364 Views - Last Post: 29 April 2013 - 05:53 AM Rate Topic: -----

#1 jprin72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 19-March 12

Reading messages from a Socket connection

Posted 28 April 2013 - 10:35 AM

I am having some problems with getting the server to successfully read the message sent from the client via sockets. The client (child process is supposed to simulate the life support system of a space ship. It should adjust the Air Levels then Environmental variables (simulated by sleeping) and pass this information to the parent via a socket connection. Right now I believe that the processes are running correctly (the time stamps from the parent are correct), but the parent is not reading in the message from the child. Here is the code I have so far:

#include <unistd.h>
#include <stdio.h>
#include <wait.h> 
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> 
#include <arpa/inet.h>
#include "randapi.h"

#define MaxLen 80
#define MY_SERVER_PORT 6378

#define pIn 0	//Pipe for Message (String) 
#define pOut 1	//Pipe for Time (int)

int main()
{
	int ret, PipMsg[2];
	int sec;
	char tBuf[3];
	char msg[MaxLen];
	struct sockaddr_in servaddrc;
	struct sockaddr_in servaddr;
	int serverFD, connectionFD;

	//seedRandGenerator();

	time_t currentTime;
	time_t entryTime; 

	printf("Hello\n");

	//ret = pipe(PipMsg);
	//printf("%i  ", ret);	

	while(1)
	{	
		seedRandGenerator();
		entryTime = time(NULL); //start loop time
		if(fork() == 0)	//created child successfully
		{
			//START CHILD CODE LIFESUPPORT

			//int connectionFD, in; 
			//struct sockaddr_in servaddrc;
			
			connectionFD = socket(AF_INET, SOCK_STREAM, 0);

			memset(&servaddrc, 0, sizeof(servaddrc));
			servaddrc.sin_family = AF_INET; 
			servaddrc.sin_port = htons(MY_SERVER_PORT); 
			servaddrc.sin_addr.s_addr = inet_addr("127.0.0.1"); 

			//sleep?
			sleep(0);
			connect(connectionFD, (struct sockaddr*)&servaddrc, sizeof(servaddrc));

			//printf("client/child set up\n");
 	
			char air[MaxLen] = "Adjusted Breathing Gas Levels \n Process Time: "; 
			char env[MaxLen] = "Adjusted Environmentals \n Process Time: "; 
					
			sec = (int)(getRandExponential()*5.0);
				
			snprintf(tBuf, 3, "%d", sec); 
			strcat(air, tBuf); //add Process time to string 
			strcat(air, " secs");

			write(connectionFD, air, strlen(air) +1);

			sleep(0);
	
			sec = (int)(getRandExponential()*3.0)+1; //+1 to prevent no ptime 
				
			sleep(sec); //simulate process time for Envrionmentals 

			snprintf(tBuf, 3, "%d", sec); 
			strcat(env, tBuf);
			strcat(env, " secs");

			write(connectionFD, env, strlen(env) +1); 
		//printf("end ch");
			 	
			close(connectionFD);
			return 0;	//terminate child
	
		}else{
			//START PARENT CODE
			//int serverFD, connectionFD, in; 
			//struct sockaddr_in servaddr; 
			
			serverFD = socket(AF_INET, SOCK_STREAM, 0);

			memset(&servaddr, 0, sizeof(servaddr)); 
			servaddr.sin_family = AF_INET;
			servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
			servaddr.sin_port = htons(MY_SERVER_PORT);

			bind(serverFD, (struct sockaddr*)&servaddr, sizeof(servaddr)); 

			listen(serverFD, 3);

			//printf("parent set up\n");
			
				
			//sleep(5);  //allow child to run first
			//printf("back in parent");

			read(connectionFD, msg, MaxLen);
	
			currentTime = time(NULL); 
			printf("%s\n %s\n", msg, ctime(&currentTime)); //print to CRT
		
			wait(NULL);	//wait for environmental changes(Child terminates)
		
			read(connectionFD, msg, MaxLen); 
			
			currentTime = time(NULL); 
			printf("%s\n %s\n", msg, ctime(&currentTime));
		
			close(connectionFD);  
			
		
		}//end parent code
/*			
		if(fork() == 0)  //procreate child for Navigation
		{
			//START CHILD SPECIFIC CODE FOR NAV
			char nav[MaxLen] = "Adjusted Navigation \n Process Time: ";
			
			sec = (int)(getRandExponential()*6.0);
				
			sleep(sec); //simulate process time for Nav 
				

			snprintf(tBuf, 3, "%d", sec); 
			strcat(nav, tBuf); //add Process time to string 
			strcat(nav, " secs");
	
			write(PipMsg[pOut], nav, strlen(nav) +1);

			return 0; //terminate child process
		
		}else{
			//START PARENT SPECIFIC CODE
			wait(0);  //allow child to run first 

			read(PipMsg[pIn], msg, 80); //read nav in from pipe
	
			currentTime = time(NULL); 
			printf("%s\n %s\n", msg, ctime(&currentTime)); //print to CRT

		}//end parent code
*/
		currentTime = time(NULL); 
		sleep(30 - (currentTime - entryTime));

	}//end while
}//end program



Sample output:
XA[���
Sun Apr 28 12:16:38 2013

XA[���
Sun Apr 28 12:16:52 2013

XA[���
Sun Apr 28 12:17:08 2013

XA[���
Sun Apr 28 12:17:15 2013

XA[���
Sun Apr 28 12:17:38 2013

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading messages from a Socket connection

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: Reading messages from a Socket connection

Posted 28 April 2013 - 08:54 PM

Wouldn't it be better to open the pipes and corresponding sockets and loop the reading and writing, instead of continuously creating sockets?
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Reading messages from a Socket connection

Posted 29 April 2013 - 05:53 AM

On line 110, you are calling read(), but not checking whether it successfully read data or not. Since you aren't pre-initializing the destination buffer of the read(), you are left with whatever random characters were in the buffer before you called read().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1