HW 11: Socket Programming
Instructions
- You must turn in a sheet of paper that is neatly typed or written answering the questions below. (You are strongly encouraged to type your homework.)
- This homework is graded out of 80 points. Point values are associated to each question.
Questions
- (20 Points) For each of the following system calls, provide a brief, one sentence description:
socket()
bind()
accept()
listen()
connect()
close()
(10 points) For each of the arguments and return value in the
socket()
call below, briefly explain their meaning for the socket being opened.int sock; sock = socket(AF_INET, SOCK_STREAM, 0);
(10 points) For each of the arguments and return value in the
connect()
call below, briefly explain their meaning for the connection being established.int res; res = connect(sock, (struct sockaddr *) saddr_in, sizeof(saddr_in));
- (5 points) Explain why when accepting an incoming connection, a new socket is created and returned.
- (5 points) Explain the second argument to
listen()
, thebacklog
. What does this argument refer to and what does it mean? (10 points) Below is output from the
hello_server
program in the course notes. Why does the port change from client to server?#> ./hello_server Listening On: 127.0.0.1:1845 Connection From: 127.0.0.1:42555 Read from client: hello Sending: Hello 127.0.0.1:42555 Go Navy! Beat Army Closing socket
(10 points) Consider the code loop for handling client sockets: Can this program handle multiple clients simultaneously? That is, if multiple clients are connected, will the server be able to services all sockets when data is available? Explain.
char buf[BUF_SIZE]; int sockets[NUMSOCKS], i,n; //iterate over all open sockets for(i=0;i < NUMSOCKS; i++){ if(i>0){ //read from socket n = read(sockets[i], buf, BUF_SIZE); //socket closed if(n<0){ close(sockets[i]); sockets[i] = -1; } //echo back write(sockets[i], buf, n); } }