graywatson

Client/Server IO

Nov 11th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.70 KB | None | 0 0
  1. package com.j256.ormlite;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.InetSocketAddress;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10.  
  11. public class Foo {
  12.  
  13.     private static long FILE_SIZE = 1000000000L;
  14.     private static int NUM_STREAMS = 8;
  15.     private static int NUM_TRANSMISSIONS = 100;
  16.  
  17.     public static void main(String[] args) throws Exception {
  18.         new Server().start();
  19.         new Client().start();
  20.     }
  21.  
  22.     public static class Server extends Thread {
  23.  
  24.         ServerSocket sSocket;
  25.  
  26.         public Server() throws Exception {
  27.             sSocket = new ServerSocket();
  28.             sSocket.bind(new InetSocketAddress(8080));
  29.         }
  30.  
  31.         @Override
  32.         public void run() {
  33.  
  34.             Socket socket;
  35.             OutputStream stream;
  36.             try {
  37.                 socket = sSocket.accept();
  38.                 stream = socket.getOutputStream();
  39.             } catch (IOException e2) {
  40.                 e2.printStackTrace();
  41.                 return;
  42.             }
  43.  
  44.             // send the data
  45.             try {
  46.                 for (int i = 0; i < NUM_TRANSMISSIONS; i++) {
  47.                     System.out.println(sSocket.getLocalPort() + " sending set " + (i + 1) + " of " + 10);
  48.                     stream.write(new byte[(int) (FILE_SIZE / NUM_STREAMS / NUM_TRANSMISSIONS)]);
  49.                     stream.flush();
  50.                 }
  51.             } catch (IOException e) {
  52.                 e.printStackTrace();
  53.                 System.err.println("Closing server (at write bytes) with socket id: " + sSocket.getLocalPort());
  54.             }
  55.  
  56.             System.out.println("Server " + sSocket.getLocalPort() + " done");
  57.  
  58.             // close the socket
  59.             try {
  60.                 stream.close();
  61.                 socket.close();
  62.                 sSocket.close();
  63.             } catch (IOException e) {
  64.                 e.printStackTrace();
  65.             }
  66.  
  67.         }
  68.     }
  69.  
  70.     public static class Client extends Thread {
  71.         @Override
  72.         public void run() {
  73.  
  74.             byte[] bytes = new byte[1024]; // number of bytes to read per client stream
  75.  
  76.             // connect to server
  77.             Socket connectionSocket;
  78.             InputStream stream;
  79.             try {
  80.                 connectionSocket = new Socket("localhost", 8080);
  81.             } catch (UnknownHostException e) {
  82.                 e.printStackTrace();
  83.                 return;
  84.             } catch (IOException e) {
  85.                 e.printStackTrace();
  86.                 return;
  87.             }
  88.  
  89.             // read file from server
  90.             int value = 0;
  91.             int bytesRead = 0;
  92.             try {
  93.                 stream = connectionSocket.getInputStream();
  94.                 while (bytesRead < FILE_SIZE / NUM_STREAMS) {
  95.                     value = stream.read(bytes, 0, 1024);
  96.                     if (value < 0) {
  97.                         System.err.println("EOF reached");
  98.                     }
  99.                     bytesRead += value;
  100.                 }
  101.             } catch (IOException e) {
  102.                 System.out.println("Exception in read in client " + 8080);
  103.                 if (bytesRead == 1000) {
  104.                     System.out.println("************ NOT ACTUALLY BAD");
  105.                 }
  106.                 e.printStackTrace();
  107.             }
  108.  
  109.             // Finished download
  110.             System.out.println("Client " + 8080 + " done");
  111.         }
  112.     }
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment