teaching machines

CS 245 Lecture 27 – Sockets

May 6, 2014 by . Filed under cs245, lectures, spring 2014.

Agenda

Code

Timestamp.java

package lecture27;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Timestamp {
  public static void main(String[] args) throws IOException {
    Socket socket = new Socket("time-A.timefreq.bldrdoc.gov", 13);
    
    Scanner in = new Scanner(socket.getInputStream());
    while (in.hasNextLine()) {
      System.out.println(in.nextLine());
    }
    in.close();
    
    socket.close();
  }
}

WhetherWeather.java

package lecture27;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Scanner;

public class WhetherWeather {
  public static void main(String[] args) throws IOException {
    URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=Eau%20Claire,WI");
    URLConnection connection = url.openConnection();
    connection.connect();
    
    Scanner in = new Scanner(connection.getInputStream());
    StringBuilder builder = new StringBuilder();
    while (in.hasNextLine()) {
      builder.append(in.nextLine());
    }
    in.close();
    
    System.out.println(builder.toString());
    
    in = new Scanner(builder.toString());
    in.useDelimiter("\\D");
    in.findInLine("\"sunset\":");
    long time = in.nextLong();
    in.close();
    
    System.out.println(new Date(time * 1000L));
  }
}

Rhymer.java

package lecture27;

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Rhymer extends JFrame {
  private JTextField wordBox;
  private JButton getButton;
  private JList<String> rhymesList;
  private DefaultListModel<String> rhymesListModel;

  public Rhymer() {
    wordBox = new JTextField();
    getButton = new JButton("Find Rhymes");
    rhymesListModel = new DefaultListModel<>();
    rhymesList = new JList<>(rhymesListModel);

    getButton.addActionListener(e -> getRhymes(wordBox.getText()));

    // Top panel
    JPanel topPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(topPanel, BoxLayout.X_AXIS);
    topPanel.setLayout(boxLayout);
    topPanel.add(wordBox);
    topPanel.add(getButton);

    // Fill the frame
    add(topPanel, BorderLayout.NORTH);
    add(new JScrollPane(rhymesList), BorderLayout.CENTER);

    setSize(512, 512);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }

  private void getRhymes(String word) {
    try {
      URL url = new URL("http://rhymebrain.com/talk?function=getRhymes&word=" + word);
      URLConnection connection = url.openConnection();
      connection.connect();

      Scanner in = new Scanner(connection.getInputStream());
      StringBuilder builder = new StringBuilder();
      while (in.hasNextLine()) {
        builder.append(in.nextLine());
      }
      in.close();
      
      Pattern pattern = Pattern.compile("\"word\"\\s*:\\s*\"(\\w*)\"");
      Matcher matcher = pattern.matcher(builder.toString());
      
      rhymesListModel.clear();
      while (matcher.find()) {
        String rhyme = matcher.group(1);
        rhymesListModel.addElement(rhyme);
      }
      
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static void main(String[] args) {
    new Rhymer();
  }
}

MSYahAIM.java

package lecture27;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class MSYahAIM {
  private static ArrayList<Client> clients = new ArrayList<>();
  
  public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(8181);
    while (true) {
      Socket clientSocket = serverSocket.accept();
      new Thread(new ClientListener(clientSocket)).start();
    }
    // socket.close();
  }
  
  private static class ClientListener implements Runnable {
    private Client client;
    
    public ClientListener(Socket socket) throws IOException {
      client = new Client(socket, new PrintWriter(socket.getOutputStream(), true), null);
      clients.add(client);
    }
    
    @Override
    public void run() {
      try {
        Scanner in = new Scanner(client.socket.getInputStream());
        
        while (in.hasNextLine()) {
          String line = in.nextLine();
          for (Client client : clients) {
            client.out.println(line);
          }
        }
        
        in.close();
        client.out.close();
        client.socket.close();
        
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  
  private static class Client {
    private Socket socket;
    private PrintWriter out;
    private String name;
    
    public Client(Socket socket, PrintWriter out, String name) {
      this.socket = socket;
      this.out = out;
      this.name = name;
    }
  }
}

Haiku

My first love chat bombed
She said: let's be... close, I said
Chris has left the room