• 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

Reading a binary file from a client to server

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it allowed to do the following:
the client side:


and this is the Server side:
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From just a quick read it looks like it ought to run fine. Does it do what you want it to?
 
tina Kmaal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to run them both in the same machine but when I do that I got no respond
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to run your code sample but there are several unresolved types :
GUI
ResourceAllocator
CPU
Memory
DiskS
Resources

Without those other classes its difficult to run your example...
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both server and client programs have few issues:
  • server and client port numbers are different. They should be same
  • server's IP address is that of local host. I am not sure what is address of client host? To run on same host both server and host IP address should be that of local host
  • what is the format of your .dat file?
  • if numbers are specified in ASCII format in myFile.dat, I believe you cannot correctly read with DataInputStream. Refer to DataInput class to see input format

  •  
    Sushil Sharma
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    See modified client and server code. I removed stuff not required for my test.

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

    public class strtest_client
    {
    public static void main(String[] args) throws IOException {
    String IP = args[0];
    String port = args[1];

    // String IP = "192.168.1.3";
    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
    echoSocket = new Socket(IP, 4444);
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(echoSocket
    .getInputStream()));
    } catch (Exception ex) {
    System.out.println("error");
    ex.printStackTrace();
    System.exit(1);
    }
    try {
    /* DataInputStream inStream = new DataInputStream(new FileInputStream(
    "myFile.dat")); */
    // convert to java input format
    BufferedReader infile = new BufferedReader(new FileReader(new File("myFile.dat")));
    DataOutputStream outfile = new DataOutputStream(new FileOutputStream(new File("myFile.java.dat")));

    while (true) {
    String line = infile.readLine();
    if (line == null) break; outfile.writeInt(Integer.parseInt(line.trim()));
    }
    infile.close();
    outfile.close();

    DataInputStream inStream = new DataInputStream(new FileInputStream(
    "myFile.java.dat"));
    try {
    while (true) {
    int num = inStream.readInt();

    System.out.println(num);
    out.print(num);
    }
    } catch (IOException e) {
    System.out.println("finish reading the file");
    } finally {
    inStream.close();
    out.close();
    in.close();
    echoSocket.close();

    // add these
    inStream = null;
    out = null;
    in = null;
    }

    //problem.make sure that in stream is open
    if (in != null) {
    System.out.println("echo: " + in.readLine());
    }

    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    === Server code ====
    import java.io.*;
    import java.net.*;

    public class strtest_server {
    public static void main(String[] args) throws IOException {

    int port = Integer.parseInt(args[0]);

    ServerSocket serverSocket = null;

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

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

    int [] arr = new int[3];

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    clientSocket.getInputStream()));

    String inputLine, outputLine;

    out.println(" ");

    int i =0;

    while ((inputLine = in.readLine()) != null) {
    Integer num = Integer.parseInt(inputLine);
    arr[i]=num;
    i++;
    }

    out.close();
    in.close();

    clientSocket.close();
    serverSocket.close();
    }
    }

    To run on same machine, I start server as:
    java strtest_server 4444
    client as
    java strtest_client 10.0.40.103 4444

    Hope this help
     
    Bring me the box labeled "thinking cap" ... and then read this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic