-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.java
67 lines (46 loc) · 1.67 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
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
String line = null;
String inp = IO.readString(); //Client
String server = IO.readString(); //host
int port = IO.readInt(); //port
BufferedReader userdata = new BufferedReader(new InputStreamReader(System.in));
try {
//System.out.print("input is :" + userdata);
// String server = "localhost";
// int port = 12345;
Socket socket = new Socket(server , port);
/* ÐThe methods getInputStream() and getOutputStream() return the
* basic streams for the socket
* ÐCreate a DataOutputStream for the socket so we can write a
* string as bytes
* ÐCreate a BufferedReader so we can read a line of results
* from the server */
DataOutputStream toServer = new DataOutputStream(socket.getOutputStream() );
BufferedReader fromServer = new BufferedReader(
new InputStreamReader ( socket.getInputStream()));
while(true){
//While starts
System.out.println("Type the input");
line = userdata.readLine();
toServer.writeBytes(line + '\n'); //// send the line we read from the user
String result = fromServer.readLine(); //// read the response from the server
// System.out.println("(TEST) data recieved from the server ");//test
System.out.println("=> " + result);
if (result.charAt(0) == '~')
break;
}// end of while loop
System.out.println("conncetion is terminated");
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}