Skip to content

Commit c607f3b

Browse files
authored
feat: adding support for hostnames on worker connection (#232)
1 parent 03c4022 commit c607f3b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

examples/chat-api-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// 2. Run this script: `node examples/chat-api-client.js`
77

88
const HOST = process.env.HOST ? process.env.HOST : '127.0.0.1';
9-
const PORT = process.env.PORT ? Number(process.env.PORT) : 9999;
9+
const PORT = process.env.PORT ? Number(process.env.PORT) : 5000;
1010

1111
async function chat(messages, maxTokens) {
1212
const response = await fetch(`http://${HOST}:${PORT}/v1/chat/completions`, {

src/nn/nn-network.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ typedef SSIZE_T ssize_t;
99
#include <netinet/tcp.h>
1010
#include <arpa/inet.h>
1111
#include <unistd.h>
12+
#include <netdb.h> // for getaddrinfo
1213
#endif
1314
#include "nn-network.hpp"
1415
#include <cassert>
@@ -141,17 +142,33 @@ static void writeAckPacket(int socket) {
141142
}
142143

143144
static inline int connectSocket(char *host, int port) {
144-
struct sockaddr_in addr;
145-
memset(&addr, 0, sizeof(addr));
146-
addr.sin_family = AF_INET;
147-
addr.sin_addr.s_addr = inet_addr(host);
148-
addr.sin_port = htons(port);
145+
146+
int sd, err;
147+
struct addrinfo hints = {}, *addrs;
148+
char port_str[16] = {};
149+
sprintf(port_str, "%d", port);
150+
151+
hints.ai_family = AF_INET;
152+
hints.ai_socktype = SOCK_STREAM;
153+
hints.ai_protocol = IPPROTO_TCP;
154+
err = getaddrinfo(host, port_str, &hints, &addrs);
155+
if (err != 0){
156+
fprintf(stderr, "%s: %s\n", host, gai_strerror(err));
157+
abort();
158+
}
159+
160+
struct addrinfo *addr = addrs;
161+
if (addr == NULL){
162+
fprintf(stderr, "%s: %s\n", host, "addresses were null");
163+
abort();
164+
}
165+
166+
int sock = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
149167

150-
int sock = ::socket(AF_INET, SOCK_STREAM, 0);
151168
if (sock < 0)
152169
throw std::runtime_error("Cannot create socket");
153170

154-
int connectResult = ::connect(sock, (struct sockaddr*)&addr, sizeof(addr));
171+
int connectResult = ::connect(sock, addr->ai_addr, addr->ai_addrlen);
155172
if (connectResult != 0) {
156173
printf("Cannot connect to %s:%d (%s)\n", host, port, SOCKET_LAST_ERROR);
157174
throw std::runtime_error("Cannot connect");

0 commit comments

Comments
 (0)