HW #2 Questions and Answers

  1. How do I validate a usercode using the stockdb routines ? (there is no function that simply checks a usercode).

    A: You can just use sqd_numshares().


  2. Should the portfolio command really return the price of each stock held by the user ?

    A: No - the homework description was wrong - the number of shares held by the user makes more sense.


  3. How can I test my TCP ticker ?

    A: Use the telnet command, which allows you to open a TCP connection to any tcp port.


  4. OK - I'm using telnet to test my ticker server, but how do I get out of telnet ?

    A: This depends on what version of telnet you are using, most allow you to interrurt the telnet session with the control character ^[. Then you can type quit.


  5. How do I find out what port I've been assigned by the bind system call() ?

    A: You need to use getsockname():

    
      if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0)
          {
            printf("Error getsockname\n");
            exit(1);
          }
      
      printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));
    

  6. Do I have to send the entire datagram at once, or can I send it in chunks? For instance, the logout response consists of 5 bytes. can I send the transaction id and then the transaction flag, or do I have to put them together and send them as a package?

    A: Each UDP write/sendto will correspond to a read/recv - so if the reader is expecting the tranaction ID and flag in a single message (read) - you need to make sure it is all sent together.

    For HW#2 you must match the datagrams that are in the homework assignment, so you need to construct an entire message (datagram) and then send it.


  7. I'm confused about how to put an integer into four bytes...does that mean that the largest value is 9999? A: "9999" is not an integer - it is a charater string. If you have an integer variable it is already 4 bytes long (that is the internal representation used for integers on all the platforms used on campus). read/write/send/recv all operate on bytes, not characters - so you can give them any collection of bytes. Example: to write the 4 byte integer value held by the variable foo, just give write the address of foo
    int foo;
    ....
    write(fd,&foo,4);
    
    This will send the 4 byte integer variable to the file descriptor fd, keep in mind that the representation (byte order) may be different between varius machines so that reading the same 4 bytes may not be enough. For homework #2 you need to convert to and from network byte order, so that the client and server could be running on any kind of machine.

    NOTE: if fd is a UDP socket - this will result in a 4 byte UDP datagram, this is not what you want for HW#2 (see question 6).