SimpleTalk Programming

Here are the files for the programming assignment:

Here are some things to do. The last one, converting to UDP, will be optional for Fall 2000 students. Here is some further information. For the simpletalk server, you start with a ServerSocket. The accept() function here returns connected Socket instances. You then get the InputStream and read from it until end of file, more or less as follows:

 InputStream istr;
 try { istr = s.getInputStream(); }
 catch (IOException ioe) {...}
 byte[] buf = new byte[bufsize];
 int len;
 while (true) {
      try {
           len = istr.read(buf, 0, bufsize);
      }
      catch (IOException ioe) {... }
      if (len == -1) break; // this means EOF
       String str = new String(buf, 0, len);
      System.out.println(str);
 } //while reading from s

Note that this prints out an extra blank line between each output line ... it shouldn't.

For the UDP version, you have to do slightly more work. Much of this is closely tied to the different semantics of UDP sockets. First, you create a DatagramSocket (bound to a specific port on the server, any port on the client). Then, you read from the keyboard and send over the network, on the client, and read from the network and write to the screen, on the server. This part is just like the TCP version.

What changes is sending over the network. To transfer data using UDP, you will use the methods send() and receive(). The parameter for these is of type DatagramPacket. A DatagramPacket is constructed with a byte[] buffer and int length to hold the data, and also an IP address and port. The client only needs to set the IP address (using the getByName() function) and port once; the buffer and length change with each message sent. The server can ignore the IP address and port.