Closed Thread
Results 1 to 6 of 6

Thread: server client application - (i really need your help)

  1. #1
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Question server client application - (i really need your help)

    Hello,

    I have developed a client server application using C.

    i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and the passwords to the server and get a response back. basically its a ftp force attack to find the correct username and password. i have written most of the code but i'm kind of strugling to finish it off and sort out the errors. this is a task we have to do for university. i have tried different books and websites and still can't sort out the errors. i really need your help.

    Any advice would be most helpfull

    this is the client part:

    #[HIGHLIGHT="C"]include <stdio.h> /* for printf() and fprintf() */
    #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
    #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
    #include <stdlib.h> /* for atoi() and exit() */
    #include <string.h> /* for memset() */
    #include <unistd.h> /* for close() */

    #define BUFSIZE 4096 /* Size of receive buffer */

    void DieWithError(char *errorMessage); /* Error handling function */
    void handeltcpclient(int sock, char *argv[]); /* Server handling function */

    int main(int argc, char *argv[])
    {
    int sock; /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    unsigned short echoServPort; /* Echo server port */
    char *servIP; /* Server IP address (dotted quad) */

    if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
    {
    fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
    exit(1);
    }

    servIP = argv[1]; /* First arg: server IP address (dotted quad) */

    if (argc == 3)
    echoServPort = atoi(argv[2]); /* Use given port, if any */
    else
    echoServPort = 21; /* 7 is the well-known port for the echo service */

    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed");

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
    echoServAddr.sin_family = AF_INET; /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
    echoServAddr.sin_port = htons(echoServPort); /* Server port */

    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() failed");

    handeltcpclient(sock, argv);

    close(sock);
    }[/HIGHLIGHT]



    this is the handeltcpclient.c

    [HIGHLIGHT="C"]#include <stdio.h> /* for printf() and fprintf() */
    #include <string.h> /* for printf() and fprintf() */
    #include <sys/socket.h> /* for recv() and send() */
    #include <unistd.h> /* for close() */

    #define BUFSIZE 4096 /* Size of receive buffer */

    void DieWithError(char *errorMessage); /* Error handling function */
    char sendBuf[BUFSIZE]; /* Send Buffer */
    char rcvBuf[BUFSIZE];
    char rcvMsgSize[BUFSIZE];
    char srvBuf[BUFSIZE];
    char clntBuf[BUFSIZE];
    char USER[15];
    char PASS[15];
    FILE *fileuser;
    FILE *filepassword;
    int MsgSize;
    int result;
    char response[3];

    void handeltcpclient(int clntSocket)
    {


    /* Recieve message from client */
    memset(&rcvBuf,0,sizeof(rcvBuf));
    rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));

    /* Recieve Server Responses */
    memset (&clntBuf,0,sizeof(clntBuf));
    memset (&srvBuf,0,sizeof(srvBuf));
    MsgSize = RecieveMessage(clntSocket,&srvBuf, sizeof(srvBuf));

    printf("Recieved: %s" &srvBuf,rcvMsgSize);

    response = strncpy(&srvBuf, 3);
    result = strcmp( result, "230");
    if( result == 0)
    {
    sprintf(&clntBuf, "USER %s", USER);
    send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
    DieWithError("send() sent a different number of bytes than expected");


    printf("Recieved: %s" &srvBuf,messageSize);


    /* Opening username.txt to selecting USER */

    if (fileuser = (fopen ("usernames.txt", "r")) ==NULL)
    {
    printf("Error opening file.\n");
    return 1;
    }

    else {
    printf ("File opened.\n");
    while (fgets(USER, 15, file != NULL))
    {
    printf("%s",USER);
    }

    /*send USER to server*/
    sprintf(clntBuf, "USER %s", USER);

    if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf)))
    DieWithError("send() sent a different number of bytes than expected");

    printf("The attemped username was: %s\n");
    return 0;

    /* Recieve message from client */
    memset(&rcvBuf,0,sizeof(rcvBuf));
    rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));

    response = strncpy(&srvBuf, 3);
    result = strcmp( result, "331");
    if( result == 0)
    {
    sprintf(&clntBuf, "PASS %s", PASS);
    send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
    DieWithError("send() sent a different number of bytes than expected");

    result = strncpy(&srvBuf, 3);
    result = strcmp(result, "230");

    if (result == 0)
    {
    printf("successfully connected.\n");
    {
    sucess ++;
    }


    /* Opening passwords.txt to selecting PASS */
    (
    if (filepassword = (fopen ("passwords.txt", "r"))==NULL)
    {
    printf("Error opening file.\n");
    return 1;
    }
    (
    else {
    printf ("File opened.\n");
    while (fgets(PASS, 15,file)!=NULL)
    {
    printf("%s",PASS);
    }
    }
    /*send PASS to server*/
    sprintf(clntBuf, "PASS %s", PASS);

    if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0) != (strlen(clntBuf)))
    DieWithError("send() sent a different number of bytes than expected");

    printf("Closing file.\n");
    fclose(file);
    printf("The attempted password was: %s\n");
    return 0;

    }

    int RecieveMessage (int s,char *buf, int maxLen)
    {
    int received = 0;
    int rv = 0;
    rv = recv(s, buf+received, 5, 0);
    while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
    {
    received += rv;
    rv = recv(s, buf+received, 5, 0);
    }
    if (rv < 0)
    {
    DieWithError("revc() failed");
    }
    return received;
    }

    /*end*/

    this is the diewitherror.c (error handling file)

    #include <stdio.h> /* for perror() */
    #include <stdlib.h> /* for exit() */

    void DieWithError(char *errorMessage)
    {
    perror(errorMessage);
    exit(1);
    }[/HIGHLIGHT]
    Last edited by Jaan; 03-01-2008 at 04:39 AM. Reason: Please use tags when you're posting your codes!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    This type of code tends to be platform specific. What you may want to do is isolate your testing to major components. Is the connection made? Is the username received? Is the password received?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Smile

    i did get a connection before i added the loop in my handeltcpclient.c
    there seems to be a problem with my stringcompare(strcmp and strncpy) can't sort out the errors tho. im using bluefish editor in linux to compile the program. have u got any ideas how to get rid of the errors? i would really appreciate it if you could help me to sort it out as this task is worth 50% of the module and i've never done c programming before

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    If you send us the errors you're getting we may be able to help
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Question

    these are the errors i get

    [student@localhost tcp]$ gcc client.c diewitherror.c handeltcp.c
    gcc: handeltcp.c: No such file or directory
    [student@localhost tcp]$ gcc client.c diewitherror.c handeltcp.c
    gcc: handeltcp.c: No such file or directory
    [student@localhost tcp]$ gcc client.c diewitherror.c handeltcpclient.c
    handeltcpclient.c: In function ‘handeltcpclient’:
    handeltcpclient.c:28: error: incompatible types in assignment
    handeltcpclient.c:35: error: invalid operands to binary &
    handeltcpclient.c:37: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
    handeltcpclient.c:37: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
    handeltcpclient.c:37: error: too few arguments to function ‘strncpy’
    handeltcpclient.c:38: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
    handeltcpclient.c:41: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type
    handeltcpclient.c:46: error: invalid operands to binary &
    handeltcpclient.c:46: error: ‘messageSize’ undeclared (first use in this function)
    handeltcpclient.c:46: error: (Each undeclared identifier is reported only once
    handeltcpclient.c:46: error: for each function it appears in.)
    handeltcpclient.c:51: warning: assignment makes pointer from integer without a cast
    handeltcpclient.c:54: warning: ‘return’ with a value, in function returning void
    handeltcpclient.c:59: error: ‘file’ undeclared (first use in this function)
    handeltcpclient.c:71: warning: ‘return’ with a value, in function returning void
    handeltcpclient.c:75: error: incompatible types in assignment
    handeltcpclient.c:77: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
    handeltcpclient.c:77: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
    handeltcpclient.c:77: error: too few arguments to function ‘strncpy’
    handeltcpclient.c:78: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
    handeltcpclient.c:81: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type
    handeltcpclient.c:85: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type
    handeltcpclient.c:85: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
    handeltcpclient.c:85: error: too few arguments to function ‘strncpy’
    handeltcpclient.c:86: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
    handeltcpclient.c:92: error: ‘sucess’ undeclared (first use in this function)
    handeltcpclient.c:98: error: expected expression before ‘if’
    handeltcpclient.c:139: error: expected declaration or statement at end of input
    handeltcpclient.c:139: error: expected declaration or statement at end of input
    handeltcpclient.c:139: error: expected declaration or statement at end of input
    handeltcpclient.c:139: error: expected declaration or statement at end of input
    handeltcpclient.c:139: error: expected declaration or statement at end of input

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    Well, on line 28 you are attempting to assign an int to an array pointer. In line 35, you are attempting to get the address of a pointer. In line 37, you are passing the address of a pointer. In line 37 you didn't give it a second string to copy.

    etc.

    Take your errors one line at a time and make sure you understand the error and the cause. You will sometimes find that fixing an error adds/removes other errors.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2011, 07:09 AM
  2. C# client server application with databases
    By sims in forum C# Programming
    Replies: 2
    Last Post: 02-03-2009, 10:43 AM
  3. Client / Server Application
    By NikkiRae in forum Java Help
    Replies: 2
    Last Post: 10-11-2007, 04:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts