Questions about sockets


 
Thread Tools Search this Thread
Top Forums Programming Questions about sockets
# 1  
Old 10-24-2010
Questions about sockets

Hello,
the function listen creates a list that memorizes a number of incoming calls through a socket but how do the first call is deleted and the second becomes the first, is it done using accept?
I have read that accept can wait if it finds no calls in the listen list ,is it the same with read and write.I mean if i have used accept and use read while there is no write from the other side of the socket what will happen?If there was a write but not a read and again a write what will happen to the first string?Is the read function in a constant loop until it finds info to read?
# 2  
Old 10-24-2010
Quote:
Originally Posted by fireblast
Hello,
the function listen creates a list that memorizes a number of incoming calls through a socket but how do the first call is deleted and the second becomes the first, is it done using accept?
accept the first connection and immediately close it.
Quote:
I have read that accept can wait if it finds no calls in the listen list ,is it the same with read and write.
Yes. You can configure them as non-blocking, so they'll return error instead of making you wait, but they are blocking by default.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-25-2010
I have another question , if i use sd=socket(...) on a process and i want to see this socket from an other process to use it in connect for example how can i do this?Is the variable holding the socket descriptor global ,i mean does every process see it once created?
# 4  
Old 10-25-2010
Quote:
Originally Posted by fireblast
I have another question , if i use sd=socket(...) on a process and i want to see this socket from an other process to use it in connect for example how can i do this?Is the variable holding the socket descriptor global ,i mean does every process see it once created?
No, file/socket descriptors are assigned on a per-process basis, so simply passing the descriptor to another process isn't possible. There are methods to do this, but they're more complicated than that - see this recent thread for hints.
This User Gave Thanks to JohnGraham For This Post:
# 5  
Old 10-27-2010
I want to create a child process when there is a call in a socket ,how can i know if there is a least one process in the listen list to create the process so that i will not create needless processes?Does listen blocks the process if there are zero elements in the list?

Last edited by fireblast; 10-27-2010 at 11:01 AM..
# 6  
Old 10-27-2010
use select()

you should use select() to do what you want.

BSD sockets already optimizes this for you.

When you look at the file descriptor in the FD_SET, check if its the listening socket, if it is, then accept a new connection and do what you want.

Code:
 int sel = select(fdmax+1, &read_fds, NULL, NULL, &tv);
        
        if (sel == -1) {
            perror("select()");abort();
        } else if (sel) {
            // data must be ready
			
			for(i = 0; i <= fdmax; i++) {
				if (FD_ISSET(i, &read_fds)) {
					if (i == listening_sock) {
						/* handle a new connection */
						client_sock = accept(listening_sock, (struct sockaddr *) &client_sin, &addr_len);
						
						if (client_sock < 0) {perror("accept failed");abort();}
						FD_SET(client_sock, &master); /* add to master fd set */
						if (client_sock > fdmax) fdmax = client_sock; /* set max */
						
					} else {
						/* read data from client */
						read_bytes = recv(i, buffer, expected_data_len, 0);
						
						// do stuff with data from client
						
					} /* end read data from client */
				} /* end if FD_SET */
			} /* end loop through all fds */
		} /* end while loop */
		
	} else {
		// no data within tv.tv_sec seconds
		continue;
	}

# 7  
Old 11-05-2010
if i have a variable for example buf[10] and i then make a fork in the code if i change the buf variable in one process like buf="end" will the buf in the other process change too?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Any example about sockets in C++?

Hi, i am student, think learning about c++, someone has a example the how establish a conection with sockets :b::b: (1 Reply)
Discussion started by: mmartinez
1 Replies

2. Red Hat

Sockets

hai guys, I'm doing a project in which one server communicates with several clients. How can i do it when i have different port numbers???:confused: (0 Replies)
Discussion started by: rajeshb6
0 Replies

3. Programming

Help with sockets in C

if i have a server which wants to connect to exactly 5 clients, does that mean i need 5 socket file descriptors and use listen(socket_fd,1); for each one or just do listen(socket_fd,5) also whats the second parameter number mean? what happens if i put 0 there? also if i am connected... (28 Replies)
Discussion started by: omega666
28 Replies

4. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

5. Programming

Sockets

Hi,i now moved into a different section where i need to use sockets. i am completely nill in sockets. can some body please provide me what are the requirements for a socket. to use sockets in c. thanks (1 Reply)
Discussion started by: MrUser
1 Replies

6. Programming

need help with sockets

anyone and teach me how to save standard output to a file in a client/server socket. I know how to read them to the screen but i'm not quite sure how to save them to a file. my read to screen file code: memset(line, 0x0, LINE_ARRAY_SIZE); while (recv(connectSocket, line, MAX_MSG, 0) >... (1 Reply)
Discussion started by: crunchyuser
1 Replies

7. Solaris

Sockets in use

Is there a way to see what sockets are in use? The developers here are getting some defunct processes and they would like to get a socket list. This is on a Solaris 8 machine. Thanks! (1 Reply)
Discussion started by: kjbaumann
1 Replies

8. UNIX for Dummies Questions & Answers

sockets

how do i mointor how many sockets are opened from a particular foriegn address? (2 Replies)
Discussion started by: kirpond
2 Replies

9. Programming

Sockets!?!?!?!?!?!

I am looking for a way to have a program listen on a port (example: 8000) for communication I will be sending via that port to it(Linux Kernel machine). Once it recieves an appropiate command I need it to run a .bat file in linux. I know what I need to do but I am running into a few problems:... (8 Replies)
Discussion started by: bigB8210
8 Replies

10. Programming

sockets...

Hi ! I had a verry simple question to ask... In unix when we create pipes.. the unnamed pipes that is... is there any way to access those pipes outside the code ? Another thing.. do sockets have an entry in the inode table ? TIA, Devyani. (1 Reply)
Discussion started by: devy8
1 Replies
Login or Register to Ask a Question