How do I copy or rewind *argv[]


 
Thread Tools Search this Thread
Top Forums Programming How do I copy or rewind *argv[]
# 8  
Old 08-03-2011
Code:
Accepting a file descriptor: Bad file descriptor
Select failed: Bad file descriptor

struct sockaddr_in serv;
struct sockaddr_in dest;
if ((simpleSocket=socket(AF_INET, SOCK_STREAM,0)) == -1)
if (bind(simpleSocket,(struct sockaddr *)&serv,sizeof(serv)) == -1)
if (listen(simpleSocket, 1000) == -1) 
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
if (FD_ISSET(i, &read_fds))
newfd = accept(simpleSocket, (struct sockaddr *)&dest, NULL);

I thought that if I used a different struct with the accept that the network data would
be part of the (struct sockaddr_in dest). I also have couple more questions to ask
about network programming. Is there a way to receive from a network without stopping
a loop.
# 9  
Old 08-03-2011
Your problem is your third argument to accept(). Accept() wants the length of the structure (dest) in your case. You must pass a pointer to the length, so declare it first:

Code:
 socklen_t dlen;

Then assign and pass it:
Code:
dlen = sizeof( dest );
if( (newfd = accept( simpleSocket, (struct sockaddr *)&dest, &dlen )) < 0 )
       fprintf( stderr, "accept failed: %s\n", strerror( errno ) );

Quote:
Is there a way to receive from a network without stopping
a loop.
If you mean is there a way to test for data ready on any socket without blocking, yes. You can set a timeout on the select. If you set the value to zero, then the select call should return immediately and indicate which file descriptors are ready for non-blocking read. Have a very close look at the select() manual page.
# 10  
Old 08-03-2011
Re

Thanks for this coding.
# 11  
Old 08-03-2011
I used select with a time out value of 0 and I have a send command in my loop to see if
the data will be sent. I still have to send a return to the server before it will send data
the way I want it to. I'll post the whole loop because that might be the problem. Thanks much.
Code:
    for(;;)
    {
        read_fds = master;
        int i;
        if (select(fdmax+1, &read_fds, NULL, NULL, 0) == -1)
        {
            nonfatal("Select failed");
        }
        for(i = 0; i <= fdmax; i++)
        {            
            if (FD_ISSET(i, &read_fds)) // We got a connection
            {
                if (i == simpleSocket) {
                    // handle new connections
                    newfd = accept(simpleSocket, (struct sockaddr *)&inco, &clilen);

                    if (newfd == -1)
                    {
                        nonfatal("Accepting a file descriptor");
                    }
                    else 
                    {
                        FD_SET(newfd, &master); // add to master set
                        if (newfd > fdmax)      // keep track of the max
                        {
                            fdmax = newfd;
                        }
                        printf("New connection from %s on socket %d\n", inet_ntoa(inco.sin_addr), newfd);
                        const char welcome_message[] =
                        {
                            "Welcome: \r\n"
                            "Mud welcome goes here\r\n"
                        };
                        send(newfd, welcome_message, sizeof (welcome_message), 0);
                    }
                }
            }/* END FD_ISSET(i, &read_fds)*/            
        }/* for(i = 0; i <= fdmax; i++) */
                        int j;
                        if(ii<9000)    {ii++;}
                        sprintf(jog, "%d", ii);
                        for(j = 0; j <= fdmax; j++) {
                            // send to everyone!
                            if (FD_ISSET(j, &master)) {
                                    if (send(j, jog, sizeof jog, 0) == -1) {
                                        perror("send");
                                    }
                            }
                    }
    }/* END for(;;).*/

# 12  
Old 08-03-2011
Your select() statement is still not right. The last parameter is supposed to be a pointer to a timeval structure. Passing 0 (interpret as NULL in this case) indicates no timeout and the select will block until there is data for one of the filedescriptors.

Read the man page for select() -- it has a good example of how to use the timeout and illustrates the layout of the timevalue struct.
# 13  
Old 08-03-2011
Could you show me a proper select call?
# 14  
Old 08-04-2011
Quote:
Originally Posted by Errigour
Could you show me a proper select call?
Sure.

Code:
    struct timeval  delay;

    delay.tv_sec = 10;   /* seconds to delay */
    delay.tv_usec = 0;   /* micro seconds to delay */
    select( simpleSocket+1, &readfds, NULL, NULL, &delay)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ARGV how to use it?

So i am trying to read in file readFile <GivenFile> modFile looking for a regular file under the directories in the GivenFile and print them out is my over all goal. basically I am looking for anything that looks like a directory in the given file and printing it out. Since I am trying to do... (2 Replies)
Discussion started by: squidGreen
2 Replies

2. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

3. Programming

help with C, argv

when i run my program, i have a parameter, that i want to set the value to another string i am using int main(int argc, char **argv) { char my_str=argv; printf("%s",my_str); return 0; } and i get Segmentation fault ran using ./my_prog /usr/share/dict/words hello1 ... (2 Replies)
Discussion started by: omega666
2 Replies

4. Programming

ARGV help in C

Hi, Can somehelp help how to list file in a dir? (5 Replies)
Discussion started by: Learnerabc
5 Replies

5. UNIX for Dummies Questions & Answers

Need help to understand cpio and no rewind tapes

SCO openserver 5r5 I only have this available to me ... To list the files... cpio -itcvB < /dev/nrct0 To copy a file out cpio -icvdBum filename < /dev/nrct0So cpio is to archive or "zip" files up?? and /dev/nrct0 is the tape drive ??? How can i list all the files inside... (2 Replies)
Discussion started by: khaos83_2000
2 Replies

6. Shell Programming and Scripting

if #argv = (this OR that) then...

this is in one of my scripts... if ($#argv == 0) then echo 'blah bla' exit 0 endif I want it to be something like this... if ($#argv == 0 OR $argv >=3) echo 'blah bla' exit 0 endif so when the arguments are none, or greater than three I want this "if then" to take over. how? I... (5 Replies)
Discussion started by: ajp7701
5 Replies

7. Programming

Problem with fgets and rewind function ..

Hello Friends, I got stuck with fgets () & rewind() function .. Please need help.. Actually I am doing a like, The function should read lines from a txt file until the function is called.. If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies

8. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

9. UNIX for Dummies Questions & Answers

What is the function of rewind()?

What is the function of rewind()? (2 Replies)
Discussion started by: tigerkin
2 Replies

10. Programming

argv

I have a program which I wish to modify. It used to be run from the command line, but now I wish to change this so it can be used as a function. The program has complex argument processing so I want to pass my paramters to as if it were being called by the OS as a program. I have tried to... (2 Replies)
Discussion started by: mbb
2 Replies
Login or Register to Ask a Question