The Artima Developer Community
Sponsored Link

Java Answers Forum
File transfer via sockets

17 replies on 2 pages. Most recent reply: Jan 27, 2012 12:54 AM by anex shrach

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 17 replies on 2 pages [ 1 2 | » ]
Sarath

Posts: 2
Nickname: sarathcb
Registered: Feb, 2004

File transfer via sockets Posted: Feb 18, 2004 5:05 PM
Reply to this message Reply
Advertisement
Hello everybody,

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");
}

//Sleep...
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.err.println("Interrupted");
}

} //end infinite while loop
}
}


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: File transfer via sockets Posted: Feb 19, 2004 5:53 AM
Reply to this message Reply
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
---------

logan hacker

Posts: 1
Nickname: xlogan
Registered: Mar, 2007

Re: File transfer via sockets Posted: Mar 29, 2007 5:13 PM
Reply to this message Reply
why dont you post the correct code so if someone see this post he/she can read the correct solution... i am one of these people.

well thanks any way

Sagi friend

Posts: 1
Nickname: massiveoo
Registered: Aug, 2007

Re: File transfer via sockets Posted: Aug 3, 2007 4:11 AM
Reply to this message Reply
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

Cleo Saulnier

Posts: 77
Nickname: vorlath
Registered: Dec, 2005

Re: File transfer via sockets Posted: Aug 5, 2007 8:46 PM
Reply to this message Reply
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.

savvas papas

Posts: 1
Nickname: spasavvas
Registered: Aug, 2007

Re: File transfer via sockets Posted: Aug 13, 2007 5:09 AM
Reply to this message Reply
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();

System.out.println("Server message: " +input.readUTF() );
}
catch ( Exception e ) {
System.err.println(e);
}
}
}

Server

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

class Server{
public static void main (String[] args){

ServerSocket server;
Socket connection;

DataOutputStream output;

BufferedInputStream bis;
BufferedOutputStream bos;

byte[] receivedData;
int in;
try {
server = new ServerSocket( 8585 );

while ( true ) {
connection = server.accept();

output = new DataOutputStream (connection.getOutputStream() );

System.out.println( "Client message: " +input.readUTF() );
output.writeUTF( " ack 1" );

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();

output.writeUTF( " ack 2" );
}
}
catch (IOException e ) { }
}
}

the error i take is: java.net.SocketException: socket closed

plz help me

Nikita Baphana

Posts: 1
Nickname: nvbaphana
Registered: Feb, 2008

Re: File transfer via sockets Posted: Feb 22, 2008 7:20 AM
Reply to this message Reply
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);
}

}//while

}//try
catch(Exception ex)
{
ex.printStackTrace();
}
System.out.println("Writing done");
}//run
public static void main(String args[])throws Exception
{
ServerSend s1=new ServerSend();

s1.run();
}

}

Abdul Hayee

Posts: 4
Nickname: ahayee84
Registered: Jul, 2008

Re: File transfer via sockets Posted: Jul 30, 2008 9:04 AM
Reply to this message Reply
> 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.

ahayee84 .

vishali penmathsa

Posts: 3
Nickname: chavi1518
Registered: Nov, 2008

Re: File transfer via sockets Posted: Nov 18, 2008 12:37 PM
Reply to this message Reply
Hi,Even I am facing the same problem as above.Can u send me the corrected code to my mail address chavi1518@yahoo.co.in.


Thanks

vishali penmathsa

Posts: 3
Nickname: chavi1518
Registered: Nov, 2008

Re: File transfer via sockets Posted: Nov 18, 2008 12:56 PM
Reply to this message Reply
Hi,

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.

vishali penmathsa

Posts: 3
Nickname: chavi1518
Registered: Nov, 2008

Re: File transfer via sockets Posted: Nov 18, 2008 3:04 PM
Reply to this message Reply
> 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 .

Thanks for your help....

Vishali

rama kant timsina

Posts: 1
Nickname: frezone
Registered: Dec, 2008

Re: File transfer via sockets Posted: Dec 22, 2008 6:03 AM
Reply to this message Reply
plz, i also need this i am struggling it with it to.thanx in advance...........

divya nair

Posts: 1
Nickname: divyacek
Registered: Dec, 2008

Re: File transfer via sockets Posted: Dec 29, 2008 10:37 PM
Reply to this message Reply
Hiii Viswa,
Can u pls send me the complete code for file transfer using sockets. My email divyagnr@gmail.com
Thanks in advance

Pradeep Kallu

Posts: 1
Nickname: pradeep158
Registered: Feb, 2009

Re: File transfer via sockets Posted: Feb 18, 2009 7:37 AM
Reply to this message Reply
I am also stuck with the same problem...Can u please mail me the code vishwa...my mail ID is: kallupradeep86@gmail.com

charanya subramanian

Posts: 1
Nickname: scharanya
Registered: Feb, 2009

Re: File transfer via sockets Posted: Feb 22, 2009 2:06 AM
Reply to this message Reply
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 [ 1  2 | » ]
Topic: java help Previous Topic   Next Topic Topic: dos commands

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use