Skip to content

Commit

Permalink
for exam
Browse files Browse the repository at this point in the history
  • Loading branch information
YunFeng0817 committed Nov 7, 2018
1 parent 79b6369 commit 924cb4a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
44 changes: 21 additions & 23 deletions src/Lab2/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@
import java.io.ByteArrayOutputStream;
import java.io.*;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Client {

public static void main(String[] args) throws IOException {
// System.out.println("Please input destiny server host :");
// Scanner in = new Scanner(System.in);
// String host = in.nextLine();
// System.out.println("Please input destiny server port :");
// int port = in.nextInt();
public static void main(String[] args) throws IOException, InterruptedException {
File file = new File("./src/Lab2/1.png");
// File files = new File("./src/Lab2/3.png");
// if (!files.exists()) {
// files.createNewFile();
// }
File files = new File("./src/Lab2/4.png");
if (!files.exists()) {
files.createNewFile();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CloneStream(byteArrayOutputStream, new FileInputStream(file));
// SR client = new SR(host, port);
SR client = new SR("localhost", 8080);
// System.out.println("Input instruction :");
// String instruction = in.nextLine();
// client.send(instruction.getBytes());
System.out.println("Start to send file 1.png to " + "localhost" + 8080);
client.send(byteArrayOutputStream.toByteArray());
// FileOutputStream fileOutputStream = new FileOutputStream(files);
// fileOutputStream.write(test.all.toByteArray(), 0, test.all.size());
// fileOutputStream.close();
Thread.sleep(6000);
while (true) {
byteArrayOutputStream = client.receive();
if (byteArrayOutputStream.size() != 0) {
FileOutputStream fileOutputStream = new FileOutputStream(files);
fileOutputStream.write(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size());
fileOutputStream.close();
System.out.println("Get the file ");
System.out.println("Saved as 4.png");
break;
}

Thread.sleep(50);
}
}

/**
Expand All @@ -42,7 +40,7 @@ public static void main(String[] args) throws IOException {
* @param InputStream the input stream to be cloned
* @throws IOException when read input stream, some exception occur
*/
private static void CloneStream(ByteArrayOutputStream CloneResult, InputStream InputStream) throws IOException {
static void CloneStream(ByteArrayOutputStream CloneResult, InputStream InputStream) throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = InputStream.read(buffer)) != -1) {
Expand Down
20 changes: 10 additions & 10 deletions src/Lab2/SR.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void send(byte[] content) throws IOException {
sendIndex += length;
DatagramPacket datagramPacket = new DatagramPacket(oneSend.toByteArray(), length + 2, host, port);
datagramSocket.send(datagramPacket);
System.out.println("base " + base + " seq" + sendSeq);
// System.out.println("base " + base + " seq" + sendSeq);
sendSeq++;
}
datagramSocket.setSoTimeout(1000);
Expand All @@ -58,7 +58,6 @@ void send(byte[] content) throws IOException {
int ack = (int) ((recv[0] & 0x0FF) - base);
// System.out.println("seq" + (recv[0] & 0x0FF));
// System.out.println("base" + base);
System.out.println(ack);
timers.set(ack, -1);
}
} catch (SocketTimeoutException e) { // out of time
Expand Down Expand Up @@ -100,9 +99,9 @@ ByteArrayOutputStream receive() throws IOException {
int count = 0; // used to simulate datagram loss
ByteArrayOutputStream result = new ByteArrayOutputStream(); // store the received content
DatagramSocket datagramSocket = new DatagramSocket(port); // UDP socket to receive datagram and send ACKs
List<ByteArrayOutputStream> datagramBuffer = new ArrayList<>(); // window buffer,used to store the datagram out of order
List<ByteArrayOutputStream> datagramBuffer = new LinkedList<>(); // window buffer,used to store the datagram out of order
DatagramPacket recvPacket; // one temp datagram packet
List<Integer> timers = new ArrayList<>(); // to mark the datagram's status, 0 -> hasn't not received -1->obtained
List<Integer> timers = new LinkedList<>(); // to mark the datagram's status, 0 -> hasn't not received -1->obtained
int time = 0;
datagramSocket.setSoTimeout(1000);
long max = 0;
Expand All @@ -116,16 +115,16 @@ ByteArrayOutputStream receive() throws IOException {
byte[] recv = new byte[1500];
recvPacket = new DatagramPacket(recv, recv.length);
datagramSocket.receive(recvPacket);
if (count % 70 != 0) {
if (count % 170 != 0) {
long base = recv[0] & 0x0FF;
long seq = recv[1] & 0x0FF;
if (seq - base > max) {
max = seq - base;
}
System.out.println("i " + (seq - base));
System.out.println("timer " + timers.get((int) (seq - base)));
// System.out.println("i " + (seq - base));
// System.out.println("timer " + timers.get((int) (seq - base)));
if (timers.get((int) (seq - base)) == 0) {
System.out.println(seq);
// System.out.println(seq);
ByteArrayOutputStream recvBytes = new ByteArrayOutputStream();
recvBytes.write(recv, 2, recvPacket.getLength() - 2);
datagramBuffer.set((int) (seq - base), recvBytes);
Expand All @@ -135,6 +134,7 @@ ByteArrayOutputStream receive() throws IOException {
recvPacket = new DatagramPacket(recv, recv.length, recvPacket.getAddress(), recvPacket.getPort());
datagramSocket.send(recvPacket);
timers.set((int) (seq - base), -1);
System.out.println("Receive datagram " + seq);
}
}
count++;
Expand All @@ -143,7 +143,7 @@ ByteArrayOutputStream receive() throws IOException {
time++;
}
if (checkWindow(timers)) { // check if it's ok to slide window
System.out.println("slide");
// System.out.println("slide");
ByteArrayOutputStream temp = getBytes(datagramBuffer, WindowSize);
result.write(temp.toByteArray(), 0, temp.size());
max = 0;
Expand All @@ -154,7 +154,7 @@ ByteArrayOutputStream receive() throws IOException {
timers.add(0);
}
}
if (time > 10) { // check if the connect out of time
if (time > 4) { // check if the connect out of time
ByteArrayOutputStream temp = getBytes(datagramBuffer, max + 1);
result.write(temp.toByteArray(), 0, temp.size());
break;
Expand Down
17 changes: 11 additions & 6 deletions src/Lab2/Server.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package Lab2;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.io.*;

public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
File file = new File("./src/Lab2/2.png");
File file2 = new File("./src/Lab2/3.png");
if (!file.exists()) {
file.createNewFile();
}
SR server = new SR("localhost", 8080);
while (true) {
SR server = new SR(8080);
ByteArrayOutputStream byteArrayOutputStream = server.receive();
// if (byteArrayOutputStream.size() != 0) {
// System.out.println(byteArrayOutputStream.toString());
Expand All @@ -23,9 +20,17 @@ public static void main(String[] args) throws IOException, InterruptedException
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size());
fileOutputStream.close();
System.out.println("Get the file ");
System.out.println("Saved as 2.png");
fileOutputStream.close();
break;
}

Thread.sleep(50);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Client.CloneStream(byteArrayOutputStream, new FileInputStream(file2));
System.out.println("Start to send file 3.png to " + "localhost" + 8080);
server.send(byteArrayOutputStream.toByteArray());
}
}

0 comments on commit 924cb4a

Please sign in to comment.