I have spent a while reading other posts, and nobody seems to have the same case as me. I have tried all solutions I have read with no results, so I decided to create this question.
I have been working on a server/client application, and the client seems to not read data from the socket, but the server can read data the client sends. The client freezes while trying to read the line. Here's the code I have:
Client:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
public class Main {
public static Socket s = connect();
public static void sendMessage(String msg) {
if (s != null) {
PrintWriter outWriter = null;
try {
outWriter = new PrintWriter(s.getOutputStream(), true);
outWriter.println(msg);
outWriter.flush();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
outWriter.close();
} finally {
}
} else {
System.err.println("Error, socket is null!");
}
}
public static String readMessage() {
if (s != null) {
Scanner in = null;
try {
in = new Scanner(s.getInputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
String ss = "";
while (in.hasNext()) {
ss += in.next();
}
System.out.println("REPONSE:" + ss);
return ss;
} else {
System.err.println("Error, socket is null!");
}
return "err/readMessage";
}
public static Socket connect() {
try {
Socket sss = new Socket("localhost", 25586);
sss.setKeepAlive(true);
return sss;
} catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
return null;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
sendMessage("HELO");
System.out.println("Thread initialized.");
while (true) {
try {
System.out.println("Awaiting message...");
Thread.sleep(100);
String messages = readMessage();
System.out.println("Message recieved! '" + messages + "'");
String[] message = messages.split("/");
System.out.println(messages);
if (message[0].equalsIgnoreCase("DERP")) { // err/reason
System.out.println("IT'S FINALLY WORKING!");
} else {
System.out.println("Didn't work :( response:" + messages);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
});
t.start();
}
});
}
}
Server:
import java.util.List;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main { /* SERVER */
public static List<EchoThread> list = new ArrayList<>();
public static int PORT = 25586;
public static void main(String[] args) throws IOException {
new Main(PORT);
}
public Main(int port) throws IOException {
this.PORT = port;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
// new threa for a client
EchoThread s = new EchoThread(socket);
s.start();
list.add(s);
}
}
public static void sendMessageToAll(String ss) {
for (EchoThread s : list) {
s.allMessage(ss);
}
}
}
class EchoThread extends Thread {
protected Socket socket;
InputStream inp = null;
BufferedReader brinp = null;
public EchoThread(Socket clientSocket) {
this.socket = clientSocket;
try {
inp = socket.getInputStream();
brinp = new BufferedReader(new InputStreamReader(inp));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String line;
while (true) {
try {
line = brinp.readLine();
if ((line == null) || line.equalsIgnoreCase("EXIT")) {
socket.close();
return;
} else {
handle(line);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
public void handle(String s) {
String[] keys = s.split("/");
System.out.println("random: Handling request\"" + s + "\"");
System.out.println("Response: DERP");
if (s.equalsIgnoreCase("HELO")) {
System.out.println("Message recieved:" + s);
}
respond("DERP");
}
public void respond(String s) {
try {
System.out.println("Response_WILLBE:" + s);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println(s);
pw.flush();
System.out.println("Message sent!");
} catch (Exception ex) {
Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void allMessage(String s) {
respond(s);
}
}
I tried the flush()
code and \r\n
and println
fixes, but none worked!
-- Thank you for reading!