Skip to content

Commit af79547

Browse files
committed
.
1 parent d0e0463 commit af79547

4 files changed

Lines changed: 215 additions & 0 deletions

File tree

lang.java/TraditionalClient.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package sendfile;
2+
3+
import java.io.DataOutputStream;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import java.net.UnknownHostException;
8+
9+
public class TraditionalClient {
10+
11+
public static void main(String[] args) {
12+
13+
int port = 2000;
14+
var server = "localhost";
15+
Socket socket = null;
16+
String lineToBeSent;
17+
18+
DataOutputStream output = null;
19+
FileInputStream inputStream = null;
20+
int ERROR = 1;
21+
22+
// connect to server
23+
try {
24+
socket = new Socket(server, port);
25+
System.out.println("Connected with server " +
26+
socket.getInetAddress() +
27+
":" + socket.getPort());
28+
}
29+
catch (UnknownHostException e) {
30+
System.out.println(e);
31+
System.exit(ERROR);
32+
}
33+
catch (IOException e) {
34+
System.out.println(e);
35+
System.exit(ERROR);
36+
}
37+
38+
try {
39+
var fname = "sendfile/NetworkInterfaces.c";
40+
inputStream = new FileInputStream(fname);
41+
42+
output = new DataOutputStream(socket.getOutputStream());
43+
long start = System.currentTimeMillis();
44+
var b = new byte[4096];
45+
long read = 0, total = 0;
46+
while((read = inputStream.read(b))>=0) {
47+
total = total + read;
48+
output.write(b);
49+
}
50+
System.out.println("bytes send--"+total+" and totaltime--"+(System.currentTimeMillis() - start));
51+
}
52+
catch (IOException e) {
53+
System.out.println(e);
54+
}
55+
56+
try {
57+
output.close();
58+
socket.close();
59+
inputStream.close();
60+
}
61+
catch (IOException e) {
62+
System.out.println(e);
63+
}
64+
}
65+
}

lang.java/TraditionalServer.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package sendfile;
2+
3+
import java.net.*;
4+
import java.io.*;
5+
6+
public class TraditionalServer {
7+
8+
public static void main(String args[]) {
9+
10+
int port = 2000;
11+
ServerSocket server_socket;
12+
DataInputStream input;
13+
14+
try {
15+
16+
server_socket = new ServerSocket(port);
17+
System.out.println("Server waiting for client on port " +
18+
server_socket.getLocalPort());
19+
20+
// server infinite loop
21+
while(true) {
22+
Socket socket = server_socket.accept();
23+
System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
24+
input = new DataInputStream(socket.getInputStream());
25+
// print received data
26+
try {
27+
byte[] byteArray = new byte[4096];
28+
while(true) {
29+
int nread = input.read(byteArray , 0, 4096);
30+
if (0==nread)
31+
break;
32+
}
33+
}
34+
catch (IOException e) {
35+
System.out.println(e);
36+
}
37+
38+
// connection closed by client
39+
try {
40+
socket.close();
41+
System.out.println("Connection closed by client");
42+
}
43+
catch (IOException e) {
44+
System.out.println(e);
45+
}
46+
47+
}
48+
}
49+
50+
catch (IOException e) {
51+
System.out.println(e);
52+
}
53+
}
54+
}

lang.java/TransferToClient.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sendfile;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.net.InetSocketAddress;
7+
import java.net.SocketAddress;
8+
import java.nio.channels.FileChannel;
9+
import java.nio.channels.SocketChannel;
10+
11+
public class TransferToClient {
12+
13+
public static void main(String[] args) throws IOException{
14+
TransferToClient sfc = new TransferToClient();
15+
sfc.testSendfile();
16+
}
17+
18+
public void testSendfile() throws IOException {
19+
String host = "localhost";
20+
int port = 9026;
21+
SocketAddress sad = new InetSocketAddress(host, port);
22+
SocketChannel sc = SocketChannel.open();
23+
sc.connect(sad);
24+
sc.configureBlocking(true);
25+
26+
String fname = "sendfile/NetworkInterfaces.c";
27+
long fsize = 183678375L, sendzise = 4094;
28+
29+
// FileProposerExample.stuffFile(fname, fsize);
30+
var fc = new FileInputStream(fname).getChannel();
31+
long start = System.currentTimeMillis();
32+
long nsent = 0, curnset = 0;
33+
curnset = fc.transferTo(0, fsize, sc);
34+
System.out.println("total bytes transferred--"+curnset+" and time taken in MS--"+(System.currentTimeMillis() - start));
35+
//fc.close();
36+
}
37+
}

lang.java/TransferToServer.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sendfile;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.ServerSocket;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.ServerSocketChannel;
8+
import java.nio.channels.SocketChannel;
9+
10+
11+
public class TransferToServer {
12+
13+
ServerSocketChannel listener = null;
14+
15+
protected void mySetup() {
16+
InetSocketAddress listenAddr = new InetSocketAddress(9026);
17+
18+
try {
19+
listener = ServerSocketChannel.open();
20+
var ss = listener.socket();
21+
ss.setReuseAddress(true);
22+
ss.bind(listenAddr);
23+
System.out.println("Listening on port : "+ listenAddr.toString());
24+
} catch (IOException e) {
25+
System.out.println("Failed to bind, is port : "+ listenAddr.toString()
26+
+ " already in use ? Error Msg : "+e.getMessage());
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
TransferToServer dns = new TransferToServer();
33+
dns.mySetup();
34+
dns.readData();
35+
}
36+
37+
private void readData() {
38+
ByteBuffer dst = ByteBuffer.allocate(4096);
39+
try {
40+
while (true) {
41+
var conn = listener.accept();
42+
System.out.println("Accepted : "+conn);
43+
conn.configureBlocking(true);
44+
int nread = 0;
45+
while (nread != -1) {
46+
try {
47+
nread = conn.read(dst);
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
nread = -1;
51+
}
52+
dst.rewind();
53+
}
54+
}
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)