| Author |
Reading a binary file from a client to server
|
tina Kmaal
Greenhorn
Joined: Dec 28, 2006
Posts: 7
|
|
is it allowed to do the following: the client side: and this is the Server side:
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
From just a quick read it looks like it ought to run fine. Does it do what you want it to?
|
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
|
 |
tina Kmaal
Greenhorn
Joined: Dec 28, 2006
Posts: 7
|
|
|
I am trying to run them both in the same machine but when I do that I got no respond
|
 |
Chris Beihl
Greenhorn
Joined: Jan 02, 2007
Posts: 4
|
|
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...
|
 |
Sushil Sharma
Greenhorn
Joined: Nov 02, 2006
Posts: 22
|
|
Both server and client programs have few issues: server and client port numbers are different. They should be sameserver'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 hostwhat 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
Joined: Nov 02, 2006
Posts: 22
|
|
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
|
 |
 |
|
|
subject: Reading a binary file from a client to server
|
|
|