public static void main(String args[]) throws Exception { // Making a text file String filename = "C:\\Users\\S.S. Mehta\\Desktop\\Codes\\ComputerNetworks\\CN_project\\CN_project\\test3.txt"; makeTextFile(filename); // Reading the file and puttin it in buffer BufferedReader in = new BufferedReader(new FileReader(filename)); char[] c1 = new char[PACKET_SIZE]; c1 = readData(in); displayPacket(c1); // Step3 - making a socket , makeing a packet with inet address and sending it byte[] buffer = new byte[PACKET_SIZE]; DatagramSocket skt = new DatagramSocket(PORT_NUMBER); DatagramPacket request = new DatagramPacket(buffer, buffer.length); // stop till you receive wait(skt, request); System.out.println("On server side \nrequest received from Slient"); // making a packet with an inet address - InetAddress host = InetAddress.getByName("localhost"); DatagramPacket reply = makePacket(c1, host); // Sending reply packet System.out.println("Sending reply packet to client"); Thread.sleep(5000); skt.send(reply); // closing the socket skt.close(); }
public static void main(String args[]) { DatagramSocket skt = null; try { skt = new DatagramSocket(6789); byte[] buffer = new byte[1000]; while (true) { DatagramPacket request = new DatagramPacket(buffer, buffer.length); skt.receive(request); System.out.println("Data received from client"); System.out.println(new String(request.getData())); Thread.sleep(15000); String[] arrayMsg = (new String(request.getData())).split(" "); System.out.println(arrayMsg[0] + "server processed"); byte[] sendMsg = (arrayMsg[0] + "server processed").getBytes(); DatagramPacket reply = new DatagramPacket(sendMsg, sendMsg.length, request.getAddress(), request.getPort()); System.out.println("sending data from server to client"); Thread.sleep(15000); ; skt.send(reply); } } catch (Exception e) { } }
public static void main(String args[]) throws InterruptedException, IOException { int i, j; String serverInetAddress = "localhost"; String server1AddressString = "10.10.1.1"; InetAddress server1Address = InetAddress.getByName(server1AddressString); String server2AddressString = "10.10.2.2"; InetAddress server2Address = InetAddress.getByName(server2AddressString); String server3AddressString = "10.10.3.2"; InetAddress server3Address = InetAddress.getByName(server3AddressString); String server4AddressString = "localhost"; InetAddress server4Address = InetAddress.getByName(server4AddressString); DatagramSocket skt; { skt = new DatagramSocket(PORT_NUMBER_CLIENT); // socket used to listen and write InetAddress host = InetAddress.getByName(serverInetAddress); int serversocket = S1.PORT_NUMBER_SERVER; String msg = "Send file size"; byte[] b = msg.getBytes(); // dummy assignments - not used anywhere int filesize = 1; DatagramPacket reply, request; reply = new DatagramPacket(b, b.length, host, serversocket); request = new DatagramPacket(b, b.length, host, serversocket); for (i = 1; i <= 3; i++) { // defining a packet called request with parameters b(msg in bytes), b.length, host Internet // address and socket number if (i == 1) { host = server1Address; } else if (i == 2) { host = server2Address; } else if (i == 3) { host = server3Address; } request = new DatagramPacket(b, b.length, host, serversocket); // System.out.println("request sent from client to server"); Thread.sleep(S1.PAUSE_DURATION); // for error checks // Sending the packet- for getting the file size skt.send(request); // getting reply from // server........................................................................................ byte[] buffer = new byte [S1.PACKET_SIZE]; // apparently the size of data packet at the receiving side needs // to be bigger than the size of incoming datapacket reply = new DatagramPacket(buffer, buffer.length); // receiving packet from server - contatining filesize skt.receive(reply); // System.out.println("Response Received from server"); // System.out.println("on Client: - filesize= "+new String(reply.getData())); filesize = Integer.parseInt(new String(reply.getData()).trim()); // System.out.println("on Client: - filesize= "+filesize); Thread.sleep(S1.PAUSE_DURATION); } // here the client know the size of the file // Find the number of times it must make iterations - dividing filesize by packet_size // Request that many packets from server String[] buffer_string = new String[BUFFER_SIZE_CLIENT]; float delay[] = new float[filesize / S1.PACKET_SIZE]; System.out.println(filesize); System.out.println(S1.PACKET_SIZE); System.out.println(filesize / S1.PACKET_SIZE); Thread.sleep(2000); byte[] buffer = new byte[S1.PACKET_SIZE]; for (i = 0; i < filesize / S1.PACKET_SIZE; i++) { if (i % 100 != 0) { // System.out.print(" "+i); } else { System.out.println(" " + i); } msg = String.valueOf(i); b = msg.getBytes(); if (i % 3 == 0) { host = server1Address; } else if (i % 3 == 1) { host = server2Address; } else if (i % 3 == 2) { host = server3Address; } request = new DatagramPacket(b, b.length, host, serversocket); skt.send(request); delay[i] = System.nanoTime(); Thread.sleep(10); skt.receive(reply); delay[i] = System.nanoTime() - delay[i]; delay[i] = delay[i] / (1000000); /* if(empty_index<BUFFER_SIZE_CLIENT) { buffer_string[empty_index]=new String(reply.getData()); empty_index++; } else { for(j=0;j<BUFFER_SIZE_CLIENT-1;j++) { buffer_string[j]=buffer_string[j+1]; } buffer_string[BUFFER_SIZE_CLIENT-1]=new String(reply.getData()); }*/ // display_buffer(buffer_string); } Arrays.sort(delay); float delay2[] = new float[filesize / S1.PACKET_SIZE]; for (i = 0; i < delay2.length; i++) { delay2[i] = delay[delay.length - i - 1]; } // delay2 stores the array in descending values float[] Sk = new float[filesize / S1.PACKET_SIZE]; Sk[0] = (float) 0.0; for (i = 1; i < filesize / S1.PACKET_SIZE; i++) { for (j = 1; j <= i; j++) { Sk[i] = Sk[i] + delay2[j]; } Sk[i] = Sk[i] / (10 * i); } make_output(Sk); System.out.format( "Sk at 2=%f\n,10=%f\n,20=%f\n,100=%f\n and 30000=%f\n ", Sk[1], Sk[9], Sk[19], Sk[99], Sk[29999]); // display_buffer(buffer_string); skt.close(); } }
public static void default_fn() throws IOException { // Declaring Socket for sending and receiving data DatagramSocket skt = null; // declaring the fields to be used String client_name, movie_name, time, cost, password, reply_string; int i, j; // assigning the socket a port number skt = new DatagramSocket(6789); // defining the buffer and its size byte[] buffer = new byte[1000]; while (true) { // defining the packet to be received from client DatagramPacket request = new DatagramPacket(buffer, buffer.length); // receiving the packet skt.receive(request); // comment on the console System.out.println("Data received from client"); // Thread.sleep(5000);//for error check // converting the message to string and then splitting it into fields divided by newline // character String[] arrayMsg = (new String(request.getData())).split("\n"); client_name = arrayMsg[1]; movie_name = arrayMsg[2]; time = arrayMsg[3]; cost = "$12"; password = "******"; // Composing the reply message by appending start time,cost and password reply_string = client_name + "\n" + movie_name + "\n" + time + "\n" + cost + "\n" + password; System.out.println(reply_string); // Thread.sleep(5000); for error check // converting string message to byte byte[] sendMsg = (reply_string).getBytes(); // composing the data packet and sending it to the client's address and the same port the // client used DatagramPacket reply = new DatagramPacket(sendMsg, sendMsg.length, request.getAddress(), request.getPort()); System.out.println("sending confirmation"); // Thread.sleep(5000);//for error check // writing server log file PrintWriter writer = new PrintWriter("server_log.txt", "UTF-8"); writer.println(client_name); writer.println(movie_name); writer.println(time); writer.close(); // sending the data packet skt.send(reply); // Closing the server socket skt.close(); } }