Thread: Very Basic Telnet/Mud Client - coding n00b

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Very Basic Telnet/Mud Client - coding n00b

    Understand first off that I am so green to the idea of programming that I will makes stupid mistakes, and ask stupid questions. Just wanted to throw that out there.

    The mission: Design a MUD client for WebOS to run on my Touchpad.
    Current step: Write a basic telnet app that I can try use as a plugin for the webos pdk.

    Why don't I use an existing telnet app? After hours of searching and trying, I haven't found one that "I" can cross-compile successfully.

    So... I have started working with some code I found online on writing a socket conversation.

    The problem is, that while it connects and reads all the way up to the login, it doesn't appear to be capturing from my keyboard and sending my response. Any insight? Here's what I have so far.

    (ANSI to Come?)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> 
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
    
        char buffer[256];
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0) 
             error("ERROR writing to socket");
        while (server != NULL) {
            printf("Starting Loop");
            bzero(buffer,256);
            n = read(sockfd,buffer,2);
            printf("%s",buffer);
            while (strlen(buffer) > 0) {
                bzero(buffer,256);
                n = read(sockfd,buffer,255);
                printf("%s",buffer);
            }
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        }
        close(sockfd);
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is C, so it should probably go into the C section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    This is C, so it should probably go into the C section.
    Heh, it even compiles without a warning on gcc 4.4.5 on Linux with -Wall and -pedantic.

    *Moved to C programming forum*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    you mean I didn't even get it in the right forum! *facepalm* Clearly, I'm new to this site.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You indentation is throwing me off here. Ok, so this is your telnet side, not the mud side. Are you getting anything from the server you are testing this on? You need to check and see if write is actually writing. How about echoing your input buffer back, so you know it's seen you type something?


    Quzah.
    Last edited by quzah; 10-11-2011 at 03:01 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    because it was already in the code that I found. ;/ Yes, I realize it will pause and wait. I was thinking of either trying for a timeout or figuring out how to do some... thread ... thing that I heard of. Really, all I need it to do is take input and send it, and display incoming. Very basic 'conversation'. I guess. But I have no idea how to do that.... trying to figure it out. I just wanted to make 'this' work and figure out how to properly send data because it doesn't appear to be doing it.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Yes, it is the client end. If my indents are throwing you for a loop, maybe that's where my problem is and I'm misunderstanding my loops....
    The first write will echo, the second one doesn't. It then runs through the whole while server != NULL loop one more time then terminates. Hope that makes sense.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (server != NULL) {
        printf("Starting Loop");
        bzero(buffer,256); /* memset will do this */
        n = read(sockfd,buffer,2);
        printf("%s",buffer);
        while (strlen(buffer) > 0) {
            bzero(buffer,256);
            n = read(sockfd,buffer,255); /* why don't you use n */
            printf("%s",buffer);
        }
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer)); /* again */
    }
    You don't echo your input at all. You do:
    Code:
    starting loop
        echo what you just got from server
        read all from server and echo
        read user input
        write user input
    You never echo the user input that you have typed. You also never check n to see how much was written or read.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I added it in after your suggestion:
    Code:
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        printf("%s",buffer);

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't paying attention to the return value of write (or read even). That will tell you how much is actually written. Also, if that buffer is printed, then you know your program is actually getting your input, which was your concern earlier.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    the buffer prints when used after line 47, but NOT when used after line 62. Could it be that the server end has stopped listening? Could I be missing something that keeps the socket open? After I have sent one instruction and it has responded in full, it seems to be where the dialogue ends.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    you say I am not paying attention to the return value of write (or read even). Does this mean there is another bit of information travelling I don't know about? Some kind of confirmation that I should be receiving (and sending?) I'll have to look into that if that is the case.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    thanks for bearing through my un-schooled thought processes.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are trying to debug what is happening, so you should be paying attention to everything that goes on:
    Code:
    while (server != NULL) /* This seems a weird way to control if you keep going. */
    {
        printf("Starting Loop");
        bzero(buffer,256);
        n = read(sockfd,buffer,2); /* why are you only reading 2 bytes? */
        printf("n:%d, %s",buffer); /* see what read returned */
        while (strlen(buffer) > 0) /* this is kinda odd too */
        {
                bzero(buffer,256);
                n = read(sockfd,buffer,255); /* use read > 0 for loop control */
                printf("%s",buffer);
            }
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer)); /* strlen doesn't include a nul */
        }
        close(sockfd);
        return 0;
    }
    Ok, your loop controls are weird. You'll notice I showed you how to use the return values to see how much a read or write gives you while you are debuggin too.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Okay, Code Update:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> 
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n, c;
        struct sockaddr_in serv_addr;
        struct hostent *server;
    
        char buffer[256];
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");
        c = 1;
        //printf("Please enter the message: ");
        //bzero(buffer,256);
        //fgets(buffer,255,stdin);
        //n = write(sockfd,buffer,strlen(buffer));
        //printf("%s",buffer);
        //if (n < 0) 
        //     error("ERROR writing to socket");
        while (c > 0) {
            printf("Starting Loop");
            bzero(buffer,256);
            n = read(sockfd,buffer,255);
            if (n < 0) {
                c = 0;
                printf("Closing Connection");
            }
            printf("%s",buffer);
            do {
                printf("Buffer Clear \n");
                bzero(buffer,256);
                printf("Buffer Reading ...");
                n = read(sockfd,buffer,255);
                printf("Buffer Read.\n");
                printf("%s",buffer);
                printf("\n");
                printf(" BUFFER DONE : ");
                printf("%d",n);
                printf("\n");
            } while (n > 0);    
        printf("GET INPUT:");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        printf("%d",n);
        printf(" characters input reading: ");
        printf("%s",buffer,"\n");
        }
        close(sockfd);
        return 0;
    }
    Code:
    Buffer Clear
    Buffer Reading ...Buffer Read.
    (Text Content)
    BUFFER Done : 255
    this keeps going. Perfect.

    THEN I get to the login prompt:
    Code:
    By what name are you known (or "new" to create a new character): 
    BUFFER DONE : 118
    Buffer Clear
    My guess is that bzero is hanging.... maybe because the buffer only had 118 characters? Or in the login, the server sent something that is causing a pause. Any insight?
    An
    Last edited by Cebb; 10-11-2011 at 12:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telnet-client, get abracadabra from server
    By LuckyStr in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 05:49 AM
  2. Complete n00b Question, Client -> Sever MMO Program?
    By Zeusbwr in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-28-2005, 08:33 PM
  3. C++ telnet client
    By GUIPenguin in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-30-2005, 11:08 AM
  4. Basic telnet source
    By JustMe in forum Windows Programming
    Replies: 1
    Last Post: 05-27-2003, 02:24 AM
  5. Most Secure (SSH) Telnet Client for Programming
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-14-2002, 08:49 PM