Skip to content

Commit 6015a13

Browse files
committed
domain name resolving now supported!
1 parent dbd4800 commit 6015a13

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
*.out

chatroom_client.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,32 @@
55
#include "interface.h"
66
#include <netinet/in.h>
77
#include <stdlib.h>
8+
#include <netdb.h>
9+
#include <arpa/inet.h>
810

911
int connect_to(const char* host, const unsigned short port);
1012
struct Reply process_command(const int sockfd, char* command);
1113
void enter_chatmode(const char* host, const int port);
14+
int hostname_to_ip(char* hostname, char* ip);
1215
int REPLY_SIZE = sizeof(struct Reply);
1316

1417
int main(int argc, char** argv){
1518
//If not specified, use defaults.
1619
char* host_addr = argc > 1 ? argv[1] : "chat-room-438.cf";
20+
char* ip_addr[16];
21+
hostname_to_ip(host_addr, ip_addr);
22+
1723
unsigned short port = argc > 2 ? atoi(argv[2]) : 59254;
1824
// printf("Host = %s, Port = %d\n", host_addr, port);
1925

20-
/* if(argc != 3){
26+
/* if(argc != 3){
2127
fprintf(stderr, "Usage: Enter host address and port number\n");
2228
fprintf(stderr, "Example: ./a.out chat-room-438.cf 59254\n");
2329
return EXIT_FAILURE;
24-
}*/
30+
}*/
2531

2632
// Ping the Chatroom Manager to make sure we can send commands
27-
int sockfd = connect_to(host_addr, port);
33+
int sockfd = connect_to(ip_addr, port);
2834
if(sockfd < 0 || write(sockfd, "PING", 4) < 0){
2935
exit(EXIT_FAILURE);
3036
}
@@ -40,15 +46,15 @@ int main(int argc, char** argv){
4046
if(startswith(command, "EXIT")) break;
4147

4248
// Connect to the Chatroom Manager
43-
if((sockfd = connect_to(host_addr, port)) < 0) exit(EXIT_FAILURE);
49+
if((sockfd = connect_to(ip_addr, port)) < 0) exit(EXIT_FAILURE);
4450

4551
// Send command to the Chatroom Manager
4652
struct Reply reply = process_command(sockfd, command);
4753
display_reply(command, reply);
4854

4955
// If a successful JOIN, put this client in chatmode
5056
if(reply.status == SUCCESS && startswith(command, "JOIN")){
51-
enter_chatmode(/*reply.host*/host_addr, reply.port);
57+
enter_chatmode(/*reply.host*/ip_addr, reply.port);
5258
}
5359
}
5460
}
@@ -163,4 +169,20 @@ void enter_chatmode(const char* host, const int port){
163169

164170
close(chat_server);
165171
printf("Exiting ChatMode\n");
166-
}
172+
}
173+
174+
int hostname_to_ip(char* hostname, char* ip){
175+
struct hostent* he;
176+
struct in_addr** addr_list;
177+
int i;
178+
if((he = gethostbyname(hostname))){
179+
addr_list = (struct in_addr**) he->h_addr_list;
180+
for(i=0; addr_list[i]; ++i){
181+
// Return the file one
182+
strcpy(ip, inet_ntoa(*addr_list[i]));
183+
return EXIT_SUCCESS;
184+
}
185+
}
186+
herror("gethostbyname");
187+
return EXIT_FAILURE;
188+
}

0 commit comments

Comments
 (0)