C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 04-01-2007, 09:29 PM   #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;
}
mhetfield is offline  
Old 04-02-2007, 06:02 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,676
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.

Salem is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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