Sending the list of the directory true socket, += operator problems

This is a discussion on Sending the list of the directory true socket, += operator problems within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to use an operator += for the folowing code, but it dose not works: Code: DIR ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    25

    Sending the list of the directory true socket, += operator problems

    Hi,

    I am trying to use an operator += for the folowing code, but it dose not works:

    Code:
     DIR *dp;
     struct dirent *ep;
     char *serverlist = "";
     dp = opendir ("./");
     if (dp != NULL){
     while (ep = readdir (dp))
      serverlist = ep->d_name;
     (void) closedir (dp);
    The result with this code is, de client receives the list of the last file in that directory. Insted
    Code:
    serverlist = ep->d_name;
    i want to use
    Code:
    serverlist += ep->d_name;
    to link string. My question is, how can i achive this?

    thanks,

    alix

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Do you want strcat(serverlist, ep->d_name) ? You can't concatenate strings in C with + or +=.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    hi oogabooga,

    i searcht for what strcat means and indeed that what i want to achieve
    strcat = "The strcat() function concatenates str2 onto the end of str1, and returns str1."
    So changed it to strcat, but stil same problems:

    Code:
    DIR *dp;
                struct dirent *ep;
                char *serverlist = "";
                dp = opendir ("./");
                if (dp != NULL){
                    while (ep = readdir (dp))
                    strcat(serverlist,ep->d_name);
                    (void) closedir (dp);
                     rv = send(s, serverlist, strlen(serverlist), 0);
                    
                }
                else
                    perror ("Couldn't open the directory");
                stop_received = 1;
    but this way the application crashes
    Last edited by Alix; 01-08-2012 at 11:56 AM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You're not allocating serverlist to anything, it's an empty string literal.

    Use a malloc() on serverlist before trying to modify its contents.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Because serverlist isn't a char array you can append an arbitrary amount of data to.

    Code:
    char serverlist[10000]; 
    serverlist[0] = '\0';
    would be a start, but again would blow up for large directories.

    Or you could just do the send in the loop, and the whole "allocate a large buffer" problem goes away.
    Code:
    while (ep = readdir (dp))
         rv = send(s, ep->d_name, strlen(ep->d_name), 0);                
    (void) closedir (dp);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    Salem,

    i implemented your example, the folowing code did indeed only not 100%. Puting the send function to whilie lus dose solves the problem with the buffer. But the problem is sends only when i kill the server, the client receivs the data only when the server is killd.

    I also tryd with fflush function by implementing after whilie, but that did not helpt.
    Last edited by Alix; 01-08-2012 at 01:26 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    How about reading beej, and practising the basics of opening a connection, sending some text and closing the connection again.

    Your "diagnostic" makes no sense at all.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    Has nothink to do with basic of opennin connection. i have no problem with connection, i can reaceive and send data true client and server thats no problem, i pass that part. Right now i have a problem with requesting a list of the server directory. Clients sends a command"list" and the server receives that command. When the server trys to send the list of directory to client, it stucks in the while loop, only when i kill the server it sends the entire list to client.

    I think by putting the send method in while, that couses the problem. I wil try with malloc.

    Thanks,

    Alix

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Getting stuck in the while() loop probably isn't going to be fixed by malloc, it's more likely a problem with your code to send and receive data. Post the relevant networking parts so we can see if it's correct.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    i've found my problem, it was in the client side. I did not properly opend the socket connection on the client side and also not properly closed the socket connection.

    I have on more question, i did'nt want it to open a new thread so i better ask here.
    My question is how do i link newline"\n" to send function? Or do i have to add newline character at the readdir function? i tryd the folowing by adding '\n'
    Code:
    DIR *dp;            
                struct dirent *ep;            
                dp = opendir ("./");
                
                if (dp != NULL){
                    while (ep = readdir(dp))
                                    
                    rv = send(s, ep->d_name, strlen(ep->d_name), 0&'\n');                
                    
                    (void) closedir(dp);
                }else
                    perror ("Couldn't open the directory");
                
                close(s);
                stop_received = 1;

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > rv = send(s, ep->d_name, strlen(ep->d_name), 0&'\n');
    Just how little C do you know?
    I mean, anyone who knew the basics would know this is such a non-starter.

    Send a \n in another send call, or format a string and send that.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    True i started a week a go with C programming. I did have tried to send "\n" in another send call. But it did not worked that's why i try something different:

    Code:
    DIR *dp;            
                struct dirent *ep;            
                dp = opendir ("./");
                char *newline="\n";            
                
                if (dp != NULL){
                    while (ep = readdir(dp)){
                                        
                    rv = send(s, ep->d_name, strlen(ep->d_name), 0);
                    
                    rv = send(s, newline, strlen(newline), 0);
                    
                    }(void) closedir(dp);
                }else
                    perror ("Couldn't open the directory");
                
                close(s);
                stop_received = 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending files true tcp socket
    By Alix in forum C Programming
    Replies: 8
    Last Post: 01-05-2012, 01:14 PM
  2. Sending TCP ACK + Bytesize (Socket Programming)
    By MaSSaSLaYeR in forum C Programming
    Replies: 9
    Last Post: 11-24-2011, 01:35 AM
  3. Sending/Receiving packets over socket
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 10-22-2010, 10:01 AM
  4. sending in socket
    By fairyjerry14 in forum C Programming
    Replies: 4
    Last Post: 10-08-2007, 07:11 AM
  5. sending zero bytes over TCP socket
    By nantonop in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-03-2007, 08:10 AM

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