/************************************************************ (C) Copyright 1997 Tecnomare Spa. Venice, ITALY All rights reserved. File: EXAMPLE_TCP.C $Date: 99/03/05 $ $Revision: 0.0 $ ************************************************************/ #include "f_tcp.h" #include #define TRUE 1 #define FALSE 0 #define BUFFER_SIZE 20 #define PORT 10000 /* tcp/ip connection handling variables */ int port_number, childFileDescriptor, parentFileDescriptor; pid_t childproc_num; /*---------------------------------------------------------*/ void CloseAndExit(void) { int ww = 1; if (childproc_num == 0) { /* child process */ printf("child: task exit\n"); exit(0); } else { /* parent process */ kill(childproc_num, SIGTERM); sleep(ww); /* close port server TCPIP */ if (parentFileDescriptor) unconfig_tcp_port(parentFileDescriptor); printf("main:\t\t task exit\n"); exit(0); } /*if()*/ } /*CloseAndExit()*/ /*------------------------------------------------------------MAIN()---*/ void main (int argc, char *argv[]) { register int i,count; char console_buffer[BUFFER_SIZE]; char ip_input_buffer[BUFFER_SIZE]; char ip_output_buffer[BUFFER_SIZE]; printf("%s compiled in %s %s\n", __FILE__, __DATE__, __TIME__); if (config_tcp_port_server(PORT, &parentFileDescriptor)==-1) { SysError("TCP port server: bad configuration\n"); } childproc_num = 0; ser_to_cl_connection(parentFileDescriptor, &childFileDescriptor, &childproc_num); while (TRUE) { count = recv( childFileDescriptor, ip_input_buffer, BUFFER_SIZE, 0 ); if (count ==0) break; printf("Received:%s\n",ip_input_buffer); memcpy(ip_output_buffer,ip_input_buffer,sizeof(ip_input_buffer)); strcat(ip_output_buffer,".echo"); if ( send( childFileDescriptor, ip_output_buffer, strlen(ip_output_buffer) , 0) <= 0 ) SysError( "Error on send" ); } CloseAndExit(); } /*main()*/ void SysError( char *string ) { printf( "Error found: String given is --> %s\n", string ); exit(0); }