Thread: socket program help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    socket program help

    Hi,

    I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client.
    - the client should send this message (after the connection established): my password "anypassword"
    and the server will check, if it's not the same password that's hardcoded in the server program, the server close connection.

    so the client will be like that:

    1.telnet server
    2. got connected
    3. provide password, if it's not coreect server close connection


    here is my server application:
    Code:
    #include <unistd.h> 
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <ctype.h>
    #include <time.h>
    #define SERVERPORT 111              /* port used for the connection */  
    #define QUEUE 5                       /* max # of queued connects */
    
    int main(void)
    {
        fd_set master;   // master file descriptor list
        fd_set read_fds; // temp file descriptor list for select()
        int fdmax;        // maximum file descriptor number
        int i , j;
        int nbytes;
         int s_listen,d_newconn;  
        int yes=1;
        struct sockaddr_in serveraddr;    /* server address */
        struct sockaddr_in clientaddr;    /* client address */
        struct sigaction sig;
        socklen_t sin_size;               /* Socket address length type */
        string buffer[256];
        
        FD_ZERO(&master);    // clear the master and temp sets
        FD_ZERO(&read_fds);    
     
    
        if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) {    /* create listen socket */
            perror("socket");
            exit(1);
        }
    
        /* set the options for the socket to allow reuse of a local port/address combination. */
    
        if (setsockopt(s_listen,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        
          memset(&(serveraddr.sin_zero), '\0', 8);  /* clear our address */
          serveraddr.sin_family = AF_INET;          /* Address Family */
          serveraddr.sin_port = htons(SERVERPORT);  /* Port number */
          /* a wild IP number to allow the system to pick the route to the remote service */
          serveraddr.sin_addr.s_addr = INADDR_ANY;  
         
          /* bind address to socket */
          if (bind(s_listen, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr))  == -1) {
             perror("bind");
             exit(1);
          }
    
          /*listen*/ 
          if (listen(s_listen, QUEUE) == -1) {
             perror("listen");
             exit(1);
          }
        
    // add the listener to the master set
        FD_SET(s_listen, &master);
    
        // keep track of the biggest file descriptor
        fdmax = s_listen; // so far, it's this one
    
        // main loop
        for(;;) {
            read_fds = master; // copy it
            if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                perror("select");
                exit(1);
            }
    
            // run through the existing connections looking for data to read
            for(i = 0; i <= fdmax; i++) {
                if (FD_ISSET(i, &read_fds)) { // we got one!!
                    if (i == s_listen) {
                        // handle new connections
                        sin_size = sizeof(clientaddr);
                        if ((d_newconn = accept(s_listen, (struct sockaddr *)&clientaddr,
                                                                   &sin_size)) == -1) { 
                            perror("accept");
                        } else {
                            FD_SET(d_newconn, &master); // add to master set
                            if (d_newconn > fdmax) {    // keep track of the maximum
                                fdmax = d_newconn;
                            }
                            printf("EDMTS SERVER: new connection from %s on "
                                "socket %d\n", inet_ntoa(clientaddr.sin_addr), d_newconn);
                        }
                    } else {
                        // handle data from a client
                        if ((nbytes = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
                            // got error or connection closed by client
                            if (nbytes == 0) {
                                // connection closed
                                printf("SERVER: socket %d hung up\n", i);
                            } else {
                                perror("recv");
                            }
                            close(i); // bye!
                            FD_CLR(i, &master); // remove from master set
                        } else {
    /*
                        {{{{{{{{{{{{{{{{{{{{{{ NOT COMPLETED }}}}}}}}}}}}}}}}}
                        {{{{HERE I SHOULD COMPARE THE CLIENT MESSAGE PASSOWRD              IF IT'S THE SAME }}}}}}}}}}    */
                        }
                    } // it's SO UGLY!
                }
            }
        }
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would suggest you read the forum rules and stop spamming the board
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Writing to an Output Socket in use by another program
    By maththeorylvr in forum Windows Programming
    Replies: 4
    Last Post: 10-28-2005, 12:17 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM