Skip to content
Snippets Groups Projects
FriscoTcpClient.java 2.26 KiB
package homework1;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;


import java.io.*;
import java.net.*;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author djfrisco
 */
public class FriscoTcpClient {

    public static void main(String[] args) 
    {
        try
        {
           System.out.println("creating socket");
           
           // We request an IP to connect to ("localhost") and
           // port number at that IP (2317). This establishes
           // a connection to that IP in the form of the Socket
           // object; the server uses a ServerSocket to wait for
           // connections.
           Socket socket = new Socket("localhost", 2317); 
           
           // Read the single line written by the server. We'd
           // do things a bit differently if many lines to be read
           // from the server, instead of one only.
           InputStream is = socket.getInputStream();
           InputStreamReader isr = new InputStreamReader(is);
           BufferedReader br = new BufferedReader(isr);
           
           String serverMessage = br.readLine();
           System.out.println("1 The message the server sent was " + serverMessage);
           
           // Outgoing Messages
           OutputStream os = socket.getOutputStream();
           PrintStream ps = new PrintStream(os);
           ps.println("bbb report: X, Y, X " + System.currentTimeMillis());
           
           serverMessage = br.readLine();
           System.out.println("2 The message the server sent was " + serverMessage);
           
           //os = socket.getOutputStream();
          // ps = new PrintStream(os);
           ps.println("Final message from client " + System.currentTimeMillis());
           serverMessage = br.readLine();
           System.out.println("3 The message the server sent was " + serverMessage);
            
           
        }
        catch(Exception e)
        {
            System.out.println(e);
            System.out.println("Problem with client");
        }

    }
}