Telnet Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Telnet Help

  1. #1
    Join Date
    Nov 2010
    Posts
    4

    Question Telnet Help

    Hi i am starting a personal project and i would like someone to point me in the right direction. Basically what i am tryin to do is to get a computer to telnet to another. So the program would therefore run and then i would enter the ip address and port of the target computer i am trying to connect to, then enter the username and password to logon, and then i can run the telnet commands within it.....e.g - telnet 127.0.0.1 100
    I was discussing it with a friend of mine and he was telling me about looking into the use of sockets so thats what i am currently doing. I would like for anyone who is willing to at least let me know if i am going in the right direction or if there is a simpler way to do this besides the socket method. Also any snippets of code are always welcomed.
    Thanks in advance for any help.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: Telnet Help

    Yes you're on the right track but why write something that already exists in so many different flavours?

    See here for instance http://sourceforge.net/search/?q=telnet
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Nov 2010
    Posts
    4

    Re: Telnet Help

    Quote Originally Posted by S_M_A View Post
    Yes you're on the right track but why write something that already exists in so many different flavours?

    See here for instance http://sourceforge.net/search/?q=telnet
    Well as i said its a personal project and im doing it just because....and dont want one that is finished or anything i just want to do one and see if i myself can do as i am still a newbie in programming and then there are other things i plan to do after i finish this step.

  4. #4
    Join Date
    Nov 2010
    Posts
    4

    Re: Telnet Help

    Now the following code i got form http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx and after i ran it in my VS 2010 it compiled without errors but when i ran it the output window flashed and disappeared... can anyone tell me why?
    Also i saw that a guy mentioned that

    "Annoyingly, this wouldn't compile properly with default Visual C++ 2010 project settings. This is because project default to using Unicode.

    Instead of {struct addrinfo ...} you must use {ADDRINFOT ...}$0 $0Instead of getaddrinfo() you must use GetAddrInfo()

    I guess this is just old documentation, but an update so it will compile with a default install of VC would greatly assist beginning programmers like myself "

    However i dont quite understand what hes gettin at or if its even the solution to my problem but any help would be greatly appreciated. Thanks

    _________________________________________________________________________________________________________________________________________________

    UPDATE: well looking at it a second time i think it might be working but im still not sure it builds without errors but then it exits and says

    'Telnet Software.exe': Loaded 'C:\Users\Rod\Documents\Visual Studio 2010\Projects\Telnet Software\Debug\Telnet Software.exe', Symbols loaded.
    'Telnet Software.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\ws2_32.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\advapi32.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\nsi.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
    'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
    'Telnet Software.exe': Loaded 'C:\Windows\System32\mswsock.dll', Symbols loaded (source information stripped).
    'Telnet Software.exe': Loaded 'C:\Windows\System32\WSHTCPIP.DLL', Symbols loaded (source information stripped).
    'Telnet Software.exe': Unloaded 'C:\Windows\System32\WSHTCPIP.DLL'
    The program '[0x139C] Telnet Software.exe: Native' has exited with code 1 (0x1).


    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    
    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "23"
    
    int main() {
        WSADATA wsaData;
        SOCKET ConnectSocket = INVALID_SOCKET;
    
        struct addrinfo *result = NULL,
                        *ptr = NULL,
                        hints;
        char *sendbuf = "this is a test";
        char recvbuf[DEFAULT_BUFLEN];
        int iResult;
        int recvbuflen = DEFAULT_BUFLEN;
        
        // Initialize Winsock
        iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed: &#37;d\n", iResult);
            return 1;
        }
    
        ZeroMemory( &hints, sizeof(hints) );
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
    
        // Resolve the server address and port
        iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result);
        if ( iResult != 0 ) {
            printf("getaddrinfo failed: %d\n", iResult);
            WSACleanup();
            return 1;
        }
    
        // Attempt to connect to an address until one succeeds
        for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
    
            // Create a SOCKET for connecting to server
            ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
            if (ConnectSocket == INVALID_SOCKET) {
                printf("Error at socket(): %ld\n", WSAGetLastError());
                freeaddrinfo(result);
                WSACleanup();
                return 1;
            }
    
            // Connect to server.
            iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
            if (iResult == SOCKET_ERROR) {
                closesocket(ConnectSocket);
                ConnectSocket = INVALID_SOCKET;
                continue;
            }
            break;
        }
    
        freeaddrinfo(result);
    
        if (ConnectSocket == INVALID_SOCKET) {
            printf("Unable to connect to server!\n");
            WSACleanup();
            return 1;
        }
    
        // Send an initial buffer
        iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
        if (iResult == SOCKET_ERROR) {
            printf("send failed: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
    
        printf("Bytes Sent: %ld\n", iResult);
    
        // shutdown the connection since no more data will be sent
        iResult = shutdown(ConnectSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            printf("shutdown failed: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
    
        // Receive until the peer closes the connection
        do {
    
            iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
            if ( iResult > 0 ){
               printf("Bytes received: %d\n", iResult);
               std::cout << "Received data = " << recvbuf;
            }else if ( iResult == 0 ){
               printf("Connection closed\n");
            }else{
               printf("recv failed: %d\n", WSAGetLastError());
            }
    
        } while( iResult > 0 );
    
        // cleanup
        closesocket(ConnectSocket);
        WSACleanup();
    
        system("pause");
        return 0;
    }
    Last edited by Roddy87; November 30th, 2010 at 06:59 AM. Reason: New info added

  5. #5
    Join Date
    Nov 2010
    Posts
    4

    Re: Telnet Help

    Below is the output im gettin now after making a few changes but im not exactly sure what this means. I hope this means that a connection was actually established because then all i would have to do is maybe change the IP address to the address of a computer on the network and then i can begin the telnet process of sending commands. Does anyone have any input?


    OUTPUT:

    Bytes Sent: 14
    Bytes received: 21
    Received data = &#160;&#178;&#37;&#160;&#185;☺&#160;&#185;♥&#160;&#178;'&#160;&#178;▼&#160;&#178;Connection closed
    Press any key to continue . . .

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Telnet Help

    That's probably the telnet server querying the client for supported options. These are established before the session ever gets to a prompt. See the telnet RFC:

    http://www.faqs.org/rfcs/rfc854.html

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center