Exemplo n.º 1
0
/*
 * vislib::net::Socket::Accept
 */
vislib::net::Socket vislib::net::Socket::Accept(IPEndPoint *outConnAddr) {
    struct sockaddr_storage connAddr;
    SOCKET newSocket;

#ifdef _WIN32
    INT addrLen = static_cast<int>(sizeof(connAddr));

    if ((newSocket = ::WSAAccept(this->handle, 
            reinterpret_cast<sockaddr *>(&connAddr), &addrLen, NULL,
            0)) == INVALID_SOCKET) {
        throw SocketException(__FILE__, __LINE__);
    }

#else /* _WIN32 */
    unsigned int addrLen = static_cast<unsigned int>(sizeof(connAddr));

    TEMP_FAILURE_RETRY(newSocket = ::accept(this->handle, 
        reinterpret_cast<sockaddr *>(&connAddr), &addrLen));

    if (newSocket == SOCKET_ERROR) {
        throw SocketException(__FILE__, __LINE__);
    }

#endif /* _WIN32 */

    if (outConnAddr != NULL) {
        *outConnAddr = IPEndPoint(connAddr);
    }

    return Socket(newSocket);
}
Exemplo n.º 2
0
Arquivo: ftp.cpp Projeto: ufasoft/snif
void FtpAnalyzer::PrepareEndpoint(RCString sEp) {
	Smatch m;
	if (regex_search(sEp, m, s_reEndpoint)) {
		m_epData = IPEndPoint(htonl((atoi(String(m[1]))<<24) | (atoi(String(m[2]))<<16) | (atoi(String(m[3]))<<8) | atoi(String(m[4]))),
																	WORD((atoi(String(m[5]))<<8)|atoi(String(m[6]))));
	} else
		Throw(E_FAIL);
}
Exemplo n.º 3
0
		void run()
		{
			const char* testData = "Twenty-five or six to four";
			const size_t testDataLen = strlen(testData);
			char* buff = reinterpret_cast<char*>(calloc(testDataLen + 1, 1));

			const int port = 1337;

			try {
				// Create a listener and a client connection
				TCPListener server(port);
				TCPConnection client;

				// Start the server and connect to it
				server.start();
				client.connect(IPEndPoint(IP(127, 0, 0, 1), port));

				// Accept the connection
				auto serverConn = server.accept();

				// Test sending from the server to the client
				serverConn->send(testData, testDataLen);
				client.receive(buff, testDataLen);

				if (strcmp(testData, buff) != 0)
					throw TestFailedException("Data sent from server to client didn't go through properly");

				// Reset the buffer and try sending from
				// the client to the server
				memset(buff, 0, testDataLen);

				serverConn->shutDownSending();
				client.shutDownReceiving();

				client.send(testData, testDataLen);
				serverConn->receive(buff, testDataLen);

				if (strcmp(testData, buff) != 0)
					throw TestFailedException("Data sent from client to server didn't go through properly");

				// Shut down the connections
				client.disconnect();

				if (serverConn->receive(buff, testDataLen) != 0)
					throw TestFailedException("The server was not notified when the client disconnected");

				serverConn->disconnect();

				free(buff);
			}
			catch (...) {
				free(buff);
				throw;
			}
		}
Exemplo n.º 4
0
uint16_t GetFreePort() {
	for (uint16_t port; (port = uint16_t(++s_aTriedPort))<20000;) {
		Socket sock(AddressFamily::InterNetwork);
		try {
			sock.Bind(IPEndPoint(IPAddress::Any, port));
			return port;
		} catch (RCExc) {
		}	
	}
	Throw(HRESULT_FROM_WIN32(ERROR_NO_SYSTEM_RESOURCES));
}
Exemplo n.º 5
0
int main(int argc, char** argv) {
	Poll p;
	Socket s(AF_INET,SOCK_STREAM);
	s.bind(IPEndPoint(IPAddress("0.0.0.0"),16969));
	s.listen();
	s.repeatAccept([&p](Socket* s1) {
		p.add(*s1);
		new client(*s1);
		s1->release();
	});
	p.add(s);
	p.loop();
}
Exemplo n.º 6
0
__attribute__((constructor)) void init()
{
	cout << sizeof(generic_ui_cmd) << endl;
	//XXX: port is hardcoded
	s.connect(IPEndPoint(IPAddress(server), 16969));
	uic.inst=GenericUI::instance;
	uic.onWrite=[](const int8_t* buf, int32_t len) {
		s.send(buf, len, 0);
	};
	uic.doListen();
	pthread_t thr;
	pthread_create(&thr, NULL, &thr1, NULL);
	//listen(cb);
}
Exemplo n.º 7
0
void NetLib::Init() {
	IPAddress^ address;

	address = IPAddress::Parse("127.0.0.1");

	IPEndPoint^ endPoint = gcnew IPEndPoint(address, 8888);
	lsocket = gcnew Socket(endPoint->AddressFamily, SocketType::Stream, ProtocolType::Tcp);
	lsocket->Connect(endPoint);
	if (lsocket->Connected) {
		printf("Conncetion succesful.\n");
	}
	else {
		printf("Conncetion UNSUCCESFULL.\n");
	}
}
Exemplo n.º 8
0
/*
 * vislib::net::Socket::Close
 */
void vislib::net::Socket::Close(void) {

    if (this->IsValid()) {

#ifdef _WIN32
        if (::closesocket(this->handle) == SOCKET_ERROR) {
#else /* _WIN32 */
// panagias: Use shutdown instead of close, see http://stackoverflow.com/questions/2486335/wake-up-thread-blocked-on-accept-call/2489066#2489066
        if (TEMP_FAILURE_RETRY(::shutdown(this->handle, SHUT_RDWR)) == SOCKET_ERROR) {
#endif /* _WIN32 */
            throw SocketException(__FILE__, __LINE__);
        }

        this->handle = INVALID_SOCKET;

    } /* end if (this->IsValid()) */
}


/*
 * vislib::net::Socket::Connect
 */
void vislib::net::Socket::Connect(const IPEndPoint& address) {
#ifdef _WIN32
    if (::WSAConnect(this->handle, static_cast<const struct sockaddr *>(
            address), sizeof(struct sockaddr_storage), NULL, NULL, NULL, NULL)
            == SOCKET_ERROR) {
        throw SocketException(__FILE__, __LINE__);
    }

#else /* _WIN32 */
    // mueller: Linux of kaukerdl showed behaviour as described in 
    // http://www.madore.org/~david/computers/connect-intr.html. I took the
    // fix from there, including the inspiration for the variable naming...
    const struct sockaddr *sa = static_cast<const struct sockaddr *>(address);
    const size_t sal = sizeof(struct sockaddr_storage);
    if (::connect(this->handle, sa, sal) == SOCKET_ERROR) {
        struct pollfd linuxReallySucks;
        int someMoreJunk = 0;
        SIZE_T yetMoreUselessJunk = sizeof(someMoreJunk);

        if (errno != EINTR /* && errno != EINPROGRESS */) {
            throw SocketException(__FILE__, __LINE__);
        }
    
        linuxReallySucks.fd = this->handle;
        linuxReallySucks.events = POLLOUT;
        while (::poll(&linuxReallySucks, 1, -1) == -1) {
            if (errno != EINTR) {
                throw SocketException(__FILE__, __LINE__);
            }
        }

        this->GetOption(SOL_SOCKET, SO_ERROR, &someMoreJunk, 
            yetMoreUselessJunk);
        if (someMoreJunk != 0) {
            throw SocketException(someMoreJunk, __FILE__, __LINE__);
        }
    } /* end if (::connect(this->handle, sa, sal) == SOCKET_ERROR) */
#endif /* _Win32 */
}


/*
 * vislib::net::Socket::Connect
 */
void vislib::net::Socket::Connect(const SocketAddress& address) {
    this->Connect(IPEndPoint(address));
}