-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.java
More file actions
75 lines (70 loc) · 1.98 KB
/
EchoServer.java
File metadata and controls
75 lines (70 loc) · 1.98 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
64
65
66
67
68
69
70
71
72
73
74
75
import java.net.*;
import java.io.*;
public class EchoServer extends Thread
{
protected Socket clientSocket;
public static void main(String[] args)throws IOException
{
ServerSocket server = null;
try
{
server = new ServerSocket(10008);
System.out.println("Connection socket created");
try
{
while(true)
{
System.out.println("Waiting for connection");
new EchoServer(server.accept());
}
}
catch(IOException e)
{
System.err.println("Accept failed, ");
System.exit(1);
}
}
catch(IOException e)
{
System.err.println("Could not listen on port 10008");
System.exit(1);
}
finally
{
try
{
server.close();
}
catch(IOException e)
{
System.err.println("Could not close port : 10008");
System.exit(1);
}
}
}
private EchoServer(Socket clientsoc)
{
clientSocket = clientsoc;
start();
}
public void run()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String inputLine;
while((inputLine=in.readLine())!=null)
{
System.out.println("Server: " + inputLine);
if(inputLine.equals("STOP"))
break;
}
}
catch(IOException e)
{
System.err.println("Problem with server");
System.exit(1);
}
}
}