java net working communication two systems
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ranjith_Kumar
Posted On:   Monday, February 26, 2007 01:52 PM

How to transfer a file from one computer which is connected through internet, to another computer ,it is also connected through internet.
Please send a program.

Re: java net working communication two systems

Posted By:   Robert_Lybarger  
Posted On:   Monday, February 26, 2007 06:44 PM


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

public class FileSender {
public static void main(String[] args) {
try {
File file = new File(System.getProperty("user.home"),"myfile.ext");
FileInputStream input = new FileInputStream(file);
InetAddress addr = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(addr,port);
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
byte[] buffer = new byte[1024];
int num;
while ((num=input.read(buffer)) != -1) {
output.write(buffer,0,num);
}
output.close();
input.close();
}
catch (Exception e) {
System.err.println("Something bad happened: "+e);
e.printStackTrace(System.err);
}
}
}
About | Sitemap | Contact