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

socket_comm.c File Reference

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include <fcntl.h>
#include "command_line_parser.h"
#include "frame_comm.h"

Include dependency graph for socket_comm.c:

Include dependency graph

Go to the source code of this file.

Functions

int socket_comm (command_options_t *options)


Function Documentation

int socket_comm command_options_t options  ) 
 

Definition at line 62 of file socket_comm.c.

References clilen, command_options_t, msg, newsockfd, options, command_options::port, sockfd, and writeMessage().

Referenced by scheduler().

00062                                             {
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 }

Here is the call graph for this function:


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