-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
90 lines (78 loc) · 1.88 KB
/
Client.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.lang.*;
public class Client
{
private String message, stdin , ip;
private int port;
public static boolean ask;
public static void Needask() {
ask = true;
}
public Client()
{
try
{
ask = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Remote IP: ");
ip = br.readLine();
System.out.print("And Port:");
port = Integer.parseInt(br.readLine());
Socket con = new Socket(ip,port);
if(con.isConnected())
{
System.out.println("Connect Success");
System.out.println("Input your user name: ");
String name = br.readLine();
Thread thread = new Thread(new ClientTalk(con));
thread.start();
DataOutputStream dos = new DataOutputStream(con.getOutputStream());
while(true){
stdin = br.readLine();
if(stdin.equals("Quit")) {
br.close();
con.close();
dos.close();
System.out.println("You have just left");
break;
}
dos.writeUTF(name + " : " + stdin);
}
}
else
System.out.println("Connect fails");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
class ClientTalk implements Runnable
{
DataInputStream dis;
Socket clientSocket;
public ClientTalk(Socket clientSocket)
{
this.clientSocket = clientSocket;
}
public void run()
{
try
{
dis = new DataInputStream(clientSocket.getInputStream());
while (true)
{
String read = dis.readUTF();
Client.Needask();
System.out.println(read);
}
}
catch(IOException e)
{
//System.out.println(e.toString());
}
}
}