Contact telnet/socket/whatever before i jump of the window

This is a discussion on Contact telnet/socket/whatever before i jump of the window within the Networking/Device Communication forums, part of the General Programming Boards category; I can open command line (in win32) and write this: telnet localhost 9051 and it opens a connection with an ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    52

    Contact telnet/socket/whatever before i jump of the window

    I can open command line (in win32) and write this:

    telnet localhost 9051

    and it opens a connection with an application that listens that port.

    In vbs i would do this:

    'Make socket name
    set fruit = CreateObject("banana.Socket")


    'Connect to control port
    fruit.Connect "localhost", 9051
    And to send data i would do this:

    fruit.Write "data for teh lulz" & vbcrlf
    And to read data from the app answers:

    'read the buffer
    chktxt = fruit.ReadLine
    How can i produce the same effect in C++?

    Please make an example for each type of interaction i posted here please. It would save me much time to find out the right sintaxe.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  3. #3

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    52
    Isn't there any short tutorial? Those that you gave me are to much complex for what i'm looking, my program will have about only 50 of code and a few data sending. No more than that.
    Any few examples would be great.

    Thanks!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    The problem is your VB brain-rot has got you thinking this is a simple thing to do in a few lines of code. C is much more low level, so it takes longer to do some things.

    You could try a library which does away with some of the complexity, but it's still there.
    http://curl.haxx.se/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    In C/C++ it is going to be harder to do complex things such as networking. Heres some code that i've made that might help you with sockets:

    Socket.h
    Code:
    #ifndef SOCKETH
    #define SOCKETH
    
    #include <Winsock2.h>
    #include <string>
    
    using namespace std;
    
    namespace Socket
    {
        class IpAddress
        {
        private:
    
            in_addr addr;
    
        public:
    
            IpAddress(const string& ip) {
                addr.s_addr = inet_addr(ip.c_str());
            }
    
            IpAddress(unsigned int ip = 0) {
                addr.s_addr = ip;
            }
    
            IpAddress(in_addr ip) {
                addr = ip;
            }
    
            unsigned int ToLong() const {
                return addr.s_addr;
            }
    
            string ToString() const {
                return inet_ntoa(addr);
            }
    
            static IpAddress ResolveHost(const string& hostName);
        };
    
        IpAddress GetSocketIp(int socketId);
        int CreateTcpSocket();
        int CreateUdpSocket();
    
        bool TcpConnect(int socketId, const IpAddress& ipAddress, int port);
        bool UdpBind(int socketId, int port);
        int SendBytes(int socketId, const char* data, size_t dataSize, const IpAddress& ipAddress = 0, int port = 0);
        int ReceiveBytes(int socketId, char* data, size_t dataSize);
        int ReceiveLine(int socketId, char* data, size_t dataSize);
        void StartWinsock();
        void EndWinsock();
    
    }
    
    #endif // SOCKETH
    Socket.cpp
    Code:
    #include <winsock.h>
    #include "Socket.h"
    
    namespace Socket
    {
        sockaddr sendAddr;
        int sendAddrSize = sizeof(sendAddr);
    
        IpAddress IpAddress::ResolveHost(const string& hostName)
        {
            sockaddr_in addr;
            hostent* hostEntry;
    
            if((hostEntry = gethostbyname(hostName.c_str())) == NULL)
                return IpAddress();
    
            addr.sin_addr = *((in_addr*)*hostEntry->h_addr_list);
            return addr.sin_addr.s_addr;
        }
    
        IpAddress GetSocketIp(int socketId)
        {
            SOCKADDR_IN addr;
            int addrLen = sizeof(addr);
            if(getpeername(socketId, (SOCKADDR *)&addr, &addrLen) == -1)
                return IpAddress();
    
            return addr.sin_addr;
        }
    
        int CreateTcpSocket()
        {
            return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        }
    
        int CreateUdpSocket()
        {
            return socket(AF_INET, SOCK_DGRAM, 0);
        }
    
        bool TcpConnect(int socketId, const IpAddress& ipAddress, int port)
        {
            sockaddr_in addr;
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = ipAddress.ToLong();
            addr.sin_port = htons((unsigned short)port);
    
            if(connect(socketId, (sockaddr*)&addr, sizeof(addr)) == -1)
                return false;
    
            return true;
        }
    
        bool UdpBind(int socketId, int port)
        {
            sockaddr_in addr;
    
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = INADDR_ANY;
            addr.sin_port = htons((unsigned short)port);
            if(bind(socketId,(SOCKADDR *)&addr, sizeof(addr)) == -1)
                return false;
    
            return true;
        }
    
        int SendBytes(int socketId, const char* data, size_t dataSize, const IpAddress& ipAddress, int port)
        {
            sockaddr_in addr;
            addr.sin_family = AF_INET;
            addr.sin_port = htons((unsigned short)port);
            addr.sin_addr.s_addr = ipAddress.ToLong();
    
            return sendto(socketId, data, dataSize, 0, (sockaddr *)&addr, sizeof(addr));
        }
    
        int ReceiveBytes(int socketId, char* data, size_t dataSize)
        {
            sockaddr sendAddr;
            int sendAddrSize = sizeof(sendAddr);
    
            return recvfrom(socketId, data, dataSize, 0, (sockaddr *)&sendAddr, &sendAddrSize);
        }
    
        int ReceiveLine(int socketId, char* data, size_t dataSize)
        {
            char byte;
            unsigned int recvCount = 0;
            memset(data, 0, dataSize);
    
            while(recvCount < dataSize && recv(socketId, &byte, 1, 0) > 0)
            {
                if(byte != '\n')
                    data[recvCount++] = byte;
                else break;
            }
    
            return recvCount;
        }
    
        void StartWinsock()
        {
            #ifdef WIN32
            WSADATA wsaData;
            WSAStartup(MAKEWORD(1,1),&wsaData);
            #endif
        }
    
        void EndWinsock()
        {
            #ifdef WIN32
            WSACleanup();
            #endif
        }
    }
    main.cpp
    Code:
    #include <cstdio>
    #include "Socket.h"
    
    int main(int argc, char* argv[])
    {
        Socket::StartWinsock();
    
        int socketId = Socket::CreateTcpSocket();
    
        if(Socket::TcpConnect(socketId, Socket::IpAddress::ResolveHost("www.google.com.au"), 80))
        {
            printf("Connected to: &#37;s\n", Socket::GetSocketIp(socketId).ToString().c_str());
            char text[] = "GET / HTTP/1.1\n"
                        "Host: www.google.com.au\n"
                        "Connection: close\n"
                        "\n";
    
            if(Socket::SendBytes(socketId, text, strlen(text)))
            {
                char recvLine[0xFFFF];
                printf("Sent...awaiting reply\n");
    
                while(Socket::ReceiveLine(socketId, recvLine, sizeof(recvLine)) > 0)
                    printf("Line: %s\n", recvLine);
            }
    
        }
    
        Socket::EndWinsock();
        return 0;
    }
    Last edited by 39ster; 05-29-2008 at 12:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21