Results 1 to 3 of 3
Thread: Sending a file through socket
- 03-05-2009, 02:58 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
Sending a file through socket
Hi everybody..........
I'm trying to send a file from server to client through sockets.
There is no problem in server side. In client side the program doesn't quits. I can't get the file fully written.
Here is the code.... Anybody help me please...........
Server:
import java.io.*;
import java.net.*;
class ServerTest
{
public static void main(String args[]) throws IOException {
Server a = new Server();
a.start();
}
}
class Server {
BufferedInputStream bis;
BufferedOutputStream bos;
int in;
public void start() throws Exception,Throwable
{
ServerSocket ss=new ServerSocket(1870);
while (true) {
try{
System.out.println("Waiting for Client");
Socket s=ss.accept();
System.out.println("\nClient connected...");
String filename="ex081111.log";
System.out.println("Sending the file");
try
{
bis=new BufferedInputStream(new FileInputStream(filename));
bos=new BufferedOutputStream(s.getOutputStream());
byte[] sendData=new byte[8192];
while((in=bis.read(sendData))!=-1){
bos.write(sendData, 0,in);
}
bis.close();
bos.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("File Sent Successfully" );
}
catch(Exception e)
{
//Destroys the current instance
this.finalize();
}
}
}
}
Client:
import java.net.*;
import java.io.*;
class ClientTest
{
public static void main(String args[]) throws Exception
{
try
{
Socket s=new Socket("localhost",
System.out.println("Ready to receive");
BufferedInputStream bis;
BufferedOutputStream bos;
int in;
byte[] recievedData = new byte[8192];
bis = new BufferedInputStream(s.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("recievedfile.txt"));
while ((in = bis.read(recievedData)) != -1)
bos.write(recievedData,0,in);
System.out.println("File Recieved Successfully");
bos.close();
bis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
- 03-13-2009, 08:09 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
maybe use a PrintWriter server side an init it with PrintWriter(s.getOutputStream(),true);
- 03-14-2009, 07:32 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
sending file over network
By qwerty in forum NetworkingReplies: 6Last Post: 04-25-2009, 01:55 AM -
problem in socket connection in sending images
By vibhor in forum NetworkingReplies: 2Last Post: 02-20-2009, 05:39 AM -
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 image file from JSP to Servlet
By ravian in forum Advanced JavaReplies: 2Last Post: 01-10-2008, 02:34 PM -
Problems sending file throught TCP sockets
By Nite in forum Advanced JavaReplies: 2Last Post: 08-04-2007, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks