Results 41 to 51 of 51
- 07-23-2010, 02:22 PM #41
Member
- Join Date
- Jul 2010
- Posts
- 16
- Rep Power
- 0
- 07-27-2010, 02:02 PM #42
Member
- Join Date
- Jul 2010
- Posts
- 16
- Rep Power
- 0
hiya, just a quick question, if to send a file to another ip add, do we change the InetAddress address = InetAddress.getByName(null) to InetAddress address = InetAddress.getAddress(null);
Thanks
- 07-29-2010, 05:35 PM #43
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
i think you should change that one to
Java Code:InetAddress address = InetAddress.getLocalHost();
- 07-29-2010, 05:40 PM #44
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
i have a question about the same code.
i update my code and try to send any random file path in computer. if the file i want to send is small enough, client will have it all, but if the the file is too small, then client just receive part of file i am sending.
can you give my any suggestion? thank you
this is my code
server
and this is client codeJava Code:import java.io.IOException; import java.net.*; import java.io.*; import java.lang.*; public class MftpServer { public static final int port = 8080; public static void main(String arg[]) throws IOException { String line; ServerSocket p; try { p = new ServerSocket(port); System.out.println("Server Started: " + p); byte[] buffer = new byte[1000]; Socket socket = p.accept(); System.out.println("Connection accepted: " + socket); //copy file code here try { BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); DataInputStream in = new DataInputStream(bis); int nbrRd = in.read(buffer); line = String.valueOf(nbrRd); System.out.println("Data from client: \n" + "> " + new String(buffer, 0, nbrRd) + "\n <"); } catch (Exception x) { x.printStackTrace(); } finally { socket.close(); System.out.println("Socket is closing"); } } catch (Exception x) { x.printStackTrace(); } finally { // p.close(); System.out.println("Server socket is closing"); } } }); }
thank you againJava Code:import java.io.IOException; import java.net.*; import java.io.*; import java.util.Scanner; public class MftpClient { public static void main(String arg[]) throws IOException { try { InetAddress address = InetAddress.getLocalHost(); System.out.println("Address = " + address); Socket socket = new Socket(address, 8080); String path; int length; Scanner input = new Scanner(System.in); System.out.println("Please enter file path you want to send: "); path = input.nextLine(); String source = path; FileInputStream in = new FileInputStream(source); byte[] buf = in.toString().getBytes(); OutputStream out = new BufferedOutputStream(socket.getOutputStream()); try { while ((length = in.read(buf)) > 0) { out.write(buf, 0, length); } } catch (Exception x) { x.printStackTrace(); } finally { out.flush(); out.close(); in.close(); System.out.println("client socket is closing"); socket.close(); } } catch (Exception x) { x.printStackTrace(); } } }); } }
- 07-29-2010, 05:46 PM #45
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
this is the original file:
and this is the result after i ran my programJava Code:import java.net.*; import java.io.*; import java.util.Scanner; public class Client { public static void main(String arg[]) throws IOException { try { InetAddress address = InetAddress.getByName(null); System.out.println("Address = " + address); Socket socket = new Socket(address, 8080); String path; int length; Scanner input = new Scanner(System.in); System.out.println("Please enter file path you want to send: "); path = input.nextLine(); String source = path; FileInputStream in = new FileInputStream(source); byte[] buf = in.toString().getBytes(); OutputStream out = new BufferedOutputStream(socket.getOutputStream()); try { while ((length = in.read(buf)) > 0) { out.write(buf, 0, length); } } catch (Exception x) { x.printStackTrace(); } finally { out.flush(); out.close(); in.close(); System.out.println("client socket is closing"); socket.close(); } } catch (Exception x) { x.printStackTrace(); } } }
so it stopped at "x.printStack" and it closed socket, i dont know how to fix this problem.Java Code:"C:\Program Files\Java\jdk1.6.0_20\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52283,suspend=y,server=n -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.6.0_20\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.6.0_20\jre\lib\json.jar;C:\Users\Chien Le\Research\2010-05 MMPong\out\production\MMPong;C:\Users\Chien Le\Research\2010-05 MMPong\out\production\jstyx-0.2.0;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\mina-0.8.0.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\log4j-1.2.8.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\dom4j-1.6.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\jaxen-1.1-beta-5.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\JSAP-2.0b.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\slf4j-log4j12-1.0-beta9.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\TableLayout.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\quartz-1.5.1.jar;C:\Users\Chien Le\Research\2010-05 MMPong\jstyx-0.2.0\lib\commons-logging-1.0.4.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 9.0.2\lib\idea_rt.jar" edu.wustl.cse.mobilab.mftp.MftpServer Connected to the target VM, address: '127.0.0.1:52283', transport: 'socket' Server Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080] Connection accepted: Socket[addr=/172.18.3.8,port=52287,localport=8080] Data from client: > import java.net.*; import java.io.*; import java.util.Scanner; public class Client { public static void main(String arg[]) throws IOException { try { InetAddress address = InetAddress.getByName(null); System.out.println("Address = " + address); Socket socket = new Socket(address, 8080); String path; int length; Scanner input = new Scanner(System.in); System.out.println("Please enter file path you want to send: "); path = input.nextLine(); String source = path; FileInputStream in = new FileInputStream(source); byte[] buf = in.toString().getBytes(); OutputStream out = new BufferedOutputStream(socket.getOutputStream()); try { while ((length = in.read(buf)) > 0) { out.write(buf, 0, length); } } catch (Exception x) { x.printStack < Socket is closing Server socket is closing
i think this is caused because of the limitation of transfer data between server and client. so i think i could cut down file into different packets and send it from server to client, but i dont know where i need to start.
thank you
- 07-29-2010, 05:56 PM #46
How many bytes were read by the server?
How many bytes were written by the client?
You only do one read in the Server? Why not loop to read all of the bytes sent?
Have you tried sending a shorter file. Say one less than 1000 bytes?
- 07-29-2010, 06:12 PM #47
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
i tried sending shorter file (which <= 1000 bytes) and it worked prefect.
but if it is bigger than 1000 bytes, then server only display 1000 bytes data in the file and then it will close the socket, the rest data after 1000 bytes will not display on server side.
- 07-29-2010, 06:22 PM #48
Do you think the following has anything to do with your problem?
byte[] buffer = new byte[1000];
You only do one read in the Server? Why not loop to read all of the bytes sent?
- 07-29-2010, 06:31 PM #49
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
if i change value of 1000 to bigger value, it will receive more data in file
what if the file is too large cause us cant write value into new byte?
- 07-29-2010, 06:45 PM #50
Why not use a loop to read the bytes sent little by little?
- 07-29-2010, 07:01 PM #51
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
sending file from client to server - socket closed error
By forex in forum NetworkingReplies: 3Last Post: 04-05-2010, 02:19 AM -
Sending Text File --- Server-To-Client
By nigamsir in forum NetworkingReplies: 1Last Post: 03-08-2010, 03:45 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
Sending a .Doc file from client to a server through socket
By amita_k29 in forum NetworkingReplies: 1Last Post: 02-10-2009, 09:16 AM -
sending jar files from client to server?
By gobinathm in forum New To JavaReplies: 2Last Post: 11-13-2007, 05:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks