-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosprg.java
More file actions
135 lines (123 loc) · 3.7 KB
/
osprg.java
File metadata and controls
135 lines (123 loc) · 3.7 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
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
import java.io.*;
import java.net.*;
public class osprg
{
private static boolean HALT=false;
static class MessageServer
{
private ServerSocket socket;
public static int SERVER_PORT=3000;
public MessageServer()
{
}
public void start()
{
try
{
ServerSocket socket = new ServerSocket(SERVER_PORT);
int count=0;
String messages[] =
{
"Hello, I'm the server!",
"Are you surprised?",
"Do you think that's ridiculous?",
"Oh damn you client!",
"Stupid!",
"Bye STUPID!"
};
while(!HALT)
{
Socket connection = socket.accept();
PrintWriter writer = new PrintWriter(connection.getOutputStream(),true);
writer.println(messages[count]);
count=(count+1)%messages.length;
connection.close();
}
socket.close();
}
catch(IOException ioe)
{
System.err.println("Server Exception: "+ioe.toString());
}
catch(Exception exc)
{
}
finally
{
try {
socket.close();} catch(Exception exc){}
System.out.println("Server: Bye");
}
}
}
static class MessageClient
{
private Socket socket;
public MessageClient()
{
}
public void start()
{
System.out.println("The client has started: ");
String userinput="y";
try
{
while(userinput.toLowerCase().equals("y"))
{
System.out.println("Connecting to the Server: ");
socket = new Socket("127.0.0.1",MessageServer.SERVER_PORT);
InputStream in = socket.getInputStream();
BufferedReader rd = new BufferedReader( new InputStreamReader( in));
String line=null;
System.out.println("Server message: ");
while(!((line=rd.readLine())==null))
System.out.println(line);
socket.close();
System.out.print("Do you wish to continue (y/N) ?: ");
userinput = (new java.util.Scanner(System.in)).nextLine();
}
}
catch(IOException ioe)
{
System.err.println("Client Exception: "+ioe.toString());
}
catch(Exception exc)
{
}
finally
{
System.out.println("Client: Bye");
}
}
}
public static void main( String[] args)
{
Thread server_thread = new Thread()
{
@Override
public void run()
{
MessageServer server = new MessageServer();
server.start();
}
};
Thread client_thread = new Thread()
{
@Override
public void run()
{
MessageClient client = new MessageClient();
client.start();
}
};
server_thread.start();
client_thread.start();
try {
client_thread.join();
HALT=true;
server_thread.join();
} catch (InterruptedException e) {
System.err.println("Main Interrupted");
}
}
}