Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
/** * File: client-socket.cc * ---------------------- * Presents the implementation of the createClientSocket, which is used for a * a good number of our networking examples. */ #include "client-socket.h" #include // for gethostbyname #include // for socket, AF_INET #include // for SOCK_STREAM #include // for close #include // for memset using namespace std; int createClientSocket(const string& host, unsigned short port) { struct hostent *he = gethostbyname(host.c_str()); if (he == NULL) return kClientSocketError; int s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) return kClientSocketError; struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr = *((struct in_addr *)he->h_addr); if (connect(s, (struct sockaddr *) &address, sizeof(address)) == 0) return s; close(s); return kClientSocketError; }