Java, EoT symbol gets saved in txt file when copying (Java, TCP, socket)

409 Views Asked by At

Hello I wrote a programm that sends a TCP protocol to a server or can be used as a server to receive protocols.

This is the transmitter method:

public void tell(String msg, Actor sender) {
    InetAddress addr;
    ServerSocket svSocket;
    try {
        addr = InetAddress.getLocalHost();
        Socket sock = new Socket(addr, port);
        PrintStream ps = new PrintStream(sock.getOutputStream());
        ps.println(msg);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And this is the receiver method:

public void run() {
    Printer printer = new Printer();
    ServerSocket svSocket;
    try {
        svSocket = new ServerSocket(this.port);
        Socket sock = new Socket();
        String message = "";
        while(!message.equals("\u0004")){
            sock = svSocket.accept();
            InputStreamReader reader = new InputStreamReader(sock.getInputStream());
            BufferedReader br = new BufferedReader(reader);
            message = br.readLine();
            printer.tell(message, null);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

It works to send simple messages and have them print out but as soon as I want to send files via. command line:

Start jar, create server with port 5555, localhost as address, copy.txt the file to create.

java -jar foo.jar -l 5555 > copy.txt

Start jar, create client with target port 5555, localhost as address, myFile.txt the file to send.

 java -jar foo.jar localhost 5555 < myFile.txt

Everything works, but it saves an EoT symbol at the very end of the file. It doesn't bother too much in txts but it corrupts images.

How can I solve this issue ?

Thanks!

0

There are 0 best solutions below