Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Sending file using socket

  Asked By: Holly    Date: Sep 16    Category: Java    Views: 2530
  

can anyone help me plss, how to send file of any format using
socket? what's the best stream? thanks......

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Calvin Banks     Answered On: Sep 16

Ok you can use this source code to implement your
requirement:

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

public class EchoClient {
public static void main(String[] args) throws
IOException {

// 1 : the first part will send  information using a
socket
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
echoSocket = new Socket("taranis", 7);
out = new
PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader(

echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host:
taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to:
taranis.");
System.exit(1);
}

// 2 : And the second part of the code will receive
the information previoulsy sent.

BufferedReader stdIn = new BufferedReader(
new
InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}

out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}


But make sure you don't need to use SSL and in that
case there is no proxy between the two machines.
Because SSL tunneling with sockets may cause some
problems and you have to avoid by configuring the
communication channel first.

 
Didn't find what you were looking for? Find more on Sending file using socket Or get search suggestion and latest updates.




Tagged: