Thread: Desperate Over Ecs!

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Desperate Over Ecs!

    Hello guys. It's me again. With that stupid Encrypted Chat System. I thought I fixed it, but no, I didn't.
    It just keep repeating the stuff in child process. Even if there is no message on the socket. I am desperate. Sorry for being a noob. I have few sleepless nights and and I ran out of ideas and possibilities. I need someone with much more experience than me to help me on this.

    Here is the code: http://rafb.net/p/4ZdTCq72.html
    Here is ECSCRYPT.h: http://rafb.net/p/xe0cVO73.html

    which right now doesn't have an encryption algorithm. I am goin gto add it when I get the program working perfectly. There still are some print-outs that are rude or something like that. I didn't get a chance to clean it yet.

    I would be forever in your debt if you helped me solve this problem!


    THANK YOU VERY MUCH.
    <<deleted because of colour and size abuse>>

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Can you post the output from your program so that we can see what you mean?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Looking at this past thread of yours: http://cboard.cprogramming.com/showthread.php?t=105015

    Second, for connect(), bind() and accept() the second argument is a struct sockaddr* so you need typecasting.
    In your case it'd look like this:
    Code:
    connect(clisock, (struct sockaddr*)&clisock_addr, addrlen)
    For the other two, the typecasting is (of course) the same.
    Doesn't look like you did that to me. From your latest code:
    Code:
    if(connect(clisock, &clisock_addr, addrlen) == -1)
    It does look like you figured this out, though. http://cboard.cprogramming.com/showthread.php?t=105091
    Code:
                            recvcheck = recv(clisock, &buff, sizeof(buff), 0);
                            if(recvcheck != 0 && recvcheck != -1)
    [edit] By the way: you should declare your global variables after you include your header files. "struct sockaddr_in" probably won't exist until you #include a header. [/edit]
    Last edited by dwks; 07-14-2008 at 04:26 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    Hey. I know I should do some typecasting there, but it's not a big issue RIGHT NOW. I've done few programs that involved sockets and they work just fine(with a warning at compile-time). I will fix it but that is not the thing that is causing me my problem!
    <<deleted because of colour and size abuse>>

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What is your problem? Post the output you get, and describe what you wanted to get.

    "It just keep repeating the stuff in child process" isn't really enough to go on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Weird

    Well. It keeps writing "Message from serv" or "Message to serv" even after the actual message has been printed. It is hard to explain or show because there things you need to see by running the program. Is it possible to compile, please?
    <<deleted because of colour and size abuse>>

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I didn't fancy it swearing at me.

    Sure, I can compile it. I get quite a few warnings when I do . . . .
    Code:
    In file included from escrypt.c:32:
    escrypt.h: In function ‘ecscrypto’:
    escrypt.h:14: warning: control reaches end of non-void function
    escrypt.h: At top level:
    escrypt.h:8: warning: unused parameter ‘buffer’
    escrypt.h: In function ‘ecsdecrypto’:
    escrypt.h:22: warning: control reaches end of non-void function
    escrypt.h: At top level:
    escrypt.h:16: warning: unused parameter ‘buffer’
    escrypt.c: In function ‘main’:
    escrypt.c:45: warning: format ‘&#37;s’ expects type ‘char *’, but argument 2 has type ‘char (*)[16]’
    escrypt.c:48: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[5]’
    escrypt.c:60: warning: passing argument 2 of ‘connect’ from incompatible pointer type
    escrypt.c:111: warning: passing argument 2 of ‘bind’ from incompatible pointer type
    escrypt.c:125: warning: passing argument 2 of ‘accept’ from incompatible pointer type
    escrypt.c:125: warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness
    escrypt.c:159: warning: control reaches end of non-void function
    escrypt.c: At top level:
    escrypt.c:34: warning: unused parameter ‘argc’
    escrypt.c:34: warning: unused parameter ‘argv’
    [edit] Here's how I got it to compile. I also changed the functions in the header file to return void.
    Code:
    /* 
     * File:   ecs.c
     * Author: cookie
     *
     * Created on June 30, 2008, 3:02 AM
     */
        /*VARIABLES*/
        int choice;
        char port[5];
        int clisock, servsock;
        struct sockaddr_in clisock_addr;
        struct sockaddr_in servsock_addr;
        char ip[16];
        char pass[] = "rightdoggy";
        int addrlen, servaddrlen;
        char buff[48000];
        int i;
        int recvcheck;
        /*ENDOFVARS*/
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <string.h>
    #include <strings.h>
    #include <errno.h>
    #include "escript.h"
     
    int main(int argc, char** argv)
    {   
        printf("\n---------Welcome---------\n");
        printf("\n1. Connect\n2. Listen For Conenction\n");
        printf("\n-------------------------\n");
        scanf("%d", &choice);
        system("clear");
        switch(choice)
        {
            case 1:
                printf("Target IP: ");
                scanf("%s", ip);
                system("clear");
                printf("Port: ");
                scanf("%s", port);
                system("clear");
                clisock = socket(AF_INET, SOCK_STREAM, 0);
                if(clisock == -1)
                {
                    printf("Error Creating Socket");
                    exit(1);
                }         
                clisock_addr.sin_family = AF_INET;
                clisock_addr.sin_port = htons(2110);
                clisock_addr.sin_addr.s_addr = inet_addr(ip);
                addrlen = sizeof(clisock_addr);
                if(connect(clisock, (struct sockaddr *)&clisock_addr, addrlen) == -1)
                {
                    printf("Error Connecting To Target\n");
                    getchar();
                }
                else
                {
                    printf("Established Connection\n");
                    pid_t frk = fork();
                    while(1)
                    {
                        if(frk > 0)
                        {
                            printf("\nMessage to serv: ");
                            fgets(buff, 48000, stdin);
                            ecscrypto(buff);
                            send(clisock, &buff, sizeof(buff), 0);
                        }
                        else if(frk == 0)
                        {
                            recvcheck = recv(clisock, &buff, sizeof(buff), 0);
                            if(recvcheck != 0 && recvcheck != -1)
                            {
                                ecsdecrypto(buff);
                                printf("\nMessage from serv: %s\n", buff);
                            }
                        }
                        else if(frk == -1)
                        {    
                            printf("\nERROR FORKING\n");
                            exit(1);
                        }
                    }
                }
                close(clisock);
            break;
     
            case 2:
                printf("Hello!\n");
                servsock = socket(AF_INET, SOCK_STREAM, 0);
                if(servsock == -1)
                {
                    printf("\nERROR CREATING SOCKET\n");
                    exit(1);
                }
                printf("before addrs\n");
                servsock_addr.sin_family = AF_INET;
                servsock_addr.sin_port = htons(2110);
                servsock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                servaddrlen = sizeof(servsock_addr);
                printf("after addrs\n");
                if(bind(servsock, (struct sockaddr *)&servsock_addr, servaddrlen) == -1)
                {
                    printf("\nERROR BINDING SOCKET\n");
                    exit(1);
                }
                printf("Bound socket\n");
                if(listen(servsock, 5) == -1)
                {
                    printf("\nERROR CREATING BACKLOG\n");
                    exit(1);
                }
                printf("Created backlog\n");
                printf("Now Accepting Connections...\n");
                clisock = accept(servsock, (struct sockaddr *)&clisock_addr, (socklen_t *)&addrlen);
                if(clisock == -1)
                {
                    printf("\nERROR ESTABLISHING CONECTION\n");
                    exit(1);
                }
                printf("\nCONNECTION ESTABLISHED\n");
                pid_t frkserv = fork();
                while(1)
                {
                    if(frkserv > 0)
                    {
                        recvcheck = recv(clisock, &buff, sizeof(buff), 0);
                        if(recvcheck != 0 && recvcheck != -1)
                        {
                            ecsdecrypto(buff);
                            printf("\nMessage from client: %s\n", buff);
                        }             
                    }
                    else if(frkserv == 0)
                    {
                        printf("Message to client: ");
                        fgets(buff, 48000, stdin);
                        ecscrypto(buff);
                        send(clisock, &buff, sizeof(buff), 0);
                    }
                    else if(frkserv == -1)
                    {
                        printf("ERROR FORKING");
                        exit(1);
                    }
                }
            break;
        }
        
        return 0;
    }
    I see what you mean about extra messages being printed.

    server:
    Code:
    Hello!
    before addrs
    after addrs
    Bound socket
    Created backlog
    Now Accepting Connections...
    
    CONNECTION ESTABLISHED
    Message to client: Message to client: Message from client:
    
    Message from client:
    Message from client: message 1
    
    Message from client:
    Message from client: message 2
    
    Message from client:
    Message from client: message 3
    
    Message from client:
    client:
    Code:
    Established Connection
    
    Message from serv:
    
    Message to serv: 
    Message to serv: message 1
    
    Message to serv: message 2
    
    Message to serv: message 3
    Message to serv:
    I don't know why that is, I'm not familiar enough with BSD sockets. I suspect, however, it's in the way you read from the socket.

    First of all, recv() returns 0 "when the peer has performed an orderly shutdown", and -1 on error. [/edit]
    Last edited by dwks; 07-14-2008 at 05:31 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    thanks. Now people know what I was talking about. I suspect it is from the fork(), not the sockets! Thank you very very much. Still hoping I can get this worked out(with your<<all of you>> of course)
    <<deleted because of colour and size abuse>>

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Alright, here's what I think is happening.

    You read a message from the user, and store it in an array with 48000 elements. You send this with a send() call.

    However, you evidently can't send that much at once. When I print the return value of recv, guess what I get?
    Code:
    Hello!
    before addrs
    after addrs
    Bound socket
    Created backlog
    Now Accepting Connections...
    
    CONNECTION ESTABLISHED
    Message to client: Message to client:
    Message from client: "" [32792]
    
    Message from client: "" [15208]
    
    Message from client: "Greetings." [32792]
    
    Message from client: "" [15208]
    In other words, you have to recv() twice for each bunch of 48000 bytes you send, because each recv() is only getting a maximum of 32792 bytes.

    When I change 48000 to 1000 (and I have to do it in lots of places, including the header file), it more or less works.
    Code:
    Hello!
    before addrs
    after addrs
    Bound socket
    Created backlog
    Now Accepting Connections...
    
    CONNECTION ESTABLISHED
    Message to client: Message to client:
    Message from client: "" [1000]
    
    Message from client: "Greetings." [1000]
    
    Message from client: "again" [1000]
    There's still another message sent at the very beginning, but that's another problem, I think.

    Also -- I have to CTRL-C your program every time, since there's no way to exit from it. This results in the port it uses being blocked up, so I had to increment the port every time I ran the program, which was very annoying.

    Not to mention that your program uses 100&#37; of the CPU.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    Thanks. That explains why it used to work at the beginning when i had the array up to 128. I will try and see if it works. How can I make it use less of the CPU?


    THANK YOU!
    <<deleted because of colour and size abuse>>

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can add a sleep(0) in you main loop to give up the cpu.
    Woop?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does sleep(0) actually delay at all? The man page I read didn't say anything about that . . . .

    You could try usleep(1) to delay for one millisecond.

    BTW, both sleep and usleep are in unistd.h.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    Thanks. I knew about sleep(); So that actually reduces the CPU cycles. I makes the program wait a few seconds. I get it.


    THANKS. I still have some problems where the messages you send to the server overlap with the messages you receive. For example, I am about to send a message. And if the server just sends me a message, it overlaps with the one I was about to send. I guess I should make a variable that will be 1 if I am about to send a message. And while(variable == 1) do not receive. Something like that. Anyone has a better idea?
    <<deleted because of colour and size abuse>>

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I suppose that you've getting overlap because both forks of your code are trying to access the screen at the same time. I might create a semaphore or a flag that controls whether the screen is being written to or not. Same basic idea, but it might be useful later to treat it as a "screen-control" flag instead of a "about-to-send" flag.

    Incidentally, there's no real need for the server, apon receiving a message from a client, to immediately send that message back to the client. I'd broadcast the message to all of the connected clients except the client that sent the message in the first place.

    The client knows what it sent. It doesn't need the server to tell it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    The client knows what it sent. It doesn't need the server to tell it.
    But, if the server resends the client's text, or acknowledges it, then you can keep the conversations on all the clients in order.
    If the client doesn't have the server tell it, then this scenario can occur:
    Code:
    Clients "A" says "Hi", Client "B" says "Hello"
    Client A:
    A: Hi
    B: Hello
    
    Client B:
    B: Hello
    A: Hi
    Whether or not this is importance to you is a design decision. (Me and some friends call 'jinx' on each other, yes, over IM. Order of arrival of messages is important there, but it is not guaranteed to be the same on the protocol we use (AIM).)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate !! In need of a helping hand...
    By yojimbo2005 in forum C Programming
    Replies: 20
    Last Post: 11-23-2006, 10:27 AM
  2. Debugging Help - I'm Desperate
    By Tman in forum Game Programming
    Replies: 2
    Last Post: 02-21-2006, 01:39 PM
  3. I don't like doing this but i'm desperate
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-21-2002, 06:44 PM
  4. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM
  5. Desperate to get help w/ program
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 02:59 PM