- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathMyServer1.java
33 lines (31 loc) · 1.16 KB
/
MyServer1.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
importjava.net.*;
importjava.io.*;
publicclassMyServer1 {
publicstaticvoidmain(String[] args) throwsException {
// open socket and bind it 3333
ServerSocketss = newServerSocket(3333);
// listens until a client asked for a connection
Sockets = ss.accept();
DataInputStreamdin = newDataInputStream(s.getInputStream());
DataOutputStreamdout = newDataOutputStream(s.getOutputStream());
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
Stringstr = "", str2 = "";
// read until client says stop
while (!str.equals("stop")) {
str = din.readUTF();
System.out.println("clients says: " + str);
str2 = br.readLine();
dout.writeUTF(str2);
/*
* The java.io.Writer.flush() method flushes the stream. If the stream has saved
* any characters from
* the various write() methods in a buffer, write them immediately to their
* intended destination.
*/
dout.flush();
}
din.close();
s.close();
ss.close();
}
}