added client
This commit is contained in:
@@ -46,8 +46,8 @@ int main(int argc, char* argv[]) {
|
|||||||
pthread_t input_thread, send_thread, receive_thread;
|
pthread_t input_thread, send_thread, receive_thread;
|
||||||
socklen_t sin_size;
|
socklen_t sin_size;
|
||||||
struct addrinfo *p, *q;
|
struct addrinfo *p, *q;
|
||||||
struct addrinfo hints, remote_hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *servinfo, *remote_servinfo;
|
struct addrinfo *servinfo;
|
||||||
struct sockaddr_storage their_addr;
|
struct sockaddr_storage their_addr;
|
||||||
char s[INET6_ADDRSTRLEN];
|
char s[INET6_ADDRSTRLEN];
|
||||||
char *msg;
|
char *msg;
|
||||||
@@ -56,7 +56,8 @@ int main(int argc, char* argv[]) {
|
|||||||
int yes = 1;
|
int yes = 1;
|
||||||
|
|
||||||
if (argc != 4) {
|
if (argc != 4) {
|
||||||
printf("Wrong number of arguments\n");
|
printf("Wrong number of arguments. Usage: ./chatty <local port> \
|
||||||
|
<remote IP> <remote port>\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,21 +82,21 @@ int main(int argc, char* argv[]) {
|
|||||||
if ((socketfd = socket(p->ai_family,
|
if ((socketfd = socket(p->ai_family,
|
||||||
p->ai_socktype,
|
p->ai_socktype,
|
||||||
p->ai_protocol)) == -1) {
|
p->ai_protocol)) == -1) {
|
||||||
perror("listener: socket");
|
perror("Server: socket");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bind */
|
/* Bind */
|
||||||
if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
|
if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
|
||||||
close(socketfd);
|
close(socketfd);
|
||||||
perror("listener: bind");
|
perror("Server: bind");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
fprintf(stderr, "listener: failed to bind socket\n");
|
fprintf(stderr, "Server: failed to bind socket\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -110,6 +111,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sin_size = sizeof their_addr;
|
sin_size = sizeof their_addr;
|
||||||
|
printf("Waiting for connection...\n");
|
||||||
confd = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size);
|
confd = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size);
|
||||||
if (confd == -1) {
|
if (confd == -1) {
|
||||||
perror("accept");
|
perror("accept");
|
||||||
@@ -117,8 +119,9 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
inet_ntop(their_addr.ss_family,
|
inet_ntop(their_addr.ss_family,
|
||||||
get_in_addr((struct sockaddr *)&their_addr),
|
get_in_addr((struct sockaddr *)&their_addr),
|
||||||
s, sizeof s);
|
s,
|
||||||
printf("server: got connection from %s\n", s);
|
sizeof s);
|
||||||
|
printf("Getting chatty with %s\n", s);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,42 +129,47 @@ int main(int argc, char* argv[]) {
|
|||||||
else {
|
else {
|
||||||
/* You're the client */
|
/* You're the client */
|
||||||
/* Get remote info */
|
/* Get remote info */
|
||||||
memset(&remote_hints, 0, sizeof(remote_hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
remote_hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
remote_hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
if ((status = getaddrinfo(remote_machine, remote_port, &remote_hints,
|
if ((status = getaddrinfo(remote_machine, remote_port, &hints,
|
||||||
&remote_servinfo)) != 0) {
|
&servinfo)) != 0) {
|
||||||
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
fprintf(stderr,"getaddrinfo error: %s\n",gai_strerror(status));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
for (q = remote_servinfo; q != NULL; q = q->ai_next) {
|
for (q = servinfo; q != NULL; q = q->ai_next) {
|
||||||
/* Create socket */
|
/* Create socket */
|
||||||
if ((socketfd = socket(q->ai_family,
|
if ((socketfd = socket(q->ai_family,
|
||||||
q->ai_socktype,
|
q->ai_socktype,
|
||||||
q->ai_protocol)) == -1) {
|
q->ai_protocol)) == -1) {
|
||||||
perror("talker: socket");
|
perror("Client: socket");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
inet_ntop(q->ai_family, get_in_addr((struct sockaddr *)q->ai_addr),
|
||||||
|
s, sizeof s);
|
||||||
|
printf("Client: attempting connection to %s\n", s);
|
||||||
|
|
||||||
|
if (connect(socketfd, q->ai_addr, q->ai_addrlen) == -1) {
|
||||||
|
perror("Client: connect");
|
||||||
|
close(socketfd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q == NULL) {
|
if (q == NULL) {
|
||||||
fprintf(stderr, "talker: failed to create socket\n");
|
fprintf(stderr, "Client: failed to create socket\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
remote_servinfo = q;
|
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);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user