added logic to determine who's client and who's server
This commit is contained in:
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
#define MAXBUFSIZE 30
|
#define MAXBUFSIZE 30
|
||||||
|
|
||||||
int socketfd, socketfd_send;
|
int socketfd;
|
||||||
|
|
||||||
struct addrinfo hints, remote_hints;
|
struct addrinfo hints, remote_hints;
|
||||||
struct addrinfo *servinfo, *remote_servinfo;
|
struct addrinfo *servinfo, *remote_servinfo;
|
||||||
@@ -56,73 +56,82 @@ int main(int argc, char* argv[]) {
|
|||||||
remote_machine = argv[2];
|
remote_machine = argv[2];
|
||||||
remote_port = argv[3];
|
remote_port = argv[3];
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
if (atoi(local_port) < atoi(remote_port)) {
|
||||||
hints.ai_family = AF_INET;
|
/* Congrats, you're the server */
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_flags = AI_PASSIVE;
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
if ((status = getaddrinfo(remote_machine, local_port, &hints, &servinfo)) != 0) {
|
if ((status = getaddrinfo(NULL, local_port, &hints, &servinfo)) != 0) {
|
||||||
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
/* Create socket */
|
|
||||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
|
||||||
if ((socketfd = socket(p->ai_family,
|
|
||||||
p->ai_socktype,
|
|
||||||
p->ai_protocol)) == -1) {
|
|
||||||
perror("listener: socket");
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bind */
|
|
||||||
if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
|
|
||||||
close(socketfd);
|
|
||||||
perror("listener: bind");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (p == NULL) {
|
|
||||||
fprintf(stderr, "listener: failed to bind socket\n");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
servinfo = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get remote info */
|
|
||||||
memset(&remote_hints, 0, sizeof(remote_hints));
|
|
||||||
remote_hints.ai_family = AF_INET;
|
|
||||||
remote_hints.ai_socktype = SOCK_STREAM;
|
|
||||||
|
|
||||||
if ((status = getaddrinfo(remote_machine, remote_port, &remote_hints,
|
|
||||||
&remote_servinfo)) != 0) {
|
|
||||||
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
for (q = remote_servinfo; q != NULL; q = q->ai_next) {
|
|
||||||
/* Create socket */
|
/* Create socket */
|
||||||
if ((socketfd_send = socket(q->ai_family,
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
q->ai_socktype,
|
if ((socketfd = socket(p->ai_family,
|
||||||
q->ai_protocol)) == -1) {
|
p->ai_socktype,
|
||||||
perror("talker: socket");
|
p->ai_protocol)) == -1) {
|
||||||
continue;
|
perror("listener: socket");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bind */
|
||||||
|
if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
|
||||||
|
close(socketfd);
|
||||||
|
perror("listener: bind");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (p == NULL) {
|
||||||
|
fprintf(stderr, "listener: failed to bind socket\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
servinfo = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (q == NULL) {
|
|
||||||
fprintf(stderr, "talker: failed to create socket\n");
|
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
remote_servinfo = q;
|
/* You're the client */
|
||||||
|
/* Get remote info */
|
||||||
|
memset(&remote_hints, 0, sizeof(remote_hints));
|
||||||
|
remote_hints.ai_family = AF_INET;
|
||||||
|
remote_hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
|
if ((status = getaddrinfo(remote_machine, remote_port, &remote_hints,
|
||||||
|
&remote_servinfo)) != 0) {
|
||||||
|
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
for (q = remote_servinfo; q != NULL; q = q->ai_next) {
|
||||||
|
/* Create socket */
|
||||||
|
if ((socketfd = socket(q->ai_family,
|
||||||
|
q->ai_socktype,
|
||||||
|
q->ai_protocol)) == -1) {
|
||||||
|
perror("talker: socket");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (q == NULL) {
|
||||||
|
fprintf(stderr, "talker: failed to create socket\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
remote_servinfo = q;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pthread_create(&input_thread, NULL, input, (void*) msg);
|
pthread_create(&input_thread, NULL, input, (void*) msg);
|
||||||
|
|
||||||
pthread_join(input_thread, NULL);
|
pthread_join(input_thread, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user