Reputation: 1046
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?
Upvotes: 5
Views: 3303
Reputation: 4164
Extend your ClientHandler and overwrite the constructor to include code for handling file transfers. Have two ports open, one for text and another for file transfers.
private static PrintWriter output;
public ClientHandler(Socket socket) throws IOException
{
client = socket;
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
}
private static ObjectOutputStream outStream;
public ClientFileHandler(Socket socket) extends ClientHandler throws IOException
{
client = socket;
outStream = new ObjectOutputStream(client.getOutputStream());
}
Upvotes: 0
Reputation: 35011
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
Upvotes: 4
Reputation: 14705
ObjectOutputStream
should only be used as an ObjectOutputStream
on that channel. Use the PrintWriter
on another socket if you really need it.
Upvotes: 0