Created
May 2, 2012 07:45
-
-
Save anonymous/2574861 to your computer and use it in GitHub Desktop.
test of multiple threads using the same socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netinet/ip.h> | |
#include <unistd.h> | |
#include <netdb.h> | |
#include <pthread.h> | |
#define POSDIGITS 8 | |
char hexchars[16]="0123456789ABCDEF"; | |
void hexdump_line(unsigned char * buf,int chars,int inpos) { | |
int outbufpos=0; | |
int c; | |
char outbuf[90]; | |
for(c=(POSDIGITS-1)*4;c>=0;c-=4){ | |
outbuf[outbufpos++]=hexchars[(inpos>>c)&0x0f]; | |
} | |
outbuf[outbufpos++]=' '; | |
for(c=0;c<chars;c++){ | |
if(c==8) outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=hexchars[buf[c]>>4]; | |
outbuf[outbufpos++]=hexchars[buf[c]&0x0f]; | |
outbuf[outbufpos++]=' '; | |
} | |
for(c=chars;c<16;c++){ | |
if(c==8) outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=' '; | |
} | |
outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=' '; | |
for(c=0;c<chars;c++){ | |
char tch; | |
if(c==8) outbuf[outbufpos++]=' '; | |
tch=buf[c]; | |
if(tch<32 || tch>=127) tch='.'; | |
outbuf[outbufpos++]=tch; | |
} | |
for(c=chars;c<sizeof(buf);c++){ | |
if(c==sizeof(buf)>>1) outbuf[outbufpos++]=' '; | |
outbuf[outbufpos++]=' '; | |
} | |
outbuf[outbufpos]=0; | |
printf("%s\n",outbuf); | |
} | |
void hexdump_buf(char * buf,int len) { | |
int pos=0; | |
int togo=len; | |
while(togo>0) { | |
hexdump_line(buf+pos,togo>16?16:togo,pos); | |
pos += 16; | |
togo -= 16; | |
} | |
} | |
void bork(char* msg) { | |
perror(msg); | |
exit(1); | |
} | |
const static int port = 6000; | |
int sock; | |
void* recvthread(void* threadno_ptr) { | |
while(1) { | |
int threadno = (long int)threadno_ptr; | |
char buf[4096]; | |
printf("thread %d waiting...\n",threadno); | |
fflush(stdout); | |
int len = recv(sock,buf,sizeof buf,0); | |
if(len == 1) bork("recv"); | |
printf("%d received packet:\n",threadno); | |
hexdump_buf(buf,len); | |
printf("\n"); | |
} | |
} | |
int main(void) { | |
sock = socket(AF_INET,SOCK_DGRAM,0); | |
if(sock ==-1) bork("socket"); | |
struct sockaddr_in addr = { AF_INET, htons(port), 0 }; | |
if(bind(sock,(struct sockaddr*)&addr,sizeof addr) == -1) bork("bind"); | |
pthread_t thread1,thread2; | |
pthread_create(&thread1,NULL,recvthread,(void*)1); | |
pthread_create(&thread2,NULL,recvthread,(void*)2); | |
struct sockaddr_in dest = { AF_INET, htons(6001), 0 }; | |
inet_aton("127.0.0.1",&dest.sin_addr); | |
while(1) { | |
char tosend[] = "hi there\n"; | |
sendto(sock,tosend,sizeof(tosend)-1,0,(struct sockaddr*)&dest,sizeof(dest)); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment