All pastes #1204 Raw Edit

udpbounce

public c v1 · immutable
#1204 ·published 2004-10-02 08:30 UTC
rendered paste body
#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <pthread.h>#include <sys/time.h>int sock;struct ts_pkt {    struct timeval tv;    char rsvd[32 - sizeof(struct timeval)];};void *receiverThread(void *arg) {    int i = 100;    struct ts_pkt ts;    struct timeval tv;    float delay;    while(i--) {        int r = recv(sock, &ts, sizeof(ts), 0);        perror("recv");        gettimeofday(&tv, NULL);        delay = tv.tv_sec - ts.tv.tv_sec + ((tv.tv_usec - tv.tv_usec) / 1000000);        printf("received: %d %d %d %f\n", r, ts.tv.tv_sec, ts.tv.tv_usec, delay);    }}void *senderThread(void *arg) {    int i = 100;    struct ts_pkt ts;    int len = 64;    while(i--) {        gettimeofday(&ts.tv, NULL);        send(sock, &ts, sizeof(ts), 0);        printf("%d %d\n", ts.tv.tv_sec, ts.tv.tv_usec);        usleep(20000);    }}int main(void) {    struct sockaddr_in sa;    int len = sizeof(sa);    pthread_t receiver, sender;        memset(&sa, 0, sizeof(sa));        sock = socket(PF_INET, SOCK_DGRAM, 0);    sa.sin_port = htons(12388);    sa.sin_family = PF_INET;    bind(sock, (struct sockaddr *)&sa, len);        while(1) {        char buf[32];        int r;        r = recvfrom(sock, buf, 32, 0, (struct sockaddr *)&sa, &len);        if(sendto(sock, buf, r, 0, (struct sockaddr *)&sa, len) == -1)            perror("sendto");    }}