• 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

Send a String to Server and server send it somewhere on network

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Produce an application that sends a string to a server specifying a filename, and the server opens that file and reads it. The file name and it contents are then sent to a client residing �somewhere� on the network.
Above is the problem specification. Below is the list of code of two java files and what they are doing is: Client asking the file to be transfered to the server (e.g. H:/raheel.txt) and then server is reading the contents of the file and then again server is requesting for the output i.e. where to save file and again client is giving the path (e.g. H:/temp.txt). What i really don't understand is with the last sentence of the problem specification i.e. "The file name and it contents are then sent to a client residing �somewhere� on the network" and how it will work out. I would really appreciate if someone please look into this for me, cheers.
// FileClient.java
import java.net.*;
import java.io.*;
public class FileClient
{
private static final int SERVERPORT = 9999;
public static void main(String[] argv)
{
String inFileName, s, x, outFileName, c, currentLine;
BufferedReader inFile;
PrintWriter outFile;
try
{
System.out.println();
System.out.println();
// Get the filename
Socket conn = new Socket("", SERVERPORT);
BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter a file name to be sent to the server");
inFileName = keyIn.readLine();
// Open the connection to the server and convert the raw streams
// Socket conn = new Socket("LRC-S072", SERVERPORT);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
// Send the filename to the server
pw.println(inFileName);
PrintWriter pow = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
pow.println(inFileName);

System.out.println("Enter output filename:");
outFileName = keyIn.readLine();
inFile = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFileName)));

while ((currentLine = inFile.readLine()) != null)
outFile.println(currentLine);
// release the streams
inFile.close();
outFile.close();
/* System.out.println("Press any key to see the name and cotnents of the file on this client");
c = EasyIn.getString();
//client opens the file and reads it
BufferedReader inFilen = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
while((x = inFilen.readLine()) != null)
{
System.out.println(x);
}
*/
// Read the results line by line, and write them to the display (we could of
// course get a PrintWriter corresponding to a local file, and write the
// results there instead
/*while ((s = br.readLine()) != null)
System.out.println(s);*/
br.close();
pw.close();
inFile.close();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
EasyIn.pause();
}
}
===========================================================================
// FileServer.java
import java.net.*;
import java.io.*;

public class FileServer
{
private static final int PORT = 9999;
public static void main(String[] argv)
{
String inFileName, currentLine, x;
try
{
System.out.println("");
System.out.println("FileServer Waiting for connection");
// Listen for inbound connections, when one comes in, get the Socket object for it,
// and convert the streams of that socket
ServerSocket s = new ServerSocket(PORT, 1);
Socket conn = s.accept();
System.out.println("");
System.out.println("");
System.out.println("<+++++++++++++Client is connected+++++++++++++++++ ");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));//, true);
// Get the filename from the client
inFileName = br.readLine();
// Get an input stream for the corresponding file and turn it into a BufferedReader
BufferedReader inFile = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
// Read the file line by line, and write it back over the network
pw.println(inFileName);
while ((currentLine = inFile.readLine()) != null)
System.out.println(currentLine);
//PrintWriter outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFileName)));
//pw.println(currentLine);
/*BufferedReader inFiles = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
while((currentLine = inFiles.readLine())!= null)
pw.println(inFileName); */
/* PrintWriter psw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
psw.println("Files Reached at the Srerver side"); */
br.close();
pw.close();
// psw.close();
inFile.close();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
EasyIn.pause();
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!


What i really don't understand is with the last sentence of the problem specification i.e. "The file name and it contents are then sent to a client residing �somewhere� on the network" and how it will work out. I would really appreciate if someone please look into this for me, cheers.


You're asking us for help with the one part of the assignment we can't help you with: asking the professor for clarification. If you don't understand the problem, it is the job of whoever assigned it to you to explain it. Understanding and refining requirements is actually an important skill in real-world software development -- you might as well get some practice now!
If later you have some Java questions, come on back and we'll be glad to help you.
reply
    Bookmark Topic Watch Topic
  • New Topic