Forums Register Login

sockets and printwriting

+Pie Number of slices to send: Send
Help, I'm slowly being driven mad trying to get round a problem.

I have a server and client that operate happily together by means of a simple protocol. Now I'm trying to get the server to write the clients reponses to a txt file, so I can do something with them later... However....The outfile is being generated, but its blank.


Any ideas?



Hamish
+Pie Number of slices to send: Send
Hi, welcome to the ranch!

Post a little bit of code that does the writing and maybe we can spot something. Use the "Code" button below the editor window to keep your code formatting and make it easier for us to read.
+Pie Number of slices to send: Send
Thanks for the reply Stan, thought it would be rude to just throw in loads of code without an introduction!! I've used the code button...
I guess the error is on the outfile printwriter, but can I see it? The file is created, but nothing is written.



import java.net.*;
import java.io.*;

public class libraryServer {
public static FileOutputStream outStream;
public static PrintWriter outFile;
public static void main(String[] args) throws IOException {


ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

outStream= new FileOutputStream("hamish.txt");
outFile = new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));

String inputLine, outputLine;
libraryProtocol kkp = new libraryProtocol();

outputLine = kkp.processInput(null);
outFile.println(outputLine);

while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
outFile.println(outputLine);
if (outputLine.equals("Bye."))
break;
}

in.close();
outFile.close();
clientSocket.close();
serverSocket.close();
}
}
+Pie Number of slices to send: Send
>Now I'm trying to get the server to write the clients reponses to a txt >file, so I can do something with them later... However....The outfile is >being generated, but its blank.

The only printwriter in your piece of code writes back to the client.
from your code,

outFile = new PrintWriter(clientSocket.getOutputStream(),true);



Create a printwriter that writes to a file.



RAM.
+Pie Number of slices to send: Send
Very nice!
I've got me a populated text file!

Thanks for your help Ramprasad


Hamish
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 971 times.
Similar Threads
Wireless Tool Kt & Proxy
Login to ASP page
servlet response
Retrieve user Information
FTP-Protocol: PORT question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:13:23.