• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sockets and printwriting

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice!
I've got me a populated text file!

Thanks for your help Ramprasad


Hamish
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic