-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
63 lines (51 loc) · 1.83 KB
/
Client.java
File metadata and controls
63 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.net.Socket;
import java.time.LocalTime;
public class Client {
public final String host = "localhost";
public final int port = 5555;
public String messageFromUser;
public String messageFromServer;
BufferedReader inFromUser;
BufferedReader inFromServer;
PrintWriter outServer;
Socket clientSocket;
public void start() throws IOException{
clientSocket = new Socket(host,port); //server connection
inFromUser = new BufferedReader(new InputStreamReader(System.in));
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outServer = new PrintWriter(clientSocket.getOutputStream(), true);
Thread readThread = new Thread(new Reader());
readThread.start();
}
public void closeConn() throws IOException{
inFromServer.close();
inFromUser.close();
outServer.close();
clientSocket.close();
}
public class Reader implements Runnable{
public void run(){
try{
System.out.print("Enter what to send to server\n");
while ((messageFromUser = inFromUser.readLine()) !=null){
outServer.println(messageFromUser);
messageFromServer = inFromServer.readLine();
System.out.println(messageFromServer);
System.out.print("Enter what to send to server\n");
}
} catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String []argv) {
Client c1 = new Client();
System.out.print("Client started " + LocalTime.now() + "\n");
try {
c1.start();
} catch (IOException e) {
System.out.print("Connection lost");
}
}
}