-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocClient.java
More file actions
58 lines (50 loc) · 1.44 KB
/
Copy pathSocClient.java
File metadata and controls
58 lines (50 loc) · 1.44 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
package SocketProgramming;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class SocClient extends Thread {
int port=9999;
Socket s;
{
try {
s = new Socket("localhost",port);
} catch (IOException e) {
}
}
@Override
public void run() {
try{
BufferedReader r=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
read(r);
write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
private void write(PrintWriter out)throws Exception {
while (true) {
out.write(new Scanner(System.in).next());
out.flush();
if(new Scanner(System.in).next().equalsIgnoreCase("exit")){
s.close();
}
}
}
private void read(BufferedReader r) throws IOException {
while (true) {
String lines = r.readLine();
System.out.println("Server : " + lines);
if (lines.equalsIgnoreCase("exit")) {
System.out.println("Server terminated");
s.close();
break;
}
}
}
public static void main(String[] args) throws Exception {
SocClient c=new SocClient();
System.out.println("Client started");
c.start();
}
}