11 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <netinet/in.h> 15 #include <netinet/tcp.h> 34 const char* colon = strchr(host,
':');
36 const char* colon = index(host,
':');
38 short unsigned int port = 26542;
42 port = atoi(colon + 1);
43 std::string hostname(host, colon - host);
44 he = gethostbyname(hostname.c_str());
47 he = gethostbyname(host);
52 int sd = (int)socket(PF_INET, SOCK_STREAM, 0);
57 far_end.sin_family = AF_INET;
58 far_end.sin_port = htons(port);
59 far_end.sin_addr = *(in_addr*)(he->h_addr);
60 memset(&far_end.sin_zero,
'\0', 8);
61 if (connect(sd, (sockaddr*)&far_end,
sizeof(far_end)) == -1)
62 THROWERRNO(
"connect(" << host <<
':' << port <<
")");
67 _write(sd, &
id,
sizeof(
id)) < (
int)
sizeof(
id)
69 write(sd, &
id,
sizeof(
id)) < (
int)
sizeof(
id)
72 std::cerr <<
"write failed!" << std::endl;
int open_socket(const char *host)