GophHub - go4retro/tcpser/src/ip232test.c


Raw File

    1	/*
    2	 ============================================================================
    3	 Name        : IP232Test.c
    4	 Author      : Jim Brain
    5	 Version     :
    6	 Copyright   : Your copyright notice
    7	 Description : Hello World in C, Ansi-style
    8	 ============================================================================
    9	 */
   10	
   11	#include <sys/socket.h>   // for recv...
   12	#include <stdio.h>
   13	#include <stdlib.h>
   14	#include <sys/types.h>
   15	#include <sys/socket.h>
   16	#include <netinet/in.h>
   17	#include <arpa/inet.h>
   18	#include <netdb.h>
   19	#include <unistd.h>       // for read...
   20	
   21	#include "dce.h"
   22	#include "debug.h"
   23	#include "util.h"
   24	#include "ip.h"
   25	
   26	void *ip_thread(void *arg) {
   27	
   28	  fd_set readfs;
   29	  int max_fd = 0;
   30	  int res = 0;
   31	  unsigned char buf[256];
   32	  dce_config *data = (dce_config *)arg;
   33	  int rc;
   34	
   35	
   36	  LOG_ENTER();
   37	  while(TRUE) {
   38	    FD_ZERO(&readfs);
   39	    FD_SET(data->ifd, &readfs);
   40	    max_fd=MAX(max_fd, data->ifd);
   41	    max_fd++;
   42	    rc = select(max_fd, &readfs, NULL, NULL, NULL);
   43	    if(rc == -1) {
   44	      ELOG(LOG_FATAL, "Select returned error");
   45	      exit(-1);
   46	    } else {
   47	      // we got data
   48	      if (FD_ISSET(data->ifd, &readfs)) {  // socket
   49	        LOG(LOG_DEBUG, "Data available on socket");
   50	        res = dce_read(data, buf, sizeof(buf));
   51	        LOG(LOG_DEBUG, "Read %d bytes from socket", res);
   52	        buf[res] = 0;
   53	        //if(!res) {
   54	        //  ELOG(LOG_FATAL, "Lost connection");
   55	        //  exit(-1);
   56	        //}
   57	        //parse_ip_data(cfg, buf, res);
   58	      }
   59	    }
   60	  }
   61	  LOG_EXIT();
   62	}
   63	
   64	void send_cmd(int sd, unsigned char cmd) {
   65	  unsigned char buf[2];
   66	
   67	  buf[0] = 255;
   68	  buf[1] = cmd;
   69	  ip_write(sd, buf, 2);
   70	
   71	}
   72	
   73	int main(int argc, char *argv[]) {
   74	  dce_config data;
   75	
   76	  char address[] = "localhost:25232";
   77	  //unsigned char text[] = "atdtdilbert\x0d";
   78	
   79	  log_init();
   80	  dce_init_config(&data);
   81	
   82	  printf("IP232 Test Program\n");
   83	
   84	  log_set_level(LOG_ALL);
   85	  log_set_trace_flags(255);
   86	
   87	  data.ofd = ip_connect(address);
   88	  if(data.ofd > -1) {
   89	    data.ifd = data.ofd;
   90	    data.is_connected = TRUE;
   91	    spawn_thread(ip_thread, (void *)&data, "IP");
   92	    //dce_write(&data, text, strlen((char *)text));
   93	    // add in more text here.
   94	    while(1);
   95	  }
   96	
   97	
   98	  return EXIT_SUCCESS;
   99	}
  100	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.