简体   繁体   English

Java静态变量值无法传输

[英]Java Static variable value can't not be transmitted

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. 我有一个家庭作业,在其中我们应该实现服务器/客户端类,以便我们将1400Bytes数据包发送到服务器,然后将其作为答复返回。 We must mess the throughput in kbit/s and it must be printed when there a timeout by the server! 我们必须弄乱吞吐量(以kbit / s为单位),并且服务器超时时必须将其打印出来! 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! 但是问题是我正在计算客户端类中的吞吐量,并尝试在服务器类中的timeout-exception的“ catch块”中打印它,但是此值始终以0.0传输/打印,在“客户级”中打印时情况并非如此! I tried with simple static variable, with static get-method, but it doesnt work! 我尝试使用简单的静态变量和静态get-method,但是它不起作用! Can anyone help me please? 谁能帮我吗? I must give it back today at 00:00! 我今天必须在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);
}
}

It doesn't matter both server and client are in the same project, since you are running two instances (using eclipse's play button twice) they not share anything. 服务器和客户端都在同一个项目中都没关系,因为您正在运行两个实例(两次使用eclipse的播放按钮),它们不共享任何内容。

They could share attributes if server and client where different threads of the same program, but it doesn't make sense for you homework (or any application I can think of). 如果服务器和客户端在同一程序的不同线程中,则它们可以共享属性,但这对您的家庭作业(或我能想到的任何应用程序)没有意义。

As stated in my previous comment you should calculate throughput both in client and server. 如我先前的评论所述,您应该计算客户端和服务器中的吞吐量。

You could make a third class, a throughput calculator , that keeps the common logic. 您可以创建第三类,即吞吐量计算器 ,它保留了常见的逻辑。 Then both client and server can reuse the code. 然后,客户端和服务器都可以重用代码。 Keep in mind that at execution time they will be executing different copies of the same code, so they will not share values. 请记住,在执行时,他们将执行同一代码的不同副本,因此它们将不会共享值。

Well, one obvious error that I see is that you are using a static initializer to call UDPClient.getDurchsatz() from the server class. 好吧,我看到的一个明显的错误是您正在使用静态初始化程序从服务器类中调用UDPClient.getDurchsatz() This works, because the two classes are in the same package, but it also does not work because that call is made before any non-static-initializer code in the server class is executed. 之所以可行,是因为这两个类在同一个包中,但是它也不起作用,因为该调用是在服务器类中的任何非静态初始化程序代码执行之前进行的。 Hence, you're reading the default value, which for a double is 0 . 因此,您正在读取默认值,该值的double0 If you add a static initializer to the client class to set the value to something non-zero, you should get that value instead. 如果将静态初始化程序添加到客户端类以将该值设置为非零值,则应该获取该值。

Before something like this has any hope of working, you'll need to remove the static initialization from the field and instead ask the client class for the value when you need it. 在类似这样的东西无法起作用之前,您需要从字段中删除静态初始化, 而是在需要时向客户端类询问该值。

However, even without having looked at your code in detail, I think you'll also need some architectural changes. 但是,即使没有详细研究代码,我认为您也需要进行一些体系结构更改。 The normal way to calculate throughput is to time a (set of) send or receive call(s) where it is/they are made, and then calculate the throughput based on the amount of data transferred and elapsed time. 计算吞吐量的通常方法是在发送/接收呼叫的时间(一组)进行呼叫,然后根据传输的数据量和经过的时间计算吞吐量。 That can be done either on the client or server side, and you don't need a separate communications channel between the two to do that. 可以在客户端或服务器端完成此操作,并且您不需要在两者之间使用单独的通信通道来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM