int checkSerialActivity() { if (client_count<MAX_CLIENTS) { int client_sock = accept_incoming(listen_sock); if (client_sock!=-1) { clients[client_count]=client_sock; client_count++; printf("New connection. %d total.\n",client_count); } } int i; unsigned char buffer[1024]; struct pollfd fds[1+MAX_CLIENTS]; fds[0].fd=serialfd; fds[0].events=POLLIN; fds[0].revents=0; for (i=0;i<client_count;i++) fds[1+i].fd=clients[i]; fds[1+i].events=POLLIN; fds[1+i].revents=0; // read from serial port and write to client socket(s) poll(fds,1+client_count,500); if (fds[0].revents&POLLIN) { int c=read(serialfd,buffer,1024); int i; for(i=0;i<client_count;i++) write(clients[i],buffer,c); } // read from client sock and write to serial port slowly for(i=0;i<client_count;i++) if (fds[1+i].revents&POLLIN) { int c=read(clients[i],buffer,1024); slow_write(serialfd,(char *)buffer,c); if (c<1) { close(clients[i]); clients[i]=clients[--client_count]; printf("Closed client connection, %d remaining.\n",client_count); } } return 0; }
int main(int argc,char **argv) { char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr; struct bpf_program fp; /* to hold compiled program */ bpf_u_int32 pMask; /* subnet mask */ bpf_u_int32 pNet; /* ip address*/ pcap_if_t *alldevs, *d; int i =0; // Prepare a list of all the devices if (pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); exit(1); } if (argv[1]) dev=argv[1]; else { fprintf(stderr,"You must specify the interface to listen on.\n"); exit(-1); } // If something was not provided // return error. if(dev == NULL) { printf("\n[%s]\n", errbuf); return -1; } // fetch the network address and network mask pcap_lookupnet(dev, &pNet, &pMask, errbuf); // Now, open device for sniffing with big snaplen and // promiscuous mode enabled. descr = pcap_open_live(dev, 3000, 1, 10, errbuf); if(descr == NULL) { printf("pcap_open_live() failed due to [%s]\n", errbuf); return -1; } printf("Started.\n"); fflush(stdout); int last_colour=0x00; int in_vblank=0; int firstraster=1; int bytes=0; int listen_sock = create_listen_socket(6565); while(1) { if (client_sock==-1) { client_sock = accept_incoming(listen_sock); } struct pcap_pkthdr hdr; hdr.caplen=0; const unsigned char *packet = pcap_next(descr,&hdr); if (packet) { if (hdr.caplen == 2132) { // probably a C65GS compressed video frame. if (client_sock!=-1) write(client_sock,packet,2132); } } } printf("Exiting.\n"); return 0; }
/********************************************************************* * open connections and send requested amt of data; estimate b/w */ void simple_server(int num_ports, int base_rx_port) { int datafd, maxlfd; unsigned long diffus; struct timeval last, diff, now ={ 0L, 0L }; int opened=0, closed=0, cnt=0; fd_set fds_listeners, fds_active, fds_finished; int rc; FD_ZERO(&fds_listeners); FD_ZERO(&fds_active); FD_ZERO(&fds_finished); /* listeners on num_ports */ if((maxlfd = create_listeners(&fds_listeners, num_ports, base_rx_port)) < 0) { exit(-1); } datafd = maxlfd+1; while(1) { /* grab any new incoming connections */ rc = accept_incoming(maxlfd, &fds_listeners, &fds_active); if(rc > 0) { opened += rc; } /* select on the data FD's */ rc = send_data(&fds_active, &fds_finished); if(rc > 0) { closed += rc; } cnt++; gettimeofday(&now, (struct timezone *)0); tvsub(&diff, &now, &last); diffus = diff.tv_sec*1e6 + diff.tv_usec; if(diffus > SAMPLE_PERIOD) { int i, totb=0, prog=0; double mbs, tmbs=0.0; fprintf(stderr, "\nBandwidth:\n"); for(i=datafd; i <= maxfd; i++) { if(state[i].tx_sent_cpt) { prog++; } totb += state[i].tx_sent_cpt; mbs = (double)(8.0*state[i].tx_sent_cpt) / (double)diffus; tmbs += mbs; if(state[i].open) { fprintf(stderr,"%c%.4f ",'+', mbs); } else { if(verbose) { fprintf(stderr,"%c%.4f ",'-', mbs); } } state[i].tx_sent_cpt = 0; } fprintf(stderr, "\n\t %d streams active, %d made progress: " "tot = %d, tot Mb/s = %.2f\n" "\t opened %d, closed %d descriptors (loop count %d)\n\n", FD_POP(maxfd, &fds_active), prog, totb, tmbs/scalar, opened, closed, cnt); opened = closed = cnt = 0; last = now; } } /* end of while 1 */ }