I have to write server application that request questions from client and receives an answer. This is my client code:
clientSocket = new Socket("localhost", 1234);
System.err.println("Client started");
//get questions
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Question q = (Question)in.readObject();
//send answer
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.print("a1");
out.flush();
and server code:
//sending questions
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
List<Question> quest = Questions.getInstance().getQuestions();
out.writeObject(quest.get(0));
out.flush();
//get answer
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String temp = null;
while ((temp = in.readLine()) == null) {}
String answer = temp;
Questions successfully sent and later received by client, but server never get answer (infinite loop while reading temp variable). What is the problem?
Your calling out.print("a1"); on the client, but reading a line on the server using in.readLine(). Shouldn't you be writing out using println() on the client, else the server never gets to the end of the line? – CodeChimp Nov 21 at 21:07
Thanks for CodeChimp
Related
I have a Java code that I have to convert to C#. The code is very basic, includes streams and sockets.
Java Code:
Socket fg = new Socket("localhost", 5400);
BufferedReader in = new BufferedReader(new FileReader("reg_flight.csv"));
PrintWriter out = new PrintWriter(fg.getOutputStream());
String line;
while((line=in.readLine())!=null) {
out.println(line);
out.flush();
Thread.sleep(100);
}
out.close();
in.close();
fg.close();
I was able of course to convert the first line, but I'm having trouble finding in Google a way to output line by line via Sockets. Specifically: PrintWriter out = new PrintWriter(fg.getOutputStream());.
My C# start:
TcpClient s = new TcpClient("localhost", 5400);
NetworkStream stream = s.GetStream();
StreamReader input = new StreamReader("reg_flight.csv");
StreamWriter output = new StreamWriter(stream);
String line;
while ((line = input.ReadLine())!= null) {
Console.WriteLine(line);
output.Write(line);
output.Flush();
System.Threading.Thread.Sleep(100);
}
For some reason, this is not working (the server doesn't receive the data correctly). I've printed the line inside the while loop and the reading seems correct. What could be the reason?
Thanks
This client side is talking to a single threaded server. Now, what i'm trying to do is, if client#2 tries to connect to the socket while client#1 is already actively connected to the server side, to do something else. so for example,
if(socket1.gotrefused){
system.out.println("It got refused");
My code below, (It's working perfectly for me, i just want to add the above one way or another..)
Socket socket1;
int portNumber = 4445;
socket1 = new Socket(InetAddress.getLocalHost(), portNumber);
BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true);
pw.println("Hello");
pw.println("Hello");
pw.println("Hello");
pw.println("Hello");
String input = br.readLine();
while ((input = br.readLine()) != null) {
if(input.equals("Hi")){
pw.println("Hello");
}
else if(input.equals("Done")){
break;
}
br.close();
pw.close();
socket1.close();
}
So I want to read from socket , but it doesn't gives me anything , I am newbie to java networking so please help me , it doesn't gives me any errors but doesn't gives me any output from client socket too... here is a source code:
ServerSocket server = new ServerSocket(4444);
Socket client = server.accept();
PrintWriter out = new PrintWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String inputline = in.readLine();
while(inputline != null)
{
System.out.println("recieved "+inputline);
}
out.close();
in.close();
server.close();
client.close();
You call readLine() exactly once. If it's not null on the first iteration, you've got an infinite loop (because it will forever be non-null). Be sure to update it.
I have been looking through the multitudes of explanation of basic Java Socket use, and have constructed the following basic code for my own Server/Client echo pair. However, there is some hangup in the client code that I cannot find for the life of me. Perhaps someone else can spot it?
// Server Code:
try (ServerSocket serverSocket = new ServerSocket(22222);
Socket cSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(cSocket.getOutputStream());
BufferedReader in = new BufferedReader(
new InputStreamReader(cSocket.getInputStream()))) {
System.out.println("Client connected: " + cSocket.getInetAddress().getHostAddress());
// console DOES print ^this line and correct IP when client is run.
String inLine;
while (true) {
inLine = in.readLine();
out.println(inLine);
if (inLine.equals("exit")) break;
}
// client code
try (Socket socket = new Socket("localhost", 22222);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader consoleIn = new BufferedReader(new InputStreamReader(System.in));) {
String userIn;
while (true) {
System.out.print("Client> ");
userIn = consoleIn.readLine();
out.println(userIn); // code hangs here.
out.flush();
System.out.println("Server> " + in.readLine());
if (userIn.equals("exit")) break;
}
It isn't blocking there. It's blocking in the readLine() from the server. Try a flush() after the println() in the server.
I have a code like the one below
Server side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
String data = inFromNode.readLine().toString();
String data1 = inFromNode.readLine().toString();
String data2 = inFromNode.readLine().toString();
outToNode .println("Hi");
}
Client side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
outToNode .println("Hi");
outToNode .println("Hi");
outToNode .println("Hi");
String data = inFromNode.readLine().toString();
}
The problem is the client side code is waiting for the reply from the server. I am sure the server side has sent it(I tried placing logs after the send on the server side and they got printed.) Am I overlooking on something here? Is the code wrong in any way?
Try closing the PrintWriter and the Socket when writing to client finishes from Server. This should ideally fix your problem.