/* Chesapeake TTCP * Copyright 1997, Chesapeake Computer Consultants, Inc. */ import java.net.ServerSocket; import java.net.Socket; import java.net.InetAddress; import java.io.DataInputStream; import java.io.DataOutputStream; import java.util.Date; import java.io.IOException; import java.io.EOFException; import java.net.SocketException; // Handle a TCP data transfer class TransThread extends Thread { // Local Variables private long start, end; // Epoch based start and end times private String Host; private int Port; private boolean TransmitReceive; // TRANSMIT or RECEIVE private boolean Protocol; // TCP or UDP private int BufSize; private int NumBuf; private byte[] buf; private ServerSocket Server; private Socket Sock; private ParamsClass Params; private ttcpControls Controls; TransThread(ParamsClass p, ttcpControls c){ /* XXX Bogon alert! * In an ideal world, ttcpControls should not be seen by * TransThread. Until I learn of another way to do * interthread communication, we're going to see if * Controls is non-null and use it to communicate with * the GUI. If Controls is null, then we're running * without the GUI and the output goes directly to the * console. The function 'report()' does this work. */ Controls = c; Params = p; Host = p.getHost(); Port = p.getPort(); TransmitReceive = p.getTR(); Protocol = p.getProtocol(); BufSize = p.getBufSize(); NumBuf = p.getNumBuf(); buf = new byte[BufSize]; Server = null; Sock = null; } // Thread exection mainline public void run(){ int i; if (Protocol == Params.TCP){ TransTCP(); }/* else { TransUDP(); }*/ } // Transfer using TCP private void TransTCP() { int i=0; DataInputStream in = null; DataOutputStream out = null; if (TransmitReceive == Params.RECEIVE) { try { if (Server == null) { // Create receive server socket Server = new ServerSocket(Port); } report("Receive: buflen= " + BufSize + " nbuf= " + NumBuf + " port= " + Port); Sock = Server.accept(); report("Receive connection from:\n " + Sock.toString() + "."); in = new DataInputStream(Sock.getInputStream()); starttime(); for (i = 0; i < NumBuf; i++){ in.readFully(buf, 0, BufSize); } } catch (EOFException e) { report("Receive: EOF"); } catch (Exception e) { report("Receive: socket or IO error: " + e); } finally { endtime(); savestats(i); try { if (in != null) in.close(); if (Sock != null) Sock.close(); if (Server != null) Server.close(); } catch (Exception e) { report("Receive: socket close error: " + e); } in = null; Sock = null; Server = null; } } else { // TRANSMIT for (i = 0; i < BufSize; i++) { buf[i] = (byte) (i % 256); } try { report("Transmit: buflen= " + BufSize + " nbuf= " + NumBuf + " port= " + Port); Sock = new Socket(Host, Port); report("Transmit connection:\n " + Sock.toString() + "."); out = new DataOutputStream(Sock.getOutputStream()); starttime(); for (i = 0; i < NumBuf; i++) { out.write(buf, 0, BufSize); } } catch (SocketException e) { report("Transmit: no receiver found listening"); i = 0; starttime(); start--; } catch (Exception e) { report("Transmitter socket or IO error: " + e); } finally { endtime(); savestats(i); try { if (out != null) out.close(); if (Sock != null) Sock.close(); } catch (Exception e){ report("Transmitter socket close error: " + e); } out = null; Sock = null; } } } /*// Transfer using UDP private void TransUDP() { System.err.println("UDP not yet implemented."); } */ // Start and end a timed run and save stats. private void starttime() { Date date = new Date(); start = date.getTime(); } private void endtime() { Date date = new Date(); end = date.getTime(); } private void savestats(int i) { Params.setBufCount(i); Params.setStart(start); Params.setEnd(end); if (Controls != null) { Controls.transferComplete(); } } private void report(String s) { if (Controls != null) { Controls.Status.appendText(s + "\n"); } else { System.err.println(s); } } }