This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes sockets and printwriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "sockets and printwriting" Watch "sockets and printwriting" New topic
Author

sockets and printwriting

hamish cooper
Greenhorn

Joined: Feb 27, 2005
Posts: 4
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
hamish cooper
Greenhorn

Joined: Feb 27, 2005
Posts: 4
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();
}
}
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

>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.
hamish cooper
Greenhorn

Joined: Feb 27, 2005
Posts: 4
Very nice!
I've got me a populated text file!

Thanks for your help Ramprasad


Hamish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sockets and printwriting