I have a problem with writing incoming information from a client to this program. The data comes in and there is output every second from the System.out, but the FileWriter only prints the first line of output from when the program was started. I stop the program manually, and check the file after. I'm unsure of what's wrong, please help.
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.net.*;
import java.io.*;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Server {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
String msg_received;
FileWriter fw = new FileWriter("HeartData.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
System.out.println("Waiting for Android client to connect...");
while (true)
{
try
{
ServerSocket server = new ServerSocket(2323);
Socket s = server.accept();
server.close();
InetAddress clientAddress = s.getInetAddress();
System.out.println("Incoming connection from: " + clientAddress.getHostName() + "[" + clientAddress.getHostAddress() + "]");
DataInputStream DIS = new DataInputStream(s.getInputStream());
msg_received = DIS.readUTF();
out.println(msg_received + "," + LocalTime.now() + "," + LocalDate.now());
System.out.printf("Android says: %sat %s%n", msg_received, LocalTime.now());
}
catch (IOException e){e.printStackTrace();}
finally {
out.close();
}
}
}
}
Example Output:
Waiting for Android client to connect...
Incoming connection from: hostname.domain[ipaddress]
Android says: SOMETHING
at 10:51:06.013
You have this structure:
while (true) {
try {
// Code which writes one line
} finallly {
out.close();
}
}
In other words, you're closing the output after the first line, but continuing to do work. That's not going to reopen the output...
You should really use a try-with-resource block for the whole thing, with the while loop entirely inside it, so that you don't close the writer until your whole loop has finished. (Admittedly at the moment it can only finish due to an exception... you might want to add some non-exceptional way of stopping the loop...)
Related
This question already has answers here:
unreachable statement after while [duplicate]
(2 answers)
Closed 1 year ago.
I am trying to close the DataOutputStream and DataInputStream but I am getting an unreachable code error which doesn't even allow me to run, here is my code:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MultipleClient {
public static void main(String[] args) throws IOException {
try {
Scanner scan = new Scanner(System.in);
InetAddress ip = InetAddress.getByName("localhost");
Socket socket = new Socket(ip, 4571);
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.println(inputFromServer.readUTF());
int choice = scan.nextInt();
if (choice == 1) {
outputToServer.writeInt(choice);
System.out.println(inputFromServer.readUTF());
}
}
}
scan.close();
inputFromServer.close();
outputToServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I guess I am getting that because Eclipse is detecting that this lines precede a while(true) which might be confusing however, I have had a similar code working fine, I tried comparing between both and I can't see the difference, here is the other code:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MultipleClient {
public static void main(String[] args) throws IOException {
try {
Scanner scan = new Scanner(System.in);
// Getting local IP Address (127.0.0.1)
InetAddress ip = InetAddress.getByName("localhost");
// Establish the connection with Server on port 5056
Socket socket = new Socket(ip, 5056);
// This will trigger the accept() function of the Server
// Receiving input and sending output to Server
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.println(inputFromServer.readUTF());
String tosend = scan.nextLine();
outputToServer.writeUTF(tosend);
// Sending Exit closes the connection and breaks the loop
if(tosend.equals("Exit"))
{
System.out.println("-----------------------------------------------------------------------------------");
System.out.println("Closing this connection : " + socket);
socket.close();
System.out.println("Connection closed");
break;
}
System.out.println("-----------------------------------------------------------------------------------");
// Printing message received from Server
String received = inputFromServer.readUTF();
System.out.println(received);
}
// Closing resources
scan.close();
inputFromServer.close();
outputToServer.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
You simply have a while(true) {...} block that doesn't even have a break statement so yeah those statements after it are unreachable ...
You have two choices:
Introduce a local variable initialized with true which will be updated inside the loop
Create a break statement like the second part of your uploaded code that will stop the loop when it meets a certain condition
I am attempting stream data over a socket with Java in an attempt to write a Kafka producer. I've written a class to pull the data in but I'm not getting the results I'd expect. I've got it set up so the data is being streamed from a Linux box. The source of the data is a csv file that I'm using the nc utility to stream. The class is running on a Windows 10 machine from Eclipse. When I run the class I see two weird things.
The column headers don't get transmitted.
I can only run the class once. If I want to run it again, I have to stop nc and restart it.
Below is my code. Am I missing anything? At this point I'm just trying to connect to the socket and pull the data over.
I run nc with the following command:
$ nc -kl 9999 < uber_data.csv
Below is my class
import java.net.*;
import java.io.*;
public class Client
{
static String userInput;
public static void main(String [] args)
{
try
{
InetAddress serverAddress = InetAddress.getByName("servername");
Socket socket = new Socket(serverAddress, 9999);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((userInput = input.readLine()) != null) {
System.out.println(input.readLine());
}
input.close();
socket.close();
}
catch(UnknownHostException e1)
{
System.out.println("Unknown host exception " + e1.toString());
}
catch(IOException e2)
{
System.out.println("IOException " + e2.toString());
}
catch(IllegalArgumentException e3)
{
System.out.println("Illegal Argument Exception " + e3.toString());
}
catch(Exception e4)
{
System.out.println("Other exceptions " + e4.toString());
}
}
}
You're throwing away every odd-numbered line. It should be:
while ((userInput = input.readLine()) != null) {
System.out.println(userInput);
}
Secondly, you aren't closing the socket. Use a try-with-resources:
try
{
InetAddress serverAddress = InetAddress.getByName("servername");
try (
Socket socket = new Socket(serverAddress, 9999);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
while ((userInput = input.readLine()) != null) {
System.out.println(input.readLine());
}
}
}
catch (...)
First, each call readLine() tries to read line from input stream.
In userInput = input.readLine() you read header, but println(input.readLine()) read body and print in console.
while ((userInput = input.readLine()) != null) {
System.out.println(userInput); //instead input.readLine()
}
Second, I didn't use nc, but I think problem will solve if you will close socket (and reader) in finally statement.
I hope it would be helpful.
For the first question: you were trying to print userInput string. But it's printing the result of another readline() call.
For the second: after the file has been transferred, you have to stop and restart nc; no matter what you do from your side. It's from nc side.
See the nc documentation.
I have a simple echo server, and I want when a connected user types anything to the server, all other clients and that client will get a the message + " | MOD".
It wont send to all clients now but it should and I just don't know what's wrong in my code, so now it will just send the message + " | MOD" to the client who sent the message but not to all others also as it should.
I just don't get it, I have a loop that goes through all clients, but it still won't send to all.
SERVER:
package com.murplyx.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
public static ServerSocket server;
public static ArrayList<Socket> clients = new ArrayList<Socket>();
public static void broadcast(String message) {
try {
for (Socket socket : clients) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
try {
server = new ServerSocket(9000);
while (true) {
clients.add(server.accept());
for (Socket socket : clients) {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
if (line != null) {
broadcast(line + " | MOD");
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
CLIENT:
package com.murplyx.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String args[]) {
try {
while (true) {
Socket socket = new Socket("localhost", 9000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
out.println(input.readLine());
System.out.println(in.readLine());
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please help.
Thanks alot.
One of the issues you have is that each client will repeatedly do read stdin, write socket, read socket, write stdout, ... ad infinitum.
When you broadcast all other clients are still typically sat in the read stdin phase, so they don't know that there's stuff waiting to be read on the socket. They're still waiting for the user to enter something.
One of the simplest options is to start two threads in each client - one just handles read stdin, write socket, ... and the other handles read socket, write stdout.
[Another (potentially more sophisticated) option us to use Java NIO to poll both the socket and stdin for available input at the same time].
A second issue is that you're blocking in the accept call in the server, and then reading from each socket in turn. You might accept in one thread, and have another thread per client read from just the client, and rebroadcast to the others. NIO can also be a good option here - you can poll for reads any any client.
I'm not exactly sure how ArrayLists play with sockets, so I definitely would go back to using a normal array for it (see the edited code here Java EchoTCPServer - Send to all clients)
Some things I see that I think can to be fixed:
On the Client:
-Stop closing the socket in the While loop. Close it OUTSIDE the while loop (When the client is done with the server). Also, declare the socket outside the Loop.
NOTE ON THIS: When a client makes a socket to connect to the server, it is automatically given a device port, so two different devices will never have the same IP connected to the server. A TCP connection consists of 2 ports, server socket and client socket, and the sockets are denoted by [deviceip:port,serverip:port] (iirc).
-Also, on the client you don't need to declare a new reader everytime you move through the while loop. Put that all outside. The only thing inside the while loop should be your readline + print statements.
-readLine is a blocking method. (just in case you don't know what that means, it means that readLine will make your program be stuck there until the it actually reads a line. To bypass this, you can use an if statement combined with the .ready() function. The ready function checks to see if there is anything to be "read in", so if there's no input it wont be stuck on "readLine".
On the Server:
-Like i said earlier, I'd change back to using a normal Array.
-Your server will still get stuck on .accept(). As such, you will never be able to read input from the clients except once after each connection. You can use a thread to listen instead, and it will still work.
eg: (this code goes with the code that's in the link i attached (also your question), put it before the while loop of your server)
// create a tcp listener thread to deal with listening to clients
Thread listenerThread = new Thread() {
public void run() {
String clientSentence;
while (true) {
//loop through each connected socket
for (int i = 0; i <= intLastSocket; i++) {
Socket z = clientSocket[i];
//make sure the socket is not null or closed (can't do anything
//with closed or null sockets
if ((z != null) && (!z.isClosed())) {
try {
// Deal with TCP input here
BufferedReader input = new BufferedReader(new
InputStreamReader(z.getInputStream()));
// read in a line but only if there is one
if (input.ready()) {
clientSentence = input.readLine();
}
} catch (IOException x) {
printTCP("IOException caught when reading in: "
+ x.toString());
}
if (clientSentence != null) {
System.out.println("Received from client: "
+ clientSentence);
//send this message to the client
outputStream[i].println(clientSentence + " | MOD");
}
// clear the input
clientSentence = null;
}
}
}
}
};
listenerThread.start();
I try to make a little Server-Client connection.
They both have a Scanner and a PrintWriter, and they are writing to each other using a Socket's input and output stream.
Client.java:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
static ServerSocket serverSocket;
static Socket socket;
static PrintWriter printWriter;
static Scanner scanner;
public static void main(String[] args) throws UnknownHostException, IOException {
socket = new Socket("localhost", 13344);
scanner = new Scanner(socket.getInputStream());
printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println("dataline 1");
printWriter.println("dataline 2");
printWriter.println("dataline 3");
printWriter.flush();
//Error!? => I never got the echo from server in output
while (scanner.hasNextLine()) {
String lineRead = scanner.nextLine();
System.out.println("From server" + lineRead);
}
socket.close();
scanner.close();
printWriter.close();
System.out.println("Client has quit.");
}
}
Server.java
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
static ServerSocket serverSocket;
static Socket socket;
static PrintWriter printWriter;
static Scanner scanner;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(13344);
System.out.println("Waiting for Client to connect");
socket = serverSocket.accept();
scanner = new Scanner(socket.getInputStream());
printWriter = new PrintWriter(socket.getOutputStream());
System.out.println("Client has connected!!");
while (scanner.hasNextLine()) {
String lineRead = scanner.nextLine();
System.out.println("From Client: " + lineRead);
}
//Error!? => This line never runs
System.out.println("Now sending echo to Client");
printWriter.println("Echo from server1");
printWriter.println("Echo from server2");
printWriter.flush();
socket.close();
printWriter.close();
scanner.close();
System.out.println("Server has quit.");
}
}
I start the server: java Server.java
I start the client: java Client.java
Server's output:
Waiting for client to connect
Client has connected!!
From Client: dataline 1
From Client: dataline 3
From Client: dataline 3
Client's output is empty, not a word on it.
As you can see Server's code termination stops AFTER it is read from Client's output stream, and Client's code termination stops BEFORE it could read from Server's output stream.
My question is:
How this Scanner-PrintWrier communication works, how do i know if a printWriter printed BEFORE a scanner could read in a server-client connection like this? What i did wrong and why? How to use properly a scanner?
E D I T:
#T.C Do you mean like this? Now i got full output, both Server and Client are quit after they are sent and received data.
I modified like this:
String line = "";
while (!line.equals("#")) {
line = scanner.nextLine();
if (!line.equals("#")) {
System.out.println("From server" + line);
}
}
The Scanner.hasNext*() methods will block to wait for input to scan, so you can't use it to detect when the client has finished sending.
Simplest way to solve this problem would be to have the client send a special string telling the server it's done sending.
I have already posted some samples on client-server communication with detailed description.
Please have a look at below post that might help you to understand it better.
Multiple clients access the server concurrently
Java Server with Multiclient communication.
Try with BufferedReader that contains ready() that tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
You can try with InputStream#available() but it doesn't have read line method.
Go with the solution suggested by #T.C. but he is not provided any sample code on it. You can find in it my post.
Had almost the exact same problem, after banging my head in the keyboard for a couple of hours, this is what saved me:
printWriter = new PrintWriter(socket.getOutputStream(), true);
The second parameter sets auto-flushing to true.
I wrote two small java programs: a TCP client which sends many lines of data from a txt file, and a TCP Server which accepts connection and receives line by line.
It works, but Server receives all the lines together, when transmission is over and client closes the socket. I don't understand why, because I'm using flush after each line sent into the Client, and I always thought that was to avoid this kind of situation.
If my test.txt file contents the numbers 1 2 3 ... 10, each one in a new line, Server's output is: "12345678910" and then in a new line it writes in console "null".
Server code:
import java.io.*;
import java.net.*;
class ServidorTCP {
private String HOST;
static final int PUERTO = 20001;
public ServidorTCP( ) {
try{
ServerSocket skServidor = new ServerSocket(PUERTO);
Socket skCliente = skServidor.accept();
DataInputStream inFromClient = new DataInputStream(new BufferedInputStream(skCliente.getInputStream()));
while(true){
String lineaLeida = inFromClient.readUTF();
if(lineaLeida.equals("END")) break;
System.out.println(lineaLeida);
}
inFromClient.close();
skCliente.close();
skServidor.close();
System.out.println( "Transmission ended" );
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}
public static void main( String[] arg ) {
new ServidorTCP();
}
}
Cliente code:
import java.io.*;
import java.net.*;
class ClienteTCP {
static final String HOST = "192.168.1.201";
static final int PUERTO = 20001;
public ClienteTCP( ) {
try{
Socket skCliente = new Socket(HOST, PUERTO);
if(skCliente.isConnected()) System.out.println("Connected!");
DataOutputStream outToServer = new DataOutputStream(skCliente.getOutputStream());
File testFile = new File("test.txt");
BufferedReader input = new BufferedReader(new FileReader(testFile));
String line;
while((line=input.readLine())!=null) {
if(!line.endsWith("\n")) line = line + "\n";
System.out.println("Sending: " + line);
outToServer.writeBytes(line);
outToServer.flush();
Thread.currentThread().sleep(1000);
}
outToServer.writeBytes("END");
input.close();
outToServer.close();
skCliente.close();
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}
public static void main( String[] arg ) {
new ClienteTCP();
}
}
Where is the problem?
EDIT: I have edited the code as suggested. Now doesn't receive anyhing
The fact of the matter is that your code doesn't work at all, let alone before or after the socket is closed. You are writing with writeUTF() and reading with readLine(). This does not work. You need to:
write with DataOutputStream.writeUTF() and read with DataInputStream.readUTF()
write with println() (supplied by several I/O classes), or write() or print() (supplied by several I/O classes), followed by BufferedWriter.newline(), or
write with ObjectOutputStream.writeObject() and read with ObjectInputStream.readObject().
Etc. Writing with one API and reading with a non-symmetrical API doesn't work.
The problem is not with reading or writing (or at least not explicitly). The problem is your string. input.readLine() removes the end of line which means the string you're sending is just the number. On the server side then you receive a lot of strings but inFromClient.readLine() waits until it gets an end of line which never arrives until the stream closes and sends an end of file.
Just add an end of line to every string you're sending and it should work.