Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
260 views3 pages

Serversideclientio

This class represents the handling of client input/output on the server side. It contains methods to receive data from a client socket, send data to the client, and close the connection. When data is received, it is broadcast to all other clients after possible processing depending on the data type. The run method contains the main receive/send loop that runs until the connection is closed.

Uploaded by

api-537257222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views3 pages

Serversideclientio

This class represents the handling of client input/output on the server side. It contains methods to receive data from a client socket, send data to the client, and close the connection. When data is received, it is broadcast to all other clients after possible processing depending on the data type. The run method contains the main receive/send loop that runs until the connection is closed.

Uploaded by

api-537257222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ServerSideClientIO.

java
1 package main;
2
3 import java.io.IOException;
13
14 public class ServerSideClientIO implements Runnable {
15
16 private boolean closeConnection;
17 private ClypeData dataToReceiveFromClient, dataToSendToClient;
18 private ObjectInputStream inFromClient;
19 private ObjectOutputStream outToClient;
20 private ClypeServer server;
21 private Socket clientSocket;
22
23 public ServerSideClientIO(ClypeServer server, Socket clientSocket) {
24 this.server = server;
25 this.clientSocket = clientSocket;
26 closeConnection = false;
27 this.inFromClient = null;
28 this.outToClient = null;
29 this.dataToReceiveFromClient = null;
30 this.dataToSendToClient = null;
31 }
32
33 /**
34 * Receives data
35 */
36 public void receiveData() {
37 try {
38
39 dataToReceiveFromClient = (ClypeData) inFromClient.readObject();
40
41 if(dataToReceiveFromClient == null)
42 return;
43
44 if(dataToReceiveFromClient.getType() == ClypeData.ALLUSERS) {
45 dataToReceiveFromClient = new MessageClypeData
(dataToReceiveFromClient.getUserName(), this.server.getAllUsers(), ClypeData.ALLUSERS,
dataToReceiveFromClient.getClientID());
46 }
47
48 if(dataToReceiveFromClient.getType() == ClypeData.LOGOUT) {
49 this.server.remove(this);
50 this.closeConnection = true;
51 dataToReceiveFromClient = new MessageClypeData
(dataToReceiveFromClient.getUserName(), this.server.getAllUsers1(), ClypeData.LOGOUT,
dataToReceiveFromClient.getClientID());
52 }
53
54 if(dataToReceiveFromClient.getType() == ClypeData.GET_ID) {
55 int kid = this.server.getNextID();
56 dataToReceiveFromClient = new MessageClypeData
(dataToReceiveFromClient.getUserName(), "" + kid, ClypeData.GET_ID, kid);
57 }
58
59 if(dataToReceiveFromClient.getType() == ClypeData.GET_CONNECTED) {
60 dataToReceiveFromClient = new MessageClypeData
(dataToReceiveFromClient.getUserName(), "" + this.server.getAllUsers1(),
ClypeData.GET_CONNECTED, dataToReceiveFromClient.getClientID());
61 }
ServerSideClientIO.java
62 } catch (ClassNotFoundException | IOException e) {
63 // TODO Auto-generated catch block
64 System.err.println(e.getMessage());
65 closeConnection = true;
66 }
67 }
68
69 public synchronized boolean isClosed() {
70 return closeConnection;
71 }
72
73 /**
74 * Sends data
75 */
76 public void sendData() {
77 try {
78 outToClient.writeObject(dataToSendToClient);
79 } catch (IOException e) {
80 // TODO Auto-generated catch block
81 System.err.println("ERROR: " + e.getMessage());
82 closeConnection = true;
83 }
84 }
85
86 /**
87 * @param dataToSendToClient data to send to client
88 */
89 public void setDataToSendToClient(ClypeData dataToSendToClient) {
90 this.dataToSendToClient = dataToSendToClient;
91 }
92
93 public String getUsername() {
94 return this.dataToReceiveFromClient.getUserName();
95 }
96
97 public int getClientid() {
98 return this.dataToReceiveFromClient.getClientID();
99 }
100
101 @Override
102 public void run() {
103 // TODO Auto-generated method stub
104
105 try {
106
107 outToClient = new ObjectOutputStream(clientSocket.getOutputStream());
108 inFromClient = new ObjectInputStream(clientSocket.getInputStream());
109
110 while(!closeConnection) {
111 receiveData();
112 dataToSendToClient = dataToReceiveFromClient;
113 this.server.broadcast(dataToSendToClient);
114 }
115
116 inFromClient.close();
117 outToClient.close();
118 clientSocket.close();
119 } catch (UnknownHostException e) {
120 System.err.println("Unknown Host: " + e.getMessage());
ServerSideClientIO.java
121 } catch (NoRouteToHostException e) {
122 System.err.println("Server unreachable " + e.getMessage());
123 } catch (ConnectException e) {
124 System.err.println("Connection refused: " + e.getMessage());
125 } catch (IOException e) {
126 System.err.println(e.getMessage());
127 }
128
129
130 }
131
132 }
133

You might also like