Thread: Very Basic Telnet/Mud Client - coding n00b

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cebb View Post
    Is it possible to share a socket between two threads?
    Yes, but you will have to use mutex locks on it or you will end up with a nasty mess. Do not bother trying come up with (apparently) clever ways to circumvent this issue; you must use a thread safe form of locking on ALL SHARED VARIABLES, INCLUDING FD's (such as a socket). WRT to pthreads, your standard thread-safe lock is the mutex. End of story. Use a mutex, or get f-uped.

    Perhaps your problem is this:

    Code:
    pthread_create(&thr, NULL, &SockList, &sockfd)
    
        int sockfd, n, c;
        sockfd = (int)arg;
    So arg is a pointer to an int, then you assign the pointer value to your sockfd inside the thread. I think you want to dereference arg first:

    Code:
    sockfd = (int)(*arg);
    or
    sockfd = *(int*)arg;
    Last edited by MK27; 10-11-2011 at 12:12 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    YAY! Successful Mud Session!

    Now, any suggestions as to how to make the fgets non blocking?

    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;
        while(c > 0) {
            do {
                bzero(buffer,256);
                n = read(sockfd,buffer,255);
                if (n < 0) {
                    c = 0;
                    printf("Closing Connection");
                }
                printf("%s",buffer);        
            } while (strlen(buffer) == 255);    
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        }
        close(sockfd);
        return 0;
    }
    Last edited by Cebb; 10-11-2011 at 02:49 PM.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cebb View Post
    Code:
            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);
    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
    I think your read was hanging, because the mud was waiting for your to type something. There was no newline on "Buffer Reading ..." so you didn't see that as it was waiting on a read.


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

  4. #19
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I assumed it had something to do with the 'read' and nothing being there to come blocking the session. so I set the while (strlen(buffer) == 255 so that it only asks for more data if the last buffer was filled.

    Right now my primary concern is more that of the blocking fgets stdin. Google tells me I can put it in another thread or use select, but I can't find any good examples on how to do that to get a non-blocking keyboard input.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to use threads, or something like the ncurses library, which I believe lets you read key hits instead of line buffering. I haven't used it in a long time.


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

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