I'm able to transfer a file from client to server but unfortunately when i dont set the path on the server, it throws me with the following error: java.net.BindException: Already in use:JVM_Bind
and if I set the path, the file is transferred but the filed is saved on the the name which I've set in the server program.For ex: If I send, test.java from client, Its saved as receiveddata on my server.How can I avoid this???I'd be greatful if somebody can fix my code.
Thanks in advance Client Program class Client_socket {
static final int PORT = 5791; //Change this to the relevant port static final String HOST = "127.0.0.1"; //Change this to the relevant HOST,//(where Server.java is running)
public static boolean send( String filename ) {
try { System.out.print("Sending data...\n"); Socket skt = new Socket(HOST, PORT);
//Create a file input stream and a buffered input stream. FileInputStream fis = new FileInputStream(filename); BufferedInputStream in = new BufferedInputStream(fis); BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );
//Read, and write the file to the socket int i; while ((i = in.read()) != -1) { out.write(i); //System.out.println(i); }
//out.write(-1); // to signal the end of file?? //Close the socket and the file out.flush(); out.close(); in.close(); skt.close();
return true; } catch( Exception e ) {
System.out.print("Error! It didn't work! " + e + "\n");
return false; } } }
Server class Server {
// static final String OUTPUTFILENAME = "";
//************************************************************************ ***************** //If I dont set the path, it throws me with error java.net.BindException: Already in use: //JVM_Bind.Suunds like the server is not getting the file name from the client.How can I fix //this one???? //*************************************************** static final String OUTPUTFILENAME = "C:\\receiveddata"; static final int PORT = 5791;
public static void main(String args[]) {
System.out.println("New server running...\n\n");
// Infinite loop, innit while ( true ) {
try { //Create a socket ServerSocket srvr = new ServerSocket(PORT); Socket skt = srvr.accept();
//Create a file output stream, and a buffered input stream FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME); BufferedOutputStream out = new BufferedOutputStream(fos); BufferedInputStream in = new BufferedInputStream( skt.getInputStream() );
//Read, and write the file to the socket int i; while ((i = in.read()) != -1) { out.write(i); //System.out.println(i); System.out.println("Receiving data..."); } out.flush(); in.close(); out.close(); skt.close(); srvr.close(); System.out.println("Transfer complete."); } catch(Exception e) {
System.out.print("Error! It didn't work! " + e + "\n"); }
1. I don't think you are sending a File itself as per the code here, it Client read the file and send the as a text , so server wil display the text in its console,
2.To transfer a file, you have read the text in server side and write to a file.[File Write operation you have not done]
3. About the Error, it is due to once you run server and not stoeped properly then next time you will get Address in use error, please check is the server stoped and port is released.
I have emailed the full correct code you can use that...for Java Client /server.
salamz everybody.. plz any body send me da whole code of "File transfer via sockets" in Java Language ...plz send me i will be very grateful 2 U...i m waiting yr response....hope so u reply me as soon as possible...plz JAZAKALLAH
This isn't strictly necessary, but if you want improved speed, you should use the alternate form of read where you can use a byte array. Read up on it.
As for the filename, there's no amount of code that can explain this. If you just want the code without understanding, then I'm not interested. You should be able to write it up easily if you do understand.
What you have here is seemingly a misunderstanding of how files work. A file has a name because someone gave it a name. But the name is NOT part of the file. It's like a stamp on a box. It's just an identifier. It's not the actual thing in question. Much like variable names.
What you need to do is come up with your own protocol to transfer the filename over before the content is sent. Once the server has the filename, it can open the output stream and then start writing into it. An easy way to do this is to just send a null character after the filename (and the actual file would follow after that). On the server side, you will need a StringBuffer to contain the incoming filename.
Here, it's actually best to read one character at a time and store it in your StringBuffer until you reach a zero. Then you use the StringBuffer for the filename of your output stream. From then on, the rest of the code should be the same as what you have now unless you want to start using the alternate read() method.
plz any body send me whole code of "File transfer via sockets".
i make a client - server file transfer but when server receive file i want serve send a msg to client that take the file. I try different but nothing.
my code is: Client import java.net.*; import java.io.*;
class Client{ public static void main (String[] args){
DataInputStream input;
BufferedInputStream bis; BufferedOutputStream bos; int in; byte[] byteArray;
try {
Socket client = new Socket("127.0.0.1", 8585);
input = new DataInputStream (client.getInputStream() );
output.writeUTF( "send a file" ); System.out.println("Server message: " +input.readUTF() );
bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt")); bos = new BufferedOutputStream(client.getOutputStream()); byteArray = new byte[8192]; while ((in = bis.read(byteArray)) != -1){ bos.write(byteArray,0,in); } bis.close(); bos.close();
receivedData = new byte[8192]; bis = new BufferedInputStream(connection.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream("sss.txt")); while ((in = bis.read(receivedData)) != -1){ bos.write(receivedData,0,in); } bos.close();
Can u send me the file transfer code please? I have also written one but only new file is getting created without any contents i.e. empty file. My email is: Nikita.Baphana@gmail.com here is my code:
Client import java.io.*; import java.net.*; public class FileSend {
void sendFile(String fileName,String cHost,int cPort)throws Exception { Socket sSocket = new Socket("localhost",6000); OutputStream sOS = sSocket.getOutputStream();
File file = new File(fileName); int length = (int)file.length(); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); byte[] buffer = new byte[1024]; int bytesRead; while((bytesRead = bis.read(buffer)) != -1) { sOS.write(buffer); } } //Reading the file (Visualize this as a ServerThread Socket File Listener) public static void main(String args[])throws Exception { FileSend f1=new FileSend(); String fileName="C:\\Users\\Nikita\\Shree\\Add.java"; String chost="117.195.1.97"; f1.sendFile(fileName,chost,6000); }
}
//Server
import java.io.*; import java.net.*; import java.lang.*; public class ServerSend { ServerSocket ss; public void run()throws Exception { ss=new ServerSocket(4000); int i=0; InputStream is= new FileInputStream("C:\\Users\\Nikita\\Shree\\Add.java"); byte[] buffer = new byte[1024]; ss.accept(); System.out.println("Binded"); try { while(true) { i++; String f2="C:\\Users\\Nikita\\Desktop\\Nikita\\temp.java"; FileOutputStream fos = new FileOutputStream(new File(f2)); while(is.read(buffer)!=-1) { fos.write(buffer); }
> Hi, > > 1. I don't think you are sending a File itself as per the > code here, it > Client read the file and send the as a text , so server > wil display the text in its console, > > 2.To transfer a file, you have read the text in server > side and write to a file.[File Write operation you have > not done] > > 3. About the Error, it is due to once you run server and > not stoeped properly then next time you will get Address > in use error, please check is the server stoped and port > is released. > > > I have emailed the full correct code you can use > that...for > Java Client /server. > > Let me know if you need more info... > > Viswa > ---------------------------------------------------------
Dear Mr. Basavalingappa , It is quite topic know , about four years . However I am stuck with the same proble,
I am trying to transfer a text file to the computer next floor , but on the same LAN. But same is not working throwing exceptions all the time.
You indicated that you provided a working code toa desperate user like me. If this code is some still on your PC , Please send me a copy.
I shall be highly grateful. Best of regards and thanks.
I am also having problem with the code for file transfer over sockets.If Vishwa as sent you the right code can you send it to me as well.i already kept him an email.
> Hi, > > 1. I don't think you are sending a File itself as per the > code here, it > Client read the file and send the as a text , so server > wil display the text in its console, > > 2.To transfer a file, you have read the text in server > side and write to a file.[File Write operation you have > not done] > > 3. About the Error, it is due to once you run server and > not stoeped properly then next time you will get Address > in use error, please check is the server stoped and port > is released. > > > I have emailed the full correct code you can use > that...for > Java Client /server. > > Let me know if you need more info... > > Viswa > ---------
HiViswa,
Can you plzz send me the code to my email address chavi1518@yahoo.co.in.I am totally struck at this point .
hi all....evev i have the same problem for file transfer using sockets......can any one send me the full code please......my mail id is scharanya@gmail.com thanks in advance
Flat View: This topic has 17 replies
on 2 pages
[
12
|
»
]