Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectOutputStream and PrintWriter Conflict

Tags:

java

I have server and client set up, which is basically a basic text email system. I am currently using a PrintWriter to send the text between the server and client. I am trying to create a attachment based system and to do this I am using a ObjectOutputStream.

private static PrintWriter output;
private static ObjectOutputStream outStream;

public ClientHandler(Socket socket) throws IOException
{
    client = socket;
    outStream = new ObjectOutputStream(client.getOutputStream());
    input = new Scanner(client.getInputStream());
    output = new PrintWriter(client.getOutputStream(), true);
}

I currently have the problem where if I try send text via the output printwriter, for some reason extra characters will be added to the beginning of the text that is sent, meaning the program cannot identify key words being passed via the printwriter to the client. The problem will stop if i comment out the creation of the outStream object.

Can anyone give me any advice to try solve this problem of conflict?

like image 911
Jono_2007 Avatar asked Oct 09 '22 03:10

Jono_2007


1 Answers

This extra text is coming from the object output stream.

Attaching an ObjectOutputStream AND a PrintStream to the same outputstream is basically just never going to work. You have to come up with a solution for using 1 or the other. To use just a PrintStream, you might consider converting your object(s) to JSON or XML. On the other hand, you could just use an ObjectOutputStream and write your strings to the ObjectOutputStream

like image 134
ControlAltDel Avatar answered Oct 12 '22 20:10

ControlAltDel