Here is my server app
public static void main(String[] args) throws IOException, ISOException {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Server siap menerima koneksi pada port ["+PORT+"]");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
InputStreamReader inStreamReader = new InputStreamReader(socket.getInputStream());
PrintWriter sendMsg = new PrintWriter(socket.getOutputStream());
int data;
StringBuffer sb = new StringBuffer();
int counter = 0;
int lengthOfMsg = 4;
while((data = inStreamReader.read()) != 0) {
counter++;
sb.append((char) data);
if (counter == 4) lengthOfMsg += Integer.valueOf(sb.toString());
if (lengthOfMsg == sb.toString().length()) {
System.out.println("Rec. Msg ["+sb.toString()+"] len ["+sb.toString().length()+"]");
processingMsg(sb.toString(), sendMsg);
}
}
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
}
}
the problem is if the client doesn't close the connection, the server app can't receive another request form another client
and if I do closing the connection from server side, it won't work with the client's apps.
The point is in this server or client app closing the connection can't be done
What I want is: is there any way so the server can keep receive some request from client, even though another request not yet finished. so my server app can receive multi request at the same time without closing any connection.
Please help me,
nb: client's apps is from my third party vendor, so I have to follow their rules
I solved this by creating a runnable thread. This keeps your server alive up until you close it and whether the client disconnects. Try this...
public static void main(String[] args) throws IOException, ISOException {
final ServerSocket serverSocket = null;
Socket socket = null;
new Thread(new Runnable(){
@Override
public void run(){
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Server siap menerima koneksi pada port ["+PORT+"]");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
InputStreamReader inStreamReader = new InputStreamReader(socket.getInputStream());
PrintWriter sendMsg = new PrintWriter(socket.getOutputStream());
int data;
StringBuffer sb = new StringBuffer();
int counter = 0;
int lengthOfMsg = 4;
while((data = inStreamReader.read()) != 0) {
counter++;
sb.append((char) data);
if (counter == 4) lengthOfMsg += Integer.valueOf(sb.toString());
if (lengthOfMsg == sb.toString().length()) {
System.out.println("Rec. Msg ["+sb.toString()+"] len ["+sb.toString().length()+"]");
processingMsg(sb.toString(), sendMsg);
}
}
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
}
}
}).start();
}
If this doesn't work for you, you can refer to my code.
String ip = "";
try(final DatagramSocket socket = new DatagramSocket()){
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
ip = socket.getLocalAddress().getHostAddress();
}
catch (Exception e){
e.printStackTrace();
}
txt.append("SERVER STARTED\n");
txt.append("Server IP Address: " + ip + "\n\n");
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
Server.this.txt.append("Waiting for file...\n");
final ServerSocket server = new ServerSocket(8998);
server.close();
server = new ServerSocket(8998);
Server.this.socket = server.accept();
final Date date = new Date();
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
final String curDate = dateFormat.format(date);
final BufferedReader br = new BufferedReader(new InputStreamReader(Server.this.socket.getInputStream()));
final PrintWriter pw = new PrintWriter(new FileWriter(Server."C:\\Test\\File-" + curDate + ".csv"));
try {
String line;
for (line = br.readLine(); line != null; line = br.readLine()) {
wordsarray = line.split("\t");
pw.println(line);
}
pw.flush();
pw.close();
}
catch (IOException ex) {
System.out.printf("Can't write to file. ", ex);
}
finally {
if (Server.this.socket != null) {
br.close();
pw.close();
server.close();
Server.this.txt.append("\nAutomatically Saved to " + "C:\\PDT\\DataScan\\PurchaseOrder-" + curDate + ".csv" + "\n\n");
System.out.printf("Success! ");
}
}
}
}
catch (Exception e) {
System.out.printf("Something's Wrong! \n" + e);
JOptionPane.showMessageDialog(Server.this, e + "\n\nThe server needs to restart.", "Something's Wrong!", 1);
dispose();
main(null);
}
}
}).start();
It stays alive up until I close the server or the application and also handles multiple connections/requests.