View Single Post
  #1 (permalink)  
Old 07-23-2007, 02:27 AM
gabriel gabriel is offline
Member
 
Join Date: Jul 2007
Posts: 41
gabriel is on a distinguished road
how to send files through sockets
I'm developing a program and I need to send files through sockets
I'm using BufferInputStream,DataInputStream.
I choose some file from server and then send it to the client.
The client receives it but it doesn't open!
I send you my code


In server I do something like that:
Code:
public class Server { static final int Port=5000; public int nClients=0; private String file="./files/3.jpg"; /** Creates a new instance of server*/ public Server() { try { ServerSocket serverSock=new ServerSocket(Port); System.out.println( Port); while(true) { serverSock.accept(); Socket cliente=serverSock.accept(); nClients++; System.out.println(nClients); SClient(client,nClients); SocketThread thread=new SocketThread (client,file,nClients-1); hilo.start(); } } catch(Exception ex) { System.out.println(ex); } }
In the client I do that:
Code:
DataInputStream en = new DataInputStream (skClient.getInputStream()); byte buffer[] = new byte[breaden]; //breaden=en.read(buffer); File rec=new File("./rec/1.jpg"); rec.setReadable(true); rec.setWritable(true); //FileOutputStream f=new FileOutputStream(rec); FileOutputStream f=new FileOutputStream ("./rec/1.jpg"); f.write(buffer); skClient.close();
Reply With Quote
Sponsored Links