UDP and TCP Sockets

Back Up Next

 

UDP and TCP Sockets
Socket / ServerSocket
Useful for Client/Server TCP Socket programming
Usual Steps on the Client
  1. Open a socket
  2. Open an input stream and output stream to the socket
  3. Read from and write to the stream according to the server's protocol
  4. Close the streams
  5. Close the socket
Usual Steps on the Server
  1. Open the server's socket
  2. Open an output stream to the server's socket
  3. Wait for a client to connect
    When successfully connected,  keep track of the returned client socket
    Open an input stream to the client socket
  4. Read from the server's socket and write to the client's socket according to server protocol
  5. When the client disconnects
    Close the client stream and socket
  6. Close the server's stream and socket
DatagramSocket / DatagramPacket
Useful for Client/Server UDP Socket programming
Usual Steps on the Client
  1. Open a DatagramSocket
  2. Send a DatagramPacket to a known server
  3. (optional) receive the server's DatagramPacket response
  4. Close the DatagramSocket
Usual Steps on the Server
  1. Open a DatagramSocket
  2. Wait for a DatagramPacket to be received
  3. Process the contents of the packet and (optionally) respond according to server protocol
  4. (optional) send a DatagramPacket to the sender
  5. Close the DatagramSocket
QuoteServer.java
QuoteServerThread.java
QuoteClient.java