Help Socket program


 
Thread Tools Search this Thread
Top Forums Programming Help Socket program
# 1  
Old 04-02-2007
Help Socket program

Hi All,

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, the 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("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;
}


Last edited by mhetfield; 04-02-2007 at 12:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. UNIX for Advanced & Expert Users

C socket program on UNIX

Write a C socket program on UNIX where a TCP client sends/reads a time in decimal 24 hours format to the server and the server echoes the seconds, minutes, and hours in the time. Example: Client sends 18.78 hours and the server displays 18 hours, 46 minutes and 48 seconds. (1 Reply)
Discussion started by: adi_always4u143
1 Replies

3. AIX

Sample C program to Send/Recieve a file using Socket

Hi All, I urgently need a Sample C program to Send/Recieve a file using Socket. Thanks Sara (1 Reply)
Discussion started by: saraperu
1 Replies

4. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

5. UNIX for Dummies Questions & Answers

Help to run this socket program in C

i have created two files named server and client then when i run the server program it says the server is waiting(./server 5555) then when i run the client program it says "client error:connection refused" can u plz help me to run it?:( (7 Replies)
Discussion started by: kedah160
7 Replies

6. Programming

Problem with Socket program..

I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server.Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

7. Shell Programming and Scripting

socket program

All, I am looked to develop a socket program from one Solaris server to another Solaris server to send UDP packets from a source UDP port number 2505 on the first server to the source port 2505 on the second server. Is it possible to do? What is the best way to do this? I want to set the... (1 Reply)
Discussion started by: bubba112557
1 Replies

8. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

9. UNIX for Advanced & Expert Users

Fedora Linux for Socket Program Development

Hi, I am trying to port a networking application to linux, I get error while binding a socket to a port, The port is not used by any application and was verified by using netstat and other tools. I tried a simple socket and bind on a unused port, but even that fails. Is there any document... (0 Replies)
Discussion started by: venkatesh.n
0 Replies

10. Programming

How can I program socket in unix?

Excuse me . I'm a beginner . In windows , MFC can be used , but how to do in Unix ? And does unix support c++like VC++ ? How can I get developing tools in Unix ? (7 Replies)
Discussion started by: sanjohn
7 Replies
Login or Register to Ask a Question