It's a simple client/server based ping/pong program. Unfortunately, IT doesn't work and displays this error message:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
It stops at the CLIENT TASK 30 line, in practise the client doesn't read what the server has sent. Here the code:
SERVER
package serverClient;
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class Server {
public static void main(String[]args){
ExecutorService esp= Executors.newFixedThreadPool(50);
try(ServerSocket ss= new ServerSocket(1027)){
while(true){
try{
Socket s=ss.accept();
Callable<Void> task=new ServerTask(s);
esp.submit(task);
}
catch(BindException be){}
catch(ConnectException ce){}
catch(NoRouteToHostException nrthe){}
catch(IOException ioe){ioe.printStackTrace();}
}
}
catch(Exception e){e.printStackTrace();}
}
}
SERVER TASK
package serverClient;
import java.util.concurrent.*;
import java.net.*;
import java.io.*;
public class ServerTask implements Callable <Void> {
Socket s;
ServerTask(Socket s){
this.s=s;
}
public Void call(){
BufferedWriter writer=null;
BufferedReader reader=null;
try{
reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
writer=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
int i=0;
StringBuilder sb=new StringBuilder();
while((i=reader.read())!=-1){
sb.append((char)i);
}
System.out.println("The client sends: "+sb);
writer.write("pong");
writer.flush();
}
catch(IOException ioe){ioe.printStackTrace();}
finally{
try {
writer.close();
}
catch (IOException ioe) {ioe.printStackTrace();}
if(reader!=null){
try{
reader.close();
}
catch(IOException ioe){ioe.printStackTrace();}
}
try{
s.close();
}
catch(IOException ioe){ioe.printStackTrace();}
}
return null;
}
}
CLIENT
package serverClient;
import java.io.IOException;
import java.net.*;
import java.util.concurrent.*;
public class Client {
public static void main(String[] args) {
ExecutorService es= Executors.newSingleThreadExecutor();
try {
Socket s= new Socket(InetAddress.getLocalHost(),1027);
try {
s.setSoTimeout(50000);
}
catch(SocketException se){se.printStackTrace();}
Callable<Void> task=new ClientTask(s);
es.submit(task);
}
catch (UnknownHostException uhe) {uhe.printStackTrace();}
catch (IOException ioe) {ioe.printStackTrace();}
}
}
CLIENT TASK
package serverClient;
import java.util.concurrent.*;
import java.net.*;
import java.io.*;
public class ClientTask implements Callable <Void>{
Socket s;
ClientTask(Socket s){
this.s=s;
}
public Void call(){
BufferedWriter writer=null;
BufferedReader reader=null;
try{
writer=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
writer.write("ping");
writer.flush();
int i=0;
StringBuilder sb=new StringBuilder();
while((i=reader.read())!=-1){
System.out.println("I'm reading.");
sb.append((char)i);
}
System.out.println("The server sends: "+sb);
}
catch(IOException ioe){ ioe.printStackTrace();}
finally{
try {
writer.close();
}
catch (IOException ioe) {ioe.printStackTrace();}
if(reader!=null){
try{
reader.close();
}
catch(IOException ioe){ioe.printStackTrace();}
}
try{
s.close();
}
catch(IOException ioe){ioe.printStackTrace();}
}
return null;
}
}