ithewei 6 年之前
父节点
当前提交
dc063d524f
共有 4 个文件被更改,包括 15 次插入7 次删除
  1. 4 0
      Makefile
  2. 7 3
      hsocket.cpp
  3. 2 2
      hsocket.h
  4. 2 2
      ifconfig.cpp

+ 4 - 0
Makefile

@@ -20,6 +20,10 @@ ifeq ($(OS), Windows_NT)
 	OS=Windows
 endif
 
+ifneq ($(findstring mingw, $(CC)), )
+	OS=Windows
+endif
+
 ifneq ($(findstring android, $(CC)), )
 	OS=Android
 endif

+ 7 - 3
hsocket.cpp

@@ -44,8 +44,8 @@ int Listen(int port) {
     return listenfd;
 }
 
-int Connect(const char* host, int port) {
-    // gethostbyname -> socket -> connect
+int Connect(const char* host, int port, int nonblock) {
+    // gethostbyname -> socket -> nonblocking -> connect
     struct sockaddr_in addr;
     int addrlen = sizeof(addr);
     memset(&addr, 0, addrlen);
@@ -62,7 +62,11 @@ int Connect(const char* host, int port) {
         perror("socket");
         return -20;
     }
-    if (connect(connfd, (struct sockaddr*)&addr, addrlen) < 0) {
+    if (nonblock) {
+        nonblocking(connfd);
+    }
+    int ret = connect(connfd, (struct sockaddr*)&addr, addrlen);
+    if (ret != 0 && ret != EINPROGRESS) {
         perror("connect");
         return -30;
     }

+ 2 - 2
hsocket.h

@@ -7,9 +7,9 @@
 // @return sockfd
 int Listen(int port);
 
-// gethostbyname -> socket -> connect
+// gethostbyname -> socket -> nonblocking -> connect
 // @return sockfd
-int Connect(const char* host, int port);
+int Connect(const char* host, int port, int nonblock = 0);
 
 #ifdef OS_WIN
 inline int blocking(int sockfd) {

+ 2 - 2
ifconfig.cpp

@@ -96,9 +96,9 @@ int ifconfig(std::vector<ifconfig_t>& ifcs) {
 #elif defined(OS_WIN)
 #include <winsock2.h>
 #include <windows.h>
-#include <IphlpApi.h>
+#include <iphlpapi.h>
 #ifdef _MSC_VER
-#pragma comment(lib, "Iphlpapi.lib")
+#pragma comment(lib, "iphlpapi.lib")
 #pragma comment(lib, "ws2_32.lib")
 #endif
 int ifconfig(std::vector<ifconfig_t>& ifcs) {