For some reason my socket isn't doing anything when i try connect. I have the header file and the class provided below. Everything works except for the socket_connect( ) function.
Also, i do not have a server running. I have a port listener running on localhost. However, it wouldn't detect a connection. I tried connecting to google.com on port 80 and sending something, but it just wouldn't connect.
and the header file itself
Also, i do not have a server running. I have a port listener running on localhost. However, it wouldn't detect a connection. I tried connecting to google.com on port 80 and sending something, but it just wouldn't connect.
Code:
#include "sockets.h" /*---------------------------------------------------------------------------- / Function: socket_start() / Comments: Initializes socket instance /*----------------------------------------------------------------------------*/ bool sockets::socket_start() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2),&wsaData) != NO_ERROR) return false; else return true; } /*---------------------------------------------------------------------------- / Function: socket_end() / Comments: Ends socket instance /*----------------------------------------------------------------------------*/ bool sockets::socket_end() { if (WSACleanup() != 0) return true; else return false; } /*---------------------------------------------------------------------------- / Function: socket_create() / Comments: Creates a socket ready for connection /*----------------------------------------------------------------------------*/ bool sockets::socket_create() { if (hSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP) != -1) return true; else return false; } /*---------------------------------------------------------------------------- / Function: socket_start() / Comments: Uses accessors to retrieve port and address of a server. / Establishes a connection to that server. /*----------------------------------------------------------------------------*/ bool sockets::socket_connect() { my_addr.sin_family=AF_INET; my_addr.sin_port=htons(getPort()); my_addr.sin_addr.s_addr = htonl(getAddr()); if (connect(hSocket, (const struct sockaddr*)&my_addr, sizeof(my_addr)) == -1) return false; else return true; }
Code:
#pragma once #pragma comment(lib, "Ws2_32.lib") #include <WinSock2.h> #include <windows.h> #include <stdio.h> #include <io.h> #include <iostream> #include <conio.h> using namespace std; class sockets { public: /* Constructor */ sockets(char* _tAddr, char* _tPort) { port = atoi(_tPort); addr = atoi(_tAddr); } bool socket_start(); bool socket_end(); bool socket_create(); bool socket_connect(); // Accessors short getPort()const{return port;}; long getAddr()const{return addr;}; protected: SOCKET hSocket; SOCKADDR_IN my_addr; int port; int addr; };
Comment