Skip to content

Instantly share code, notes, and snippets.

@markusrenepae
Created April 2, 2017 12:35
Show Gist options
  • Save markusrenepae/9aaee52f9748c4419b0244d69b012afa to your computer and use it in GitHub Desktop.
Save markusrenepae/9aaee52f9748c4419b0244d69b012afa to your computer and use it in GitHub Desktop.
Klient
package CRS;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.Scanner;
/**
* Created by Pae on 3/10/2017.
*/
public class Client {
private static boolean isConnected = true;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print("Sisesta oma nimi: ");
final String name = sc.nextLine();
System.out.println("Ühendatud!");
Socket sokk = new Socket("localhost", 1337);
try (DataOutputStream out = new DataOutputStream(sokk.getOutputStream());
DataInputStream in = new DataInputStream(sokk.getInputStream())) {
while (sc.hasNextLine()&&isConnected){
String message = sc.nextLine();
if (Objects.equals(message, "exit")){
disconnect();
out.writeUTF("Client has left");
} else {
out.writeUTF("["+name+" "+ ZonedDateTime.now().toLocalTime().truncatedTo(ChronoUnit.SECONDS)+"] "+message);
}
}
}
}
private static void disconnect(){
isConnected=false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment