Skip to content
Snippets Groups Projects
LandasClient2.java 1.84 KiB
/*
 * 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.
 */
package mv3500_assignments;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
/**
 *
 * @author Rico
 */
public class LandasClient2 {
    
    public static DataInputStream in;
    public static DataOutputStream out;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
            //declare a scanner so we can write a message
       Scanner keyboard = new Scanner(System.in);

       // localhost ip
       String ip = "127.0.0.1";
       int port = 2317;
       Socket socket;

       try {

       //connect
       socket = new Socket(ip, port);

       //initialize streams
       //DataInputStream in = new DataInputStream(socket.getInputStream());
       //DataOutputStream out = new DataOutputStream(socket.getOutputStream());
       in = new DataInputStream(socket.getInputStream());
       out = new DataOutputStream(socket.getOutputStream());


        while (true){
           System.out.print("\nMessage to server: ");
           //Write a message
           String message = keyboard.nextLine();
           //Send it to the server which will print it and send a confirmation
           out.writeUTF(message);
           
           // recieve the incoming messages and print
           String message2 = in.readUTF();
           System.out.println(message2);
        }

        }
        catch(IOException e) {
            System.out.println(e);
            System.out.println("\nProblem with client");
        }
    }
    
}