0

I have a homework, in which we should implement a server/client classes, so that we send 1400Bytes packets to the server and we get them back as reply. We must mess the throughput in kbit/s and it must be printed when there a timeout by the server! but the problem is that I'm calculating the throughput in the Client-Class, and trying to print it in the 'catch block' of the timeout-exception in the Server-Class, but this value is always transmitted/printed as 0.0, which is not the case when I print it in the Client-Class! I tried with simple static variable, with static get-method, but it doesnt work! Can anyone help me please? I must give it back today at 00:00! It Would be great! Thanks!

package blatt6;

import java.io.*;
import java.net.*;

public class UDPClient
{
static double startTime;
static double endTime;
static double dauer; 
static double paketGroesseKBit = (1400*8) / 1024;
private static double durchsatz;


public static void main(String args[])
{
    DatagramSocket sock = null;
    int port = 7777;
    String s = "";

    for (int i=0; i<1400; i++) {
        s = s + 'b';
    }

    //BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));

    try
    {
        sock = new DatagramSocket();
        InetAddress host = InetAddress.getByName("localhost");

        int j=0;
        while(j<4)
        {
            //take input and send the packet
            echo("Enter message to send : ");
            //s = (String)cin.readLine();

            byte[] b = s.getBytes();    
            DatagramPacket  dp = new DatagramPacket(b , b.length , host , port);    
            byte[] buffer = new byte[1400];
            DatagramPacket reply = new DatagramPacket(buffer, buffer.length);

            sock.send(dp);
            startTime = System.nanoTime();      
            //buffer to receive incoming data
            sock.receive(reply);
            endTime = System.nanoTime();
            dauer = endTime - startTime;
            durchsatz = paketGroesseKBit / ((dauer/2) * Math.pow(10, -9));
            //System.out.println(dauer);

            j++;        
//              byte[] data = reply.getData();
//              s = new String(data, 0, reply.getLength());
//              
//              //echo the details of incoming data - client ip : client port - client message
//              echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
        }
    }

    catch(IOException e)
    {
        System.err.println("IOException " + e);
    }
}

//simple function to echo data to terminal
public static void echo(String msg)
{
    System.out.println(msg);
}

public static double getDurchsatz() {
    return durchsatz;
}
}

Here Server:

package blatt6;

import java.io.*;
import java.net.*;

public class UDPServer
{

static double durchy = UDPClient.getDurchsatz();

public static void main(String args[])
{
    DatagramSocket sock = null;
    int timeout = 5000;

    try
    {
        //1. creating a server socket, parameter is local port number
        sock = new DatagramSocket(7777);
        sock.setSoTimeout(timeout);
        //buffer to receive incoming data
        byte[] buffer = new byte[1400];

        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

        //2. Wait for an incoming data
        echo("Server socket created. Waiting for incoming data...");

        //communication loop
        while(true)
        {
            sock.receive(incoming);
            sock.send(incoming);                

//                byte[] data = incoming.getData();
//                String s = new String(data, 0, incoming.getLength());
//
//                //echo the details of incoming data - client ip : client port - client message
//                echo(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " - " + s);       
//                s = "OK : " + s;
//
//                DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
//                sock.send(dp);
        }
    }

    catch(IOException e)
    {
        System.out.println(durchy);
        System.err.println("IOException " + e);
    }
}

//simple function to echo data to terminal
public static void echo(String msg)
{
    System.out.println(msg);
}
}
4

2 回答 2

1

服务器和客户端都在同一个项目中并不重要,因为您正在运行两个实例(使用 eclipse 的播放按钮两次)它们不共享任何东西。

如果服务器和客户端在同一程序的不同线程中,它们可以共享属性,但这对您的作业(或我能想到的任何应用程序)没有意义。

正如我之前的评论中所述,您应该计算客户端和服务器的吞吐量。

您可以制作第三类,即吞吐量计算器,以保持通用逻辑。然后客户端和服务器都可以重用代码。请记住,在执行时,它们将执行相同代码的不同副本,因此它们不会共享值。

于 2012-12-01T18:51:28.853 回答
0

好吧,我看到的一个明显错误是您使用静态初始化程序UDPClient.getDurchsatz()从服务器类调用。这是可行的,因为这两个类在同一个包中,但它也不起作用,因为该调用是在服务器类中的任何非静态初始化程序代码执行之前进行的。因此,您正在读取默认值,即 adouble0. 如果您将静态初始化程序添加到客户端类以将值设置为非零值,则应该改为获取该值。

在这样的事情有任何工作希望之前,您需要从字段中删除静态初始化,而是在需要时向客户端类询问值。

但是,即使没有详细查看您的代码,我认为您也需要进行一些架构更改。计算吞吐量的正常方法是计算(一组)发送或接收调用的时间,然后根据传输的数据量和经过的时间计算吞吐量。这可以在客户端或服务器端完成,您不需要在两者之间建立单独的通信通道来做到这一点。

于 2012-12-01T19:02:57.147 回答