/* $id$ $Source: /home/giacomo/cvsroot/hadtempsens/sockettcp/socket.c,v $ $Header: /home/giacomo/cvsroot/hadtempsens/sockettcp/socket.c,v 1.4 2006/09/07 15:01:11 giacomo Exp $ */ /* Giacomo Ortona This program will open a port on the server and will print the data received from the client on the server's screen */ #include #include #include #include #include #include #include #include #define BUFFSIZE 2048 #define MAXPENDING 5 /*The MAXPENDING constant limits the number of connections that will be queued at a time (only one will be serviced at a time */ int g_client_socket = -1; /* /\*this function is an header for a grace plot*\/ */ /* void print_xmgr_header(FILE *out, char *title, char *subtitle) { */ /* fprintf(out, "\ */ /* # You can pipe this file thru xmgr -nxy -source stdin,\n# or, alternatively, remove the header (everything down to the double\n# character ##), and the result is a data file for Origin.\n# Title/subtitle\n@title \"%s\"\n@title font 1\n@subtitle \"%s\"\n@subtitle font 0\n# Axes\n@xaxis ticklabel font 0\n@xaxis label \"temperature\"\n@xaxis label font 2\n@yaxis ticklabel font 0\n@yaxis label \"ID device\"\n@yaxis label font 2\n# Legend\n@legend loctype view\n@legend x1 0.868347\n@legend y1 0.552632\n#@legend 0.65, 0.8\n@legend box on\n@legend box fill on\n@legend on\n@legend font 2\n@legend string 0 \"temp1\"\n@legend string 1 \"temp2\"\n@legend string 2 \"temp3\"\n@legend string 3 \"temp4\"\n@legend string 4 \"temp5\"\n@legend string 5 \"temp6\"\n# Colors\n@title color 2\n@subtitle color 4\n@xaxis label color 4\n@yaxis label color 4\n#@title color 1\n#@subtitle color 1\n#@xaxis label color 1\n#@yaxis label color 1\n# Legend colors\n@legend box color 4\n#@legend box color 1\n@legend box fill color 7\n# Line Colors/Styles\n@s0 color 1\n@s0 linestyle 1\n@s1 color 2\n@s1 linestyle 1\n@s2 color 3\n@s2 linestyle 1\n@s3 color 4\n@s3 linestyle 1\n@s3 color 5\n@s3 linestyle 1\n@s3 color 6\n@s3 linestyle 1\n# Line widths\n#@graphs sets linewidth 2\n@graphs sets linewidth 1\n## time temp1 temp2 temp3 temp4 temp5 temp6", title, subtitle); */ /* } */ /*this function is an error function*/ void Die(char *mess) { perror(mess); exit(2); } /* HandleClient function will receive any initial bytes available, then cycle and receiving more data. For short echo strings (particularly if less than BUFFSIZE ) and typical connections, only one pass through the while loop will occur. But the underlying sockets interface (and TCP/IP) does not make any guarantees about how the bytestream will be split between calls to recv() */ void HandleClient(int sock) { char buffer[BUFFSIZE]; int received = -1; /* Receive message */ if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { Die("Failed to receive initial bytes from client"); } /* /\* Send bytes and check for more incoming data in loop *\/ */ /* while (received > 0) { */ /* /\* Send back received data *\/ */ /* if (send(sock, buffer, received, 0) != received) { */ /* Die("Failed to send bytes to client"); */ /* } */ /* /\* Check for more data *\/ */ /* if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { */ /* Die("Failed to receive additional bytes from client"); */ /* } */ // } buffer[received]='\0'; printf("%s",buffer); // fprintf(out,"%s",buffer); // print_xmgr_header(out,"temperatures","7 devices"); } /*this function will start the procedure for terminating the program execution*/ void exitproc(){ char terminatecommand = 'd'; if(g_client_socket != -1) { if (send(g_client_socket, &terminatecommand, 1, 0) != 1) { Die("error sending terminate command character to client \n"); } } exit(1); } int main(int argc, char *argv[]) { signal(SIGINT, exitproc); FILE *out; int serversock, clientsock; struct sockaddr_in server, client; if (argc != 2) { fprintf(stderr, "USAGE: server \n"); exit(5); } /* Create the TCP socket */ serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (serversock < 0) { Die("Failed to create socket"); } /* both IP address and port are converted to network byte order for the sockaddr_in structure. The reverse functions to return to native byte order are ntohs() and ntohl() . These functions are no-ops on some platforms, but it is still wise to use them for cross-platform compatibility. */ /* Construct the server sockaddr_in structure */ memset(&server, 0, sizeof(server)); /* Clear struct */ server.sin_family = AF_INET; /* Internet/IP */ server.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */ server.sin_port = htons(atoi(argv[1])); /* server port */ /* Bind the server socket */ if (bind(serversock, (struct sockaddr *) &server,sizeof(server)) < 0) { Die("Failed to bind the server socket"); } /* Listen on the server socket */ if (listen(serversock, MAXPENDING) < 0) { Die("Failed to listen on server socket"); } unsigned int clientlen = sizeof(client); /* Wait for client connection */ clientsock = accept(serversock, (struct sockaddr *) &client, &clientlen); if (clientsock < 0) { Die("Failed to accept client connection"); } // store globally for exit via ctrl-c g_client_socket = clientsock; fprintf(stdout, "Client connected: %s\n",inet_ntoa(client.sin_addr)); out = fopen ("readtemp.txt","w"); /* check if output was opened! */ if (out ==0) { printf("error opening file"); exit(2); } /* commands: a peform normal temperature detection b set temperature limits for alarms c scan for alarms d close connection */ char command = 'a'; /* Run until cancelled */ while (1) { if (send(clientsock, &command, 1, 0) != 1) { Die("error sending command character"); } if(command==('b'||'B')){ uint8_t thigh,tlow; uint8_t ID[8]; printf("insert high limit: "); scanf("%x",&thigh); if (send(clientsock, &thigh, 8, 0) != 1) { Die("error sending high limit"); } printf("\ninsert low limit: "); scanf("%x",&tlow); if (send(clientsock, &tlow, 8, 0) != 1) { Die("error sending low limit"); } int q=0; for(q=0;q<8;q++){ printf("\ninsert the %d 2 numbers ID (hex without blanks): ",q+1); scanf("%x",&ID[q]); if (send(clientsock, &ID[q], 8, 0) != 1) { Die("error sending high limit"); } } } else{ HandleClient(clientsock); } } char terminatecommand = 'd'; if (send(g_client_socket, &terminatecommand, 1, 0) != 1) { Die("error sending terminate command character to client \n"); } close(clientsock); return 0; }//end of main