Main Page | Data Structures | File List | Data Fields | Globals

socket_comm.c

Go to the documentation of this file.
00001 /* socket_comm.c --- 
00002  * 
00003  * Filename: socket_comm.c
00004  * Description: 
00005  * Author: Bryce Himebaugh
00006  * Maintainer: 
00007  * Created: Tue Oct  3 10:53:28 2006
00008  * Version: 
00009  * Last-Updated: 
00010  *           By: 
00011  *     Update #: 0
00012  * Keywords: 
00013  * Compatibility: 
00014  * 
00015  */
00016 
00017 /* Commentary: 
00018  * 
00019  * 
00020  * 
00021  */
00022 
00023 /* Change log:
00024  * 
00025  * 
00026  */
00027 
00028 /* This program is free software; you can redistribute it and/or modify
00029  * it under the terms of the GNU General Public License as published by
00030  * the Free Software Foundation; either version 2, or (at your option)
00031  * any later version.
00032  * 
00033  * This program is distributed in the hope that it will be useful,
00034  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036  * GNU General Public License for more details.
00037  * 
00038  * You should have received a copy of the GNU General Public License
00039  * along with this program; see the file COPYING.  If not, write to the
00040  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00041  * Boston, MA 02111-1307, USA.
00042  */
00043 
00044 /* Code: */
00045 
00046 #include <stdio.h>
00047 #include <sys/types.h> 
00048 #include <sys/socket.h>
00049 #include <netinet/in.h>
00050 #include <strings.h>
00051 #include <string.h>
00052 #include <arpa/inet.h>
00053 #include <stdlib.h>
00054 #include <unistd.h>
00055 #include <signal.h>
00056 #include <errno.h>
00057 #include <pthread.h>
00058 #include <fcntl.h>
00059 #include "command_line_parser.h"
00060 #include "frame_comm.h"
00061 
00062 int socket_comm(command_options_t *options) {
00063   static struct sockaddr_in serv_addr, cli_addr;
00064   enum {INIT,UNCONNECTED,QUEUE_FRAME};
00065   static int bridge_state=INIT; 
00066   int oldopts;
00067   static int sockfd=0;
00068   static int clilen=0;
00069   static int newsockfd=0;
00070   char buffer[256];
00071   int n;
00072   char write_buffer[500]; 
00073   int write_chars;
00074 
00075   switch (bridge_state) {
00076   case INIT:
00077     sockfd = socket(AF_INET, SOCK_STREAM, 0);        // Load file descriptor, sockfd
00078     if (sockfd < 0) {
00079       printf("ERROR opening socket\n");
00080         exit(0);
00081     }
00082     oldopts = fcntl(sockfd, F_GETFL, 0);
00083     fcntl(sockfd, F_SETFL, oldopts | O_NONBLOCK);
00084     bzero((char *) &serv_addr, sizeof(serv_addr));   // zero the 
00085     serv_addr.sin_family = AF_INET;
00086     serv_addr.sin_addr.s_addr = INADDR_ANY;
00087     serv_addr.sin_port = htons(options->port);
00088     if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
00089       printf("ERROR on binding\n");
00090       exit(0);
00091     }
00092     listen(sockfd,5);
00093     bridge_state=UNCONNECTED;
00094     break;
00095   case UNCONNECTED:
00096     errno=0;
00097     if ((newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen))==-1) {
00098       if (errno==EWOULDBLOCK) {
00099       }
00100     }
00101     else {
00102       bridge_state=QUEUE_FRAME;
00103       oldopts = fcntl(newsockfd, F_GETFL, 0);
00104       fcntl(newsockfd, F_SETFL, oldopts | O_NONBLOCK);
00105     }
00106     break;
00107   case QUEUE_FRAME:
00108     bzero(buffer,256);
00109     n = read(newsockfd,buffer,255);
00110     if (n<0) {
00111       // no data available
00112       writeMessage(&msg,write_buffer);
00113       write_chars=write(newsockfd,write_buffer,strlen(write_buffer));
00114     }
00115     else if (n==0) {
00116       // socket closed at the other end
00117       close(newsockfd);
00118       bridge_state=UNCONNECTED;
00119     }
00120     else {
00121       // data of length n was read into the buffer
00122       // take this data and load it into the struct 
00123     }
00124     break;
00125   default:
00126     printf("Error: Unknown Communication State\n");
00127     exit(1);
00128     break;
00129   }
00130   return 0;    
00131 }
00132 
00133 
00134 
00135 
00136 /* socket_comm.c ends here */

Generated on Fri Nov 3 15:24:51 2006 for ERT1 Communication Server by  doxygen 1.3.9.1