Created
April 2, 2017 12:35
-
-
Save markusrenepae/9aaee52f9748c4419b0244d69b012afa to your computer and use it in GitHub Desktop.
Klient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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