Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256.ormlite;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class Foo {
- private static long FILE_SIZE = 1000000000L;
- private static int NUM_STREAMS = 8;
- private static int NUM_TRANSMISSIONS = 100;
- public static void main(String[] args) throws Exception {
- new Server().start();
- new Client().start();
- }
- public static class Server extends Thread {
- ServerSocket sSocket;
- public Server() throws Exception {
- sSocket = new ServerSocket();
- sSocket.bind(new InetSocketAddress(8080));
- }
- @Override
- public void run() {
- Socket socket;
- OutputStream stream;
- try {
- socket = sSocket.accept();
- stream = socket.getOutputStream();
- } catch (IOException e2) {
- e2.printStackTrace();
- return;
- }
- // send the data
- try {
- for (int i = 0; i < NUM_TRANSMISSIONS; i++) {
- System.out.println(sSocket.getLocalPort() + " sending set " + (i + 1) + " of " + 10);
- stream.write(new byte[(int) (FILE_SIZE / NUM_STREAMS / NUM_TRANSMISSIONS)]);
- stream.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Closing server (at write bytes) with socket id: " + sSocket.getLocalPort());
- }
- System.out.println("Server " + sSocket.getLocalPort() + " done");
- // close the socket
- try {
- stream.close();
- socket.close();
- sSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static class Client extends Thread {
- @Override
- public void run() {
- byte[] bytes = new byte[1024]; // number of bytes to read per client stream
- // connect to server
- Socket connectionSocket;
- InputStream stream;
- try {
- connectionSocket = new Socket("localhost", 8080);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- return;
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
- // read file from server
- int value = 0;
- int bytesRead = 0;
- try {
- stream = connectionSocket.getInputStream();
- while (bytesRead < FILE_SIZE / NUM_STREAMS) {
- value = stream.read(bytes, 0, 1024);
- if (value < 0) {
- System.err.println("EOF reached");
- }
- bytesRead += value;
- }
- } catch (IOException e) {
- System.out.println("Exception in read in client " + 8080);
- if (bytesRead == 1000) {
- System.out.println("************ NOT ACTUALLY BAD");
- }
- e.printStackTrace();
- }
- // Finished download
- System.out.println("Client " + 8080 + " done");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment