-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
144 lines (131 loc) · 4.11 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Client {
/**
* @Vikas M C
*/
//To send the name of the User to cahtApp.
public String SendName(String name, String option) {
String out = null;
try {
URL url = new URL("http://localhost:4444");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("PUT");
con.setRequestProperty("User-Agent", "Chat Server");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = option + ":" + name;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
inputLine = in.readLine();
out = inputLine;
in.close();
con.disconnect();
} catch (FileNotFoundException e) {
return "Error in the chat";
} catch (IOException ex) {
return "Error in the IO";
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
//To get the message from other Clients.
public String GetMessage(String name) {
String out = null;
try {
URL url = new URL("http://localhost:4444");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("name", name);
con.setRequestProperty("User-Agent", "Chat Server");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
inputLine = in.readLine();
out = inputLine;
in.close();
con.disconnect();
} catch (FileNotFoundException e) {
return "Something gone wrong";
} catch (IOException ex) {
return "Something gone wrong in IO exception";
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
//To send the message to other Clients.
public String SendMessage(String tao) {
String out = null;
try {
URL url = new URL("http://localhost:4444");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Chat Server");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = tao;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
inputLine = in.readLine();
out = inputLine;
in.close();
con.disconnect();
} catch (FileNotFoundException e) {
return "Could not find Client to chat";
} catch (IOException ex) {
return "Could not find Client to chat";
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
//To exit from the chatApp or Talk to other clients.
public String Delete(String name, String option) {
String out = null;
try {
URL url = new URL("http://localhost:4444");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE");
con.setRequestProperty("name", name);
con.setRequestProperty("option", option);
con.setRequestProperty("User-Agent", "Chat Server");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("name", "vikas");
con.connect();
int responseCode = con.getResponseCode();
con.disconnect();
} catch (FileNotFoundException e) {
return "Could not find Client to chat";
} catch (IOException ex) {
return "Could not find Client to chat";
} catch (Exception e) {
e.printStackTrace();
}
return out;
}
}