-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwowayServer.java
More file actions
28 lines (25 loc) · 887 Bytes
/
Copy pathTwowayServer.java
File metadata and controls
28 lines (25 loc) · 887 Bytes
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
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TwowayServer {
public static void main(String[] args) {
try {
System.out.println("S: Server is started");
ServerSocket ssoc = new ServerSocket(6666);
System.out.println("S: Server is waiting for client request");
Socket soc = ssoc.accept(); // establishing the connection
System.out.println("S: Client is connected");
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
String str = (String) ois.readUTF();
System.out.println("S: Client message: " + str);
String newStr = "Hello Client";
ObjectOutputStream oos = new ObjectOutputStream(soc.getOutputStream());
oos.writeUTF(newStr);
oos.flush();
ssoc.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}