The Artima Developer Community
Sponsored Link

Java Answers Forum
Send file through socket

1 reply on 1 page. Most recent reply: Apr 8, 2002 4:17 PM by Thomas SMETS

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 1 reply on 1 page
John Fox

Posts: 2
Nickname: john
Registered: Apr, 2002

Send file through socket Posted: Apr 5, 2002 9:58 AM
Reply to this message Reply
Advertisement
If anybody could help me meet this coursework deadline with a working system, i would be SINCERLY greatful. I have created client side and server side applications. I know how to send text through the socket but how in the world do i send files, ie. .doc files, that exist on the client harddisk??? Can i send it through a fileoutputstream? If anyone could show me some code, i'd be greatful cause i've been searching all over the net with no success...Thanks!

-------------------------------------------------------
clie nt side
-------------------------------------------------------
public static void main(String args[]){

System.out.println("Establishing connection...");

try {

Socket socket;
socket = new Socket("127.0.0.1", 1287);

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

System.out.println("Message: ");
String message = Console.in.readWord();

output.println(message); output.flush();

System.out.print(input.readLine());

input.close();
output.close();
socket.close();

} catch(Exception e) {}
}
-------------------------------------------------------


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

System.out.println("Listening for clientconnection");

try {

ServerSocket socket = new ServerSocket(1287); Socket insocket = socket.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(insocket.getInputStream()));
PrintWriter output = new PrintWriter(insocket.getOutputStream(), true);

System.out.println(input.readLine());

System.out.println("Message: ");
String message = Console.in.readWord();

output.println(message); output.flush();

insocket.close(); //close insocket
input.close();
output.close();

}

catch (Exception e) {}

}
-------------------------------------------------------


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Send file through socket Posted: Apr 8, 2002 4:17 PM
Reply to this message Reply
Why is the Object sent over the Socket ?

Because it's Serializable

Just read a little bit hte Docs and you will notice that you can easily have a code like :
  Socket s = new Socket(...);
  File f = new File(...);
  BufferedReader input = new BufferedReader(new   InputStreamReader(s.getInputStream()));
  PrintWriter output = new PrintWriter(socket.getOutputStream(), true); 
 
  out.write(f);
  out.flush();
  s.close();


thomas,

Flat View: This topic has 1 reply on 1 page
Topic: Arrays in Conways Game of Life Previous Topic   Next Topic Topic: batch function

Sponsored Links



Google
  Web Artima.com   

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