IC221: Systems Programming (SP17)

Home Policy Calendar Units Assignments Resources

HW 11: Socket Programming


  • 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.


  1. (20 Points) For each of the following system calls, provide a brief, one sentence description:
    1. socket()
    2. bind()
    3. accept()
    4. listen()
    5. connect()
    6. close()
  2. (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);
  3. (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));
  4. (5 points) Explain why when accepting an incoming connection, a new socket is created and returned.
  5. (5 points) Explain the second argument to listen(), the backlog. What does this argument refer to and what does it mean?
  6. (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:
    Connection From:
    Read from client: hello
    Sending: Hello
    Go Navy! Beat Army
    Closing socket
  7. (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++){
        //read from socket
        n = read(sockets[i], buf, BUF_SIZE);
        //socket closed
          sockets[i] = -1;
        //echo back
        write(sockets[i], buf, n);