Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sockets and printwriting

 
hamish cooper
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
hamish cooper
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice!
I've got me a populated text file!

Thanks for your help Ramprasad


Hamish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic