Simple Java Client Server Solution07/12/07 Permalink
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(20000);
while(true){
Socket s = ss.accept();
InputStream is = s.getInputStream();
OutputStream os = new FileOutputStream(new Random().nextInt(100000)+".file");
byte[] b = new byte[2048];
int n;
while((n = is.read(b,0,b.length)) !=-1) {
os.write(b, 0, n);
os.flush();
}
os.close();
is.close();
}
}
}
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 20000);
OutputStream os = s.getOutputStream();
InputStream is = new FileInputStream("filetotransfer.file");
byte[] b = new byte[2048];
int n;
while((n = is.read(b,0,b.length)) !=-1) {
os.write(b, 0, n);
os.flush();
}
os.close();
is.close();
}
}
|
|