-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
72 lines (61 loc) · 1.54 KB
/
Server.java
File metadata and controls
72 lines (61 loc) · 1.54 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
//server creation practice
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
public class Server{
//server socket
private ServerSocket listener;
//client Socket
private Socket client;
//create instance of the server
public Server(){
try{
listener = new ServerSocket(12345, 10);
}
catch(IOException ioe){
System.out.println("IO Exception: " + ioe.getMessage());
}
}
public void listen(){
try{
System.out.println("Server is listening");
client = listener.accept();
System.out.println("Now about to process the client");
processClient();
}
catch(IOException ioe){
System.out.println("IO Exception " + ioe.getMessage());
}
}
public void processClient(){
//communicate with the CLIENT
//initiate channels
try{
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
out.flush();
ObjectInputStream input = new ObjectInputStream(client.getInputStream());
//Communicate
String msg = (String)input.readObject();
System.out.println("Message from client " + msg);
out.writeObject("Hellow " + msg);
out.flush();
//close connection
out.close();
input.close();
client.close();
}
catch(IOException ioe){
System.out.println("IO Exception: " + ioe.getMessage());
}
catch (ClassNotFoundException cnfe)
{
System.out.println("Class not found: " + cnfe.getMessage());
}
}
public static void main(String[] args){
//start SERVER
Server server = new Server();
server.listen();
}
}