Created
September 9, 2019 01:03
-
-
Save matthew798/5afc244681e9b32f8501c5c516ab8273 to your computer and use it in GitHub Desktop.
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 <netinet/in.h> | |
#include <pthread.h> | |
#include <string.h> | |
#include <libnet.h> | |
struct passinfo{ | |
int fd; | |
char *socketName; | |
}; | |
volatile int runningThreads = 0; | |
pthread_mutex_t runningMutex = PTHREAD_MUTEX_INITIALIZER; | |
void DoListen(void* arg) | |
{ | |
struct passinfo pinfo = *(struct passinfo *)arg; | |
int socket = pinfo.fd; | |
char *socketName = pinfo.socketName; | |
char buf[1024]; | |
int read = 0; | |
for(int i = 0; i < 10; i++) | |
{ | |
read = recv(socket, &buf, sizeof(buf), 0); | |
if(read < 0) | |
{ | |
perror("Socket read error"); | |
exit(EXIT_FAILURE); | |
} | |
printf("%s => %s\r", socketName, buf); | |
} | |
pthread_mutex_lock(&runningMutex); | |
runningThreads--; | |
pthread_mutex_unlock(&runningMutex); | |
} | |
int main() { | |
const int on = 1; | |
const int off = 0; | |
int res; | |
int boundSock, boundConSock, remoteSock1, remoteSock2; | |
struct sockaddr_in localEp, remoteEp1, remoteEp2; | |
pthread_t listenThread1, listenThread2; | |
struct passinfo boundPinfo, boundConPinfo; | |
//Set up the local endpoint | |
memset(&localEp, 0, sizeof(struct sockaddr_in)); | |
localEp.sin_family = AF_INET; | |
localEp.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
localEp.sin_port = htons(1114); | |
//Set up the remote endpoints | |
memset(&remoteEp1, 0, sizeof(struct sockaddr_in)); | |
remoteEp1.sin_family = AF_INET; | |
remoteEp1.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
remoteEp1.sin_port = htons(1115); | |
memset(&remoteEp2, 0, sizeof(struct sockaddr_in)); | |
remoteEp2.sin_family = AF_INET; | |
remoteEp2.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
remoteEp2.sin_port = htons(1116); | |
//Create all of the sockets | |
boundSock = socket(AF_INET, SOCK_DGRAM, 0); | |
boundConSock = socket(AF_INET, SOCK_DGRAM, 0); | |
remoteSock1 = socket(AF_INET, SOCK_DGRAM, 0); | |
remoteSock2 = socket(AF_INET, SOCK_DGRAM, 0); | |
//Bind our first socket. | |
res = setsockopt(boundSock, SOL_SOCKET, SO_REUSEADDR, (const void*) &on, sizeof(on)); | |
res = bind(boundSock, (const struct sockaddr *)& localEp, sizeof(struct sockaddr_in)); | |
//Bind and connect our second | |
res = setsockopt(boundConSock, SOL_SOCKET, SO_REUSEADDR, (const void*) &on, sizeof(on)); | |
res = bind(boundConSock, (const struct sockaddr *)& localEp, sizeof(struct sockaddr_in)); | |
res = connect(boundConSock, (const struct sockaddr *)& remoteEp1, sizeof(struct sockaddr_in)); | |
//bind and connect both our remote sockets | |
res = setsockopt(remoteSock1, SOL_SOCKET, SO_REUSEADDR, (const void*)&on, sizeof(on)); | |
res = bind(remoteSock1, (const struct sockaddr *)& remoteEp1, sizeof(struct sockaddr_in)); | |
res = connect(remoteSock1, (const struct sockaddr *)& localEp, sizeof(struct sockaddr_in)); | |
res = setsockopt(remoteSock2, SOL_SOCKET, SO_REUSEADDR, (const void*)&on, sizeof(on)); | |
res = bind(remoteSock2, (const struct sockaddr *)& remoteEp2, sizeof(struct sockaddr_in)); | |
res = connect(remoteSock2, (const struct sockaddr *)& localEp, sizeof(struct sockaddr_in)); | |
memset(&boundPinfo, 0, sizeof(struct passinfo)); | |
boundPinfo.fd = boundSock; | |
boundPinfo.socketName = "Bound Socket"; | |
memset(&boundConPinfo, 0, sizeof(struct passinfo)); | |
boundConPinfo.fd = boundConSock, | |
boundConPinfo.socketName = "Bound and Connected Socket"; | |
pthread_mutex_lock(&runningMutex); | |
runningThreads = 2; | |
pthread_mutex_unlock(&runningMutex); | |
pthread_create(&listenThread1, NULL, (void *)& DoListen, &boundPinfo); | |
pthread_create(&listenThread2, NULL, (void *)& DoListen, &boundConPinfo); | |
char buf1[] = "This should make it to the bound and connected socket"; | |
char buf2[] = "This should make it to the bound socket"; | |
char endBuf[] = "end"; | |
for(int i = 0; i < 10; i++) | |
{ | |
res = send(remoteSock1, &buf1, sizeof(buf1), 0); | |
res = send(remoteSock2, &buf2, sizeof(buf2), 0); | |
} | |
while(runningThreads > 0) | |
{ | |
sleep(1); | |
} | |
fflush(stdout); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment