int main(void) { struct sockaddr_in si_other; int s, i; socklen_t slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } for (i=0; i<NPACK; i++) { printf("Sending packet %d\n", i); sprintf(buf, "This is packet %d\n", i); if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen)==-1) diep("sendto()"); } close(s); return 0; }
int main(int argc, char *argv[]) { struct kevent chlist[2]; /* events we want to monitor */ struct kevent evlist[2]; /* events that were triggered */ char buf[BUFSIZE]; int sckfd, kq, nev, i; /* check argument count */ if (argc != 3) { fprintf(stderr, "usage: %s host port\n", argv[0]); exit(EXIT_FAILURE); } /* open a connection to a host:port pair */ sckfd = tcpopen(argv[1], atoi(argv[2])); /* create a new kernel event queue */ if ((kq = kqueue()) == -1) diep("kqueue()"); /* initialise kevent structures */ EV_SET(&chlist[0], sckfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); EV_SET(&chlist[1], fileno(stdin), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); /* loop forever */ for (;;) { nev = kevent(kq, chlist, 2, evlist, 2, NULL); if (nev < 0) diep("kevent()"); else if (nev > 0) { if (evlist[0].flags & EV_EOF) /* read direction of socket has shutdown */ exit(EXIT_FAILURE); for (i = 0; i < nev; i++) { if (evlist[i].flags & EV_ERROR) { /* report errors */ fprintf(stderr, "EV_ERROR: %s\n", strerror(evlist[i].data)); exit(EXIT_FAILURE); } if (evlist[i].ident == sckfd) { /* we have data from the host */ memset(buf, 0, BUFSIZE); if (read(sckfd, buf, BUFSIZE) < 0) diep("read()"); fputs(buf, stdout); } else if (evlist[i].ident == fileno(stdin)) { /* we have data from stdin */ memset(buf, 0, BUFSIZE); fgets(buf, BUFSIZE, stdin); sendbuftosck(sckfd, buf, strlen(buf)); } } } } close(kq); return EXIT_SUCCESS; }
void send_data_packet(char *fileName, int s){ //packet style: sequence num + length + data printf("send_data_packet\n"); int f, i, fileLength = 0; char readBuffer[LENGTH + 1], ack[5]; unsigned char buf[LENGTH + 8], *ptr; ack[4] = '\0'; if(expectNum < 1) diep("can not start sending data packet \n"); if((f = open(fileName, O_RDONLY)) == -1) diep("can not open the file when sending data packets\n"); while((i = read(f, readBuffer, LENGTH)) > 0){ readBuffer[i] = '\0'; printf("reading: %s\n", readBuffer); ptr = encode_data_struct(buf, i, expectNum, readBuffer); expectNum += i; fileLength += i; //printf("ready recv\n"); while(1){ // small loop to do congestion control if(sendto(s, buf, ptr - buf, 0, &si_other, slen) < 0) diep("send error\n"); alarm(5); //recvfrom(s, ack, sizeof(char) * 4, 0, &si_other, &slen) < 0 || expectNum != decode_ack(ack) if(recvfrom(s, ack, sizeof(char) * 4, 0, &si_other, &slen) >= 0){ if(expectNum == decode_ack(ack)){ alarm(0); break; } } printf("recv error\n"); } } if(expectNum - 1 != fileLength) diep("data sending error\n"); else ackType = 2; }
void send_connection_packet(char *fileName, int s){ // packet style: file name + file size // ack style: num char readBuffer[1000],ack[2]; int f, fileLength = 0, i, j; unsigned char buf[LENGTH + 8], *ptr; ack[1] = '\0'; if((f = open(fileName, O_RDONLY)) == -1) diep("open file failed"); while((i = read(f, readBuffer, 1000)) > 0){ fileLength += i; } for(j = 0;j < strlen(fileName); j++) readBuffer[j] = fileName[j]; ptr = encode_struct(buf, fileLength, readBuffer); if(sendto(s, buf, ptr - buf, 0, &si_other, slen) == -1) diep("connection packet error\n"); while(recvfrom(s, ack, 1, 0, &si_other, &slen) < 0){ sendto(s, buf, ptr - buf, 0, &si_other, slen); } printf("the recv ack is : %s\n", ack); ackType = 1; expectNum = 1; }
/* *** MAIN *** */ int main() { // declarations struct sockaddr_in si_other; // server address int sockfd; int i; int slen = sizeof(si_other); char buf[BUFLEN]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); // not necessary memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SRV_IP, &si_other.sin_addr) == 0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } for (i=0; i<NPACK; i++) { printf("Sending packet %d\n", i); sprintf(buf, "This is packet %d\n", i); if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&si_other, slen) == -1) diep("sendto()"); } close(sockfd); return 0; }
int main(int argc, char** argv){ int i; slen = sizeof(si_other); unsigned char buf[BUFLEN + 8]; if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(s, &si_me, sizeof(si_me)) == -1) diep("bind"); while(end < 0){ if(recvfrom(s, buf, BUFLEN + 8, 0, &si_other, &slen) == -1) diep("recvfrom"); if(connection < 0) decode_buffer(buf); else{ decode_fileContent(buf); } } close(s); return 0; }
int main(void) { struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me))==-1) diep("bind"); for (i=0; i<NPACK; i++) { if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, (socklen_t*)&slen)==-1) diep("recvfrom()"); printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); } close(s); return 0; }
int main(int argc, char *argv[]) { struct sockaddr_in si_me; struct sockaddr_in si_other; int s, i, port; unsigned int slen = sizeof(si_other); char buf[BUFLEN]; if ( (s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) { diep("socket"); return 1; } port = 9999; if (argc > 1) { port = atoi(argv[1]); if (port == 0) port = 9999; } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(port); si_me.sin_addr.s_addr = htonl(INADDR_ANY); // cast to consst to avoid warnings const struct sockaddr * csi_me = (const struct sockaddr *)&si_me; if ( bind(s, csi_me, sizeof(si_me)) == -1 ) { diep("bind"); return 1; } printf("Listening on %d\n", port); for ( i = 0 ; i < NPACK ; i++) { int mes_len = 0; if ( (mes_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)) == -1 ) { diep("recvfrom()"); } printf("Received packet from %s:%d len %d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), mes_len, buf); FILE *cmd_file = fopen("/var/ops/command", "w+"); if (cmd_file == NULL) { diep("Cant write to /var/ops/command"); return 1; } // buf is not a null terminated string //fputs(buf, cmd_file); fwrite(buf, sizeof(char), mes_len, cmd_file); fclose(cmd_file); } close(s); return 0; }
int main(void) { struct sockaddr_in si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; //TEST IF First RUN int firstRun = 1; //VARS FOR SENSOR struct Sensor *mySensor = createSensor(); mySensor->label = CAP_NAME; mySensor->actions[0] = "GetTemp"; char* msg; msg = malloc(512); msg = getInitialMessage(mySensor); if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } if(firstRun!=0) { printf("FIRST RUN \n"); printf("%s", mySensor->label); firstRun =0; //SEND INITIAL DISVOVERY MESSAGE if (sendto(s, msg, BUFLEN, 0,(struct sockaddr *) &si_other, slen)==-1) { diep("sendto()"); } } /*for (i=0; i<NPACK; i++) { printf("Sending packet %d\n", i); sprintf(buf, "This is packet %d\n", i); if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1) diep("sendto()"); }*/ close(s); return 0; }
// From http://www.abc.se/~m6695/udp.html static void LOCAL_udp_listen () { struct sockaddr_in si_me, si_other; socklen_t slen=sizeof(si_other); int s, i, idx; char buf[UDP_BUFLEN]; int channel, channels, value; ssize_t packet_length; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(UDP_PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *) &si_me, sizeof(si_me))==-1) diep("bind"); for (i=0; i<UDP_BUFLEN; i++) { buf[i] = 0; } while (1) { packet_length = recvfrom(s, buf, UDP_BUFLEN, 0, (struct sockaddr *) &si_other, &slen); if (packet_length == -1) { diep("recvfrom()"); } sscanf(buf, "%d", &channels); // server exit condition if (channels < 0) { break; } pruDataMem_byte[DMX_CHANNELS_ADDR] = channels+1; channel = 0; buf[packet_length] = 0; idx = 0; // skip past the channel definition while (++idx < packet_length && buf[idx] != ' '); while (idx < packet_length) { sscanf(buf+idx, " %d", &value); // printf("%3d ", value); if (value > 0) printf("%d ", channel); pruDataMem_byte[channel++] = value; // skip past this integer in the packet while (++idx < packet_length && buf[idx] != ' '); } printf("\n"); } close(s); }
void logToSignalIndexFile(const SignalFileInfo* pSigFileInfo) { // write the string to the index file if(pSigFileInfo->indexFile == NULL) diep("Index file not opened\n"); if(pSigFileInfo->saveTagIndexFile == NULL) diep("Index file not opened\n"); fprintf(pSigFileInfo->indexFile, "%s\n", pSigFileInfo->fileNameRelativeToIndex); fflush(pSigFileInfo->indexFile); fprintf(pSigFileInfo->saveTagIndexFile, "%s\n", pSigFileInfo->fileNameShort); fflush(pSigFileInfo->saveTagIndexFile); }
int main(int argc, char* argv[]){ //判断是否输入了端口号 if(argc!=2){ printf("Usage: %s PortNumber\n",argv[0]); exit(-1); } //把端口号转化成整数 short port = 5555; if((port = atoi(argv[1]))==0){ printf("端口号有误!"); exit(-1); } WSADATA wsa; //初始化套接字DLL if(WSAStartup(MAKEWORD(2,2), &wsa)!=0){ printf("套接字初始化失败!"); exit(-1); } { struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1) diep("bind"); for (i=0; i<NPACK; i++) { if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1) diep("recvfrom()"); printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, slen)==-1) diep("sendto()"); } closesocket(s); } //清理套接字占用的资源 WSACleanup(); return 0; }
int main(int argc, char *argv[]) { if(argc>1){ initUart(argv[1]); }else{ initUart(UART); } char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) { diep("socket"); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, &si_me, sizeof(si_me))==-1) { diep("bind"); } multiWiiInit(multiwiiBuf,sendDataBack); //Start thread for UART pthread_t receive_thread; if(pthread_create(&receive_thread, NULL, receive, NULL)) { diep("Error creating thread"); } int len=0; while (1) { if ((len = recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)) == -1) { diep("recvfrom()"); } // printf("data received\n"); multiWiiStartTransmission(buf,len); //inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); } close(s); return 0; }
//////////////////////////////////////////////////////////////////////////////// /// @brief Send a line on UDP. //////////////////////////////////////////////////////////////////////////////// int sendline(Params params, std::stringstream& line) { int rv ; if ( !line ) { LOG_ERROR( "Error - sendline: null line\n") ; return false ; } struct sockaddr_in si_other ; int s, slen = sizeof(si_other) ; if ( (s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) diep("socket") ; memset((char *) &si_other, 0, sizeof(si_other)) ; si_other.sin_family = AF_INET ; si_other.sin_port = htons(params.backbonePort) ; if ( 0 == inet_aton(params.host, &si_other.sin_addr) ) { LOG_ERROR( "Error - inet_aton(host:%s,port:%i) failed\n", params.host, params.backbonePort ) ; exit(1) ; } struct sockaddr_in servaddr ; bzero(&servaddr, sizeof(servaddr)) ; servaddr.sin_family = AF_INET ; servaddr.sin_addr.s_addr = htonl(INADDR_ANY) ; servaddr.sin_port = htons(params.backbonePort) ; rv = bind(s, (struct sockaddr *) &servaddr, sizeof(servaddr)) ; if( 0 != rv ) { LOG_ERROR("Error - Unable to bind(port:%u)", params.backbonePort ) ; exit(1); } int type ; ::getMsgType(line.str().c_str(), type/*, false*/); if ( type == TX_RF || type==TX_CFG || type==TX_RSP ) { getTaiTime(); line << g_oCfg.sec_frac ; } if ( sendto(s, line.str().c_str(), line.str().length(), 0, (const sockaddr*) &si_other, slen) == -1 ) diep("sendto()") ; else LOG_INFO("\tSENT UDP: [%s]: [host:%s] [port:%d] [%s]\n", szNow(), params.host, params.backbonePort, line.str().c_str() ) ; close(s) ; return true ; }
void send(EventHandler *p, const char *data, int len, bool isDataEnd) { if (!p) { INFO_OUT("Invalid context"); return; } this->pbuff->len = len; memcpy(this->pbuff->buffer, data, len); this->pbuff->destId = (uint16_t)(long)(void*)p->getContext(); sem_t* semDest = (p == this->server) ? semClient : semServer; if (sem_post(semDest) == -1) { diep("sem_post"); } if (semDest == semServer) { if (this->server) { semNext = semServer; } else { semNext = semClient; } } else { if (this->client) { semNext = semClient; } else { semNext = semServer; } } }
int main(int argc, char *argv[]) { struct sockaddr_in si_other; int s, len; char buffer[1024]; if (argc != 4) { fprintf(stderr, "Usage: 'udp-stress IP PORT METRIC'\n"); exit(-1); } srand(time(NULL)); memset(&si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(atoi(argv[2])); if (inet_aton(argv[1], &si_other.sin_addr) == 0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) diep("socket"); len = build_packet(buffer, argv[3]); sendto(s, buffer, len, 0, (void *)&si_other, sizeof(si_other)); close(s); return 0; }
int main(int argc, char *argv[]) { int sock; struct ifconf ifconf; struct ifreq ifreq[MAXINTERFACES]; int interfaces; int i; // Create a socket or return an error. sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) diep("socket"); // Point ifconf's ifc_buf to our array of interface ifreqs. ifconf.ifc_buf = (char *) ifreq; // Set ifconf's ifc_len to the length of our array of interface ifreqs. ifconf.ifc_len = sizeof ifreq; // Populate ifconf.ifc_buf (ifreq) with a list of interface names and addresses. if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) diep("ioctl"); // Divide the length of the interface list by the size of each entry. // This gives us the number of interfaces on the system. interfaces = ifconf.ifc_len / sizeof(ifreq[0]); // Print a heading that includes the total # of interfaces. printf("IF(%d)tIPn\n", interfaces); // Loop through the array of interfaces, printing each one's name and IP. for (i = 0; i < interfaces; i++) { char ip[INET_ADDRSTRLEN]; struct sockaddr_in *address = (struct sockaddr_in *) &ifreq[i].ifr_addr; // Convert the binary IP address into a readable string. if (!inet_ntop(AF_INET, &address->sin_addr, ip, sizeof(ip))) diep("inet_ntop"); printf("Name: %s\nIP: %s\n", ifreq[i].ifr_name, ip); } close(sock); exit(0); }
void send_udp_datagram(void *buf, int len, int socket, char *target_host, int target_port) { struct sockaddr_in target_si; struct hostent *he; memset((char *) &target_si, 0, sizeof(target_si)); target_si.sin_family = AF_INET; target_si.sin_port = htons(target_port); if (!(he = gethostbyname2(target_host, AF_INET))) diep("gethostbyname2"); memmove(&target_si.sin_addr.s_addr, he->h_addr, he->h_length); if (sendto(socket, buf, len, 0, (struct sockaddr *) &target_si, sizeof(target_si)) == -1) diep("sendto"); }
// From http://www.abc.se/~m6695/udp.html static void LOCAL_udp_listen () { struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); char buf[UDP_BUFLEN]; int channel, channels, value; int packet_length; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(UDP_PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, &si_me, sizeof(si_me))==-1) diep("bind"); for (i=0; i<UDP_BUFLEN; i++) { buf[i] = 0; } // signal(SIGABRT, &sighandler); // signal(SIGTERM, &sighandler); // signal(SIGINT, &sighandler); while (udp_forever) { packet_length = recvfrom(s, buf, UDP_BUFLEN, 0, &si_other, &slen); if (packet_length == -1) { diep("recvfrom()"); } sscanf(buf, "%3d ", &channels); pruDataMem_byte[DMX_CHANNELS_ADDR] = channels+1; // does this get updated too often? channel = 0; buf[packet_length] = 0; for (i=4; i<packet_length; i+=4) { // printf("\tReceived packet (size %d) from %s:%d\nData: %s\n\n", packet_length, inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); sscanf(buf+i, "%3d ", &value); // printf("%3d %3d ", channel, value); pruDataMem_byte[channel++] = value; } // printf("\n"); } close(s); }
int main(void) { struct sockaddr_in si_me, si_other; int s, i; int slen = sizeof(si_other); char buf[BUFLEN]; uint64_t start; uint64_t end; uint64_t elapsed; uint64_t elapsedNano; static mach_timebase_info_data_t sTimebaseInfo; if ( sTimebaseInfo.denom == 0 ) { (void) mach_timebase_info(&sTimebaseInfo); } start = mach_absolute_time(); if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) { diep("socket"); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { diep("bind"); } for (i = 0; i < NPACK; i ++) { if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, (unsigned int *)&slen)==-1) { diep("recvfrom()"); } end = mach_absolute_time(); // printf("Received packet from %s:%d\nData: %s\n", // inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); printf(" time: %llu ns, elapsed=%llu ns\n", end, end-start); start = end; } close(s); return 0; }
void send_end_packet(int s){ printf("send_end_packet\n"); char end[2], rec[2]; end[0] = '1'; end[1] = '\0'; rec[1] = '\0'; if(sendto(s, end, 1, 0, &si_other, slen) == -1) diep("end packet error\n"); while(recvfrom(s, rec, 1, 0, &si_other, &slen) < 0){ if(sendto(s, end, 1, 0, &si_other, slen) == -1) diep("end packet error\n"); } if(rec[0] != '1') diep("wrong fin\n"); fin = 1; }
int main(int argc, char** argv){ int s, i; slen = sizeof(si_other); char fileName[1000]; struct sigaction act; act.sa_handler = sighandler; act.sa_flags = 0; strcpy(fileName, argv[1]); nameLength = strlen(fileName); if(argc != 2) diep("please enter filename"); if(sigaction(SIGALRM, &act, 0) == -1) perror("sigaction\n"); if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if(inet_aton(SRV_IP, &si_other.sin_addr) == 0){ fprintf(stderr, "inet_aton() failed\n"); exit(1); } while(fin < 0){ if(ackType == -1) send_connection_packet(fileName, s); else if(ackType == 1) send_data_packet(fileName, s); else send_end_packet(s); } printf("end the connection\n"); close(s); return 0; }
int main(void) { struct sockaddr_in si_me, si_other; int s, i, slen = sizeof(si_other); int n; uint8_t buf[BUFLEN]; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY ); if (bind(s, (struct sockaddr*) &si_me, sizeof(si_me)) == -1) diep("bind"); printf("Starting server\n"); while (1) { printf("."); n = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (socklen_t *) &slen); if (n == -1) { diep("recvfrom()"); break; } printf("Received packet from %s on port %d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); for (i = 0; i < n; i++) printf("%x ", buf[i]); printf("\n"); int64_t timestamp; memcpy(×tamp, buf+145, 8); printf(" Timestamp = %llu\n\n", timestamp); } printf("---------------------\n"); close(s); return 0; }
void doSend(char* toSend) { printf("Sending packet %d\n", num); sprintf(buf, "This is packet %d\n", num++); //times 2 because a short is two characters int sentBytes = sendto(s, toSend, BUFLEN * 2, 0, (struct sockaddr *) &si_other, slen); if (sentBytes ==-1) { diep("sendto()"); } }
void sendbuftosck(int sckfd, const char *buf, int len) { int bytessent, pos; pos = 0; do { if ((bytessent = send(sckfd, buf + pos, len - pos, 0)) < 0) diep("send()"); pos += bytessent; } while (bytessent > 0); }
int main(void) { UDPTerm udp; int i; int msglen; char buf[512]; if(open_udp_client(&udp, 1)) diep("udpClient_Init"); for (i=0; i<10; i++) { printf((char*)"Sending packet %d\n", i); msglen=sprintf(buf, (char*)"This is packet %d\n", i); if (udp_send(&udp, buf, msglen+1, "192.168.1.1", 9930, NULL) == -1) diep("send"); usleep(100000); } close_udp(&udp); return 0; }
void server_new(char *name) { server_t *server; if(!(server = (server_t *) calloc(1, sizeof(server_t)))) diep("[-] calloc"); server->server = strdup(name); list_append(global_lib.servers, (char *) name, server); // list_dump(global_lib.servers); }
int open_udp_socket(int port) { struct sockaddr_in si_me; int s; int reuse = 1; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) != 0) diep("setsockopt"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(port); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *) &si_me, sizeof(si_me)) != 0) diep("bind"); return s; }
void createSharedMem() { if ((key = ftok("/tmp", 'R')) == -1) { diep("ftok"); } if ((shmemid = shmget(key, SharedMemorySize, 0644 | IPC_CREAT)) == -1) { diep("shmemget"); } pbuff = (membuff *)shmat(shmemid, (void*)0, 0); if (pbuff == (membuff*)-1) { diep("shmat"); } semServer = sem_open(ServerSemName, O_CREAT, 0644, 0); semClient = sem_open(ClientSemName, O_CREAT, 0644, 0); if (this->server) { semNext = semServer; } else { semNext = semClient; } }
void writeMxArrayToSigFile(mxArray* mxTrial, mxArray* mxMeta, const SignalFileInfo *pSigFileInfo) { int error; // open the file MATFile* pmat = matOpen(pSigFileInfo->fileName, "w"); if(pmat == NULL) diep("Error opening MAT file"); // put variable in file error = matPutVariable(pmat, "trial", mxTrial); if(error) diep("Error putting variable in MAT file"); error = matPutVariable(pmat, "meta", mxMeta); if(error) diep("Error putting variable in MAT file"); // close the file matClose(pmat); }