public Cedars(String args[]) throws ArchiveException, IOException, HoneycombTestException { verbose = false; parseArgs(args); initHCClient(host); // generate lists of random sizes around 30M and 3M // sort ascending to allow continuous expansion try { initRandom(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } sizes = new long[n_files]; for (int i = 0; i < sizes.length; i++) { sizes[i] = MIN_SIZE + (long) (rand.nextDouble() * (double) RANGE); } Arrays.sort(sizes); sizes2 = new long[n_files]; for (int i = 0; i < sizes2.length; i++) { sizes2[i] = MIN_SIZE2 + (long) (rand.nextDouble() * (double) RANGE2); } Arrays.sort(sizes2); sizes3 = new long[n_files]; for (int i = 0; i < sizes3.length; i++) { sizes3[i] = MIN_SIZE3 + (long) (rand.nextDouble() * (double) RANGE3); } Arrays.sort(sizes3); oids = new String[n_files]; Arrays.fill(oids, null); shas = new String[n_files]; Arrays.fill(shas, null); if (out_file != null) { try { String host = clnthost; fo = new FileWriter(out_file, true); // append=true flog("#S Cedars [" + host + "] " + new Date() + "\n"); } catch (Exception e) { System.err.println("Opening " + out_file); e.printStackTrace(); System.exit(1); } } Runtime.getRuntime().addShutdownHook(new Thread(new Shutdown(), "Shutdown")); doIt(); done = true; }
@Override public void run() { try { // Create a server socket // DatagramSocket socket = new DatagramSocket(8000); textArea.append("UDP connnection listener started at " + new Date() + '\n'); // while (true) { // The byte array for sending and receiving datagram packets byte[] buffer = new byte[BUFFER_SIZE]; // Initialize buffer for each iteration Arrays.fill(buffer, (byte) 0); // Create a packet for receiving data // DatagramPacket receivePacket = new DatagramPacket(buffer.clone(), buffer.length); // Receive radius from the client in a packet // socket.receive(receivePacket); // Create a new thread for the UDP connection HandleUDPClient task = new HandleUDPClient( new DatagramSocket(8000), new DatagramPacket(buffer.clone(), buffer.length)); // Start the new thread // new Thread(task).start(); task.run(); // Increment clientNo clientNo++; // break; // } } catch (BindException ex) { // Not supporting multiple UDP clients for now textArea.append("Multiple UDP client connections are currently not supported.\n"); } catch (IOException ex) { ex.printStackTrace(); } }
@Override public void run() { try { // Create a packet for sending data DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length); OutputStream outputStream = null; String fileName = ""; boolean createFile = true; int bytesReceived = 0; long totalBytesReceived = 0; long fileSize = 0; socket.receive(receivePacket); // Display the client number textArea.append("Starting thread for UDP client " + clientNo + " at " + new Date() + '\n'); textArea.append( "The client host name is " + receivePacket.getAddress().getHostName() + " and port number is " + receivePacket.getPort() + '\n'); // Continuously serve the client while (true) { bytesReceived = receivePacket.getLength(); if (bytesReceived > 0) { // Get the file transmission header from the initial client packet String transmitHeader = new String(receivePacket.getData(), 0, bytesReceived); // transmitHeader = transmitHeader.substring(0, bytesReceived).trim().; String[] header = transmitHeader.split(HEADER_DEL); fileSize = Long.parseLong(header[0]); fileName = header[1]; // Send receipt acknowledgment back to the client. Just send back the number of bytes // received. setSendPacketAddress(sendPacket, receivePacket); sendPacket.setData(String.valueOf(bytesReceived).getBytes()); socket.send(sendPacket); } while (totalBytesReceived < fileSize) { // Wait for client to send bytes // socket.setSendBufferSize(BUFFER_SIZE); socket.receive(receivePacket); bytesReceived = receivePacket.getLength(); if (totalBytesReceived == 0) { if (createFile) { // Get a unique name for the file to be received // fileName = getUniqueFileName(); fileName = textFolder.getText() + fileName; outputStream = createFile(fileName); createFile = false; textArea.append("Receiving file from client.\n"); } // Write bytes to file outputStream.write(receivePacket.getData(), 0, bytesReceived); } else { if (outputStream != null) { // Write bytes to file, if any outputStream.write(receivePacket.getData(), 0, bytesReceived); } } // Increment total bytes received totalBytesReceived += bytesReceived; // Tell the client to send more data. Just send back the number of bytes received. sendPacket.setData(String.valueOf(bytesReceived).getBytes()); socket.send(sendPacket); // buffer = new byte[BUFFER_SIZE]; Arrays.fill(buffer, (byte) 0); receivePacket = new DatagramPacket(buffer, buffer.length); } outputStream.flush(); outputStream.close(); textArea.append("Received file successfully. Saved as " + fileName + "\n"); // Tell the client transmission is complete. Just send back the total number of bytes // received. sendPacket.setData(String.valueOf(totalBytesReceived).getBytes()); socket.send(sendPacket); // Reset creation flag createFile = true; totalBytesReceived = 0; // Wait for client to send another file socket.receive(receivePacket); } } catch (IOException e) { System.err.println(e); } }
public static void main(String[] args) throws Exception { //////////////////// STAGE A////////////////////////////////////////// int portNum = 12235; // InetAddress ip=InetAddress.getLocalHost(); InetAddress ip = InetAddress.getByName("attu2.cs.washington.edu"); DatagramSocket sock = new DatagramSocket(); byte[] response = new byte[HEADER_SIZE + 16]; String partA = "hello world\0"; byte[] buf = createBuffer(partA.getBytes(), 0, STEP1); DatagramPacket packet = new DatagramPacket(buf, buf.length, ip, portNum); sock.send(packet); packet = new DatagramPacket(response, response.length); sock.receive(packet); int[] a2 = partA(response); // putting the 4 ints from server into int[] // for (int i = 0; i < a2.length; i++) { // System.out.println(a2[i]); // } ////////////////////// STAGE B//////////////////////////////////////// int numSent = 0; int send = a2[0]; int len = a2[1]; portNum = a2[2]; int secretA = a2[3]; System.out.println("The secrets:\nA: " + secretA); sock.setSoTimeout(500); // .5s while (numSent < send) { // create packet buf = createBuffer(partB(numSent, len), secretA, STEP1); packet = new DatagramPacket(buf, buf.length, ip, portNum); sock.send(packet); // send packet try { sock.receive(new DatagramPacket(response, response.length)); numSent++; ByteBuffer temp = ByteBuffer.wrap(response); checkHeader(temp, 4, secretA, STEP1); // System.out.println(temp.getInt()); // For debug. See if counts up by 1 } catch (SocketTimeoutException e) { // if there's a timeout, try again continue; } } response = new byte[HEADER_SIZE + 8]; // 8 bytes -- 2 integers sock.receive(new DatagramPacket(response, response.length)); ByteBuffer bb = ByteBuffer.wrap(response); // Header checkHeader(bb, 8, secretA, STEP2); // reset the port number to the one given portNum = bb.getInt(); int secretB = bb.getInt(); System.out.println("B: " + secretB); // close the UDP socket sock.close(); /////////////////////////// STAGE C/////////////////////////////////// Socket socket = new Socket(ip, portNum); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); response = new byte[HEADER_SIZE + 16]; // 4 ints given to us this time in.read(response); bb = ByteBuffer.wrap(response); // Header checkHeader(bb, 13, secretB, STEP2); // num2 len2 secretC and char c numSent = bb.getInt(); len = bb.getInt(); int secretC = bb.getInt(); System.out.println("C: " + secretC); // stage d byte c = (byte) bb.getChar(); buf = new byte[len]; Arrays.fill(buf, c); for (int i = 0; i < numSent; i++) { byte[] b = createBuffer(buf, secretC, STEP1); out.write(b); } response = new byte[12 + 4]; // one integer. secretD plus header in.read(response); bb = ByteBuffer.wrap(response); checkHeader(bb, 4, secretC, STEP2); int secretD = bb.getInt(); socket.close(); in.close(); out.close(); System.out.println("D: " + secretD); }
/** * Logs the specified message and details to the packet logging service if enabled. * * @param message the message to log * @param sender determines whether we are the origin of this message. */ private void logPacket(SIPMessage message, boolean sender) { try { PacketLoggingService packetLogging = SipActivator.getPacketLogging(); if (packetLogging == null || !packetLogging.isLoggingEnabled(PacketLoggingService.ProtocolName.SIP) /* Via not present in CRLF packet on TCP - causes NPE */ || message.getTopmostVia() == null) return; String transport = message.getTopmostVia().getTransport(); boolean isTransportUDP = transport.equalsIgnoreCase("UDP"); byte[] srcAddr; int srcPort; byte[] dstAddr; int dstPort; // if addresses are not set use empty byte array with length // equals to the other address or just empty // byte array with length 4 (ipv4 0.0.0.0) if (sender) { if (!isTransportUDP) { InetSocketAddress localAddress = getLocalAddressForDestination( message.getRemoteAddress(), message.getRemotePort(), message.getLocalAddress(), transport); srcPort = localAddress.getPort(); srcAddr = localAddress.getAddress().getAddress(); } else { srcPort = message.getLocalPort(); if (message.getLocalAddress() != null) srcAddr = message.getLocalAddress().getAddress(); else if (message.getRemoteAddress() != null) srcAddr = new byte[message.getRemoteAddress().getAddress().length]; else srcAddr = new byte[4]; } dstPort = message.getRemotePort(); if (message.getRemoteAddress() != null) dstAddr = message.getRemoteAddress().getAddress(); else dstAddr = new byte[srcAddr.length]; } else { if (!isTransportUDP) { InetSocketAddress dstAddress = getLocalAddressForDestination( message.getRemoteAddress(), message.getRemotePort(), message.getLocalAddress(), transport); dstPort = dstAddress.getPort(); dstAddr = dstAddress.getAddress().getAddress(); } else { dstPort = message.getLocalPort(); if (message.getLocalAddress() != null) dstAddr = message.getLocalAddress().getAddress(); else if (message.getRemoteAddress() != null) dstAddr = new byte[message.getRemoteAddress().getAddress().length]; else dstAddr = new byte[4]; } srcPort = message.getRemotePort(); if (message.getRemoteAddress() != null) srcAddr = message.getRemoteAddress().getAddress(); else srcAddr = new byte[dstAddr.length]; } byte[] msg = null; if (message instanceof SIPRequest) { SIPRequest req = (SIPRequest) message; if (req.getMethod().equals(SIPRequest.MESSAGE) && message.getContentTypeHeader() != null && message.getContentTypeHeader().getContentType().equalsIgnoreCase("text")) { int len = req.getContentLength().getContentLength(); if (len > 0) { SIPRequest newReq = (SIPRequest) req.clone(); byte[] newContent = new byte[len]; Arrays.fill(newContent, (byte) '.'); newReq.setMessageContent(newContent); msg = newReq.toString().getBytes("UTF-8"); } } } if (msg == null) { msg = message.toString().getBytes("UTF-8"); } packetLogging.logPacket( PacketLoggingService.ProtocolName.SIP, srcAddr, srcPort, dstAddr, dstPort, isTransportUDP ? PacketLoggingService.TransportName.UDP : PacketLoggingService.TransportName.TCP, sender, msg); } catch (Throwable e) { logger.error("Cannot obtain message body", e); } }