-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
51 lines (34 loc) · 1.14 KB
/
Server.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.net.*;
public class Server {
public void run() throws IOException {
int port =8010;
ServerSocket socket = new ServerSocket(port);
socket.setSoTimeout(1000000);
while(true){
try{
System.out.println( "serve is listening on port "+ port);
Socket acceptedConnection=socket.accept();
System.out.println("connection accepted from client"+acceptedConnection.getRemoteSocketAddress());
PrintWriter toClient = new PrintWriter(acceptedConnection.getOutputStream(),true);
BufferedReader fromClient=new BufferedReader(new InputStreamReader(acceptedConnection.getInputStream()));
String clientMessage = fromClient.readLine();
System.out.println("Client says: " + clientMessage);
toClient.println("Hello Client how do u do ");
toClient.close();
fromClient.close();
acceptedConnection.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
Server server = new Server();
try{
server.run();
}catch(Exception ex){
ex.printStackTrace();
}
}
}