I am developing Client-Server App using Java Socket. My Application Has Server which will listen on Port
- Client will connect to that port
- Receive Data From Client
- Send Data To Client
Part of My Code
public void run() {
System.out.println("Got a client !");
try {
// Get Data From Client
int red = -1;
byte[] buffer = new byte[5 * 1024]; // a read buffer of 5KiB
byte[] redData;
StringBuilder clientData = new StringBuilder();
String redDataText;
while ((red = clientSocket.getInputStream().read(buffer)) > -1) {
/* Get Data From Client Here Code Hidden */
System.out.println("Data From Client :"
+ clientData.toString());
OutputStream out = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
String sDataToClient = "TEST DATA TO SEND IN BYTE ARRAY";
byte[] b = sDataToClient.getBytes("UTF-8");
byte[] bClientSend = new byte[b.length + 2];
bClientSend[0] = (byte) 1;
bClientSend[1] = (byte) 79;
System.arraycopy(b, 0, bClientSend, 2, b.length);
dos.write(bClientSend);
System.out.println(Arrays.toString(bClientSend));
}
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I Get java.net.SocketException: Connection reset
after Data SENT to Client at following line
while ((red = clientSocket.getInputStream().read(buffer)) > -1) {
I am able to see Array Contents of System.out.println(Arrays.toString(bClientSend));
Then error occurs