UDP客户端服务器平均计算程序

huangapple go评论74阅读模式
英文:

UDP client server average computing program

问题

以下是您提供的代码的翻译部分:

我想编写一段代码客户端向服务器发送10个整数然后服务器计算这些数字的平均值然后将平均值发送回客户端我编写了服务器代码如下我是否需要使用特定的InetAddress并且在客户端和服务器之间应该更改服务器端口
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UDP_SERVER {
        
    public static final int SERVICE_PORT = 7;
    public static final int BUFSIZE = 4096;
    byte[] bu = null;
    private DatagramSocket socket;
    public UDP_SERVER(){
        try{
            
            socket = new DatagramSocket( SERVICE_PORT );
           
        }
        catch (Exception e){
            System.err.println ("无法绑定端口");
        }
    }
    public void serviceClients(){
        int sum = 0;
        byte[] buffer = new byte[BUFSIZE];
        for (;;){
            try{
                InetAddress addr = InetAddress.getLocalHost() ;
                try{
                    
                    DatagramPacket packet = new DatagramPacket(buffer, BUFSIZE);
                    socket.receive(packet);
                    ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
                    DataInputStream d1 = new DataInputStream((bin));
                    sum = sum + d1.readInt();
                    
                }
                
                catch (IOException ioe){
                    System.err.println ("错误:" + ioe);
                }
                
                double avg = sum / 10;
                String a1 = Double.toString(avg);
                bu = a1.getBytes();
                DatagramPacket Dps = new DatagramPacket(bu, bu.length,addr, SERVICE_PORT);
                socket.send(Dps);
            }
            
            catch (IOException ex){
                System.err.println ("错误:" + ex);
            }
        }
    }
    public static void main(String args[]){
        UDP_SERVER server = new UDP_SERVER();
        server.serviceClients();
    }
}

您的问题是关于InetAddress的使用。在代码中,您使用了以下方式获取本地主机的InetAddress:

InetAddress addr = InetAddress.getLocalHost();

这将获取本地主机的InetAddress,通常用于测试和开发目的。如果您要在网络上的不同主机之间进行通信,通常不应该使用此方法,因为它只会返回本地主机的InetAddress。

要进行网络通信,您应该使用服务器的实际IP地址或主机名,而不是获取本地主机的InetAddress。您可以使用服务器的IP地址或主机名来创建InetAddress对象,然后将其用于DatagramPacket的目标地址。例如:

InetAddress serverAddress = InetAddress.getByName("服务器的IP地址或主机名");
DatagramPacket Dps = new DatagramPacket(bu, bu.length, serverAddress, SERVICE_PORT);

这样,您就可以确保将数据发送到正确的服务器而不是本地主机。

英文:

I want to write a code that the client send 10 integers to the server and then the server compute the average of these number after that the server sends the average back to the client I write the server code like this , must i use specific inet address and should i change the server port between the client and server?

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UDP_SERVER {
public static final int SERVICE_PORT = 7;
public static final int BUFSIZE = 4096;
byte[] bu = null;
private DatagramSocket socket;
public UDP_SERVER(){
try{
socket = new DatagramSocket( SERVICE_PORT );
}
catch (Exception e){
System.err.println ("Unable to bind port");
}
}
public void serviceClients(){
int sum = 0;
byte[] buffer = new byte[BUFSIZE];
for (;;){
try{
InetAddress addr = InetAddress.getLocalHost() ;
try{
DatagramPacket packet = new DatagramPacket(buffer, BUFSIZE);
socket.receive(packet);
ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
DataInputStream d1 = new DataInputStream((bin));
sum = sum + d1.readInt();
}
catch (IOException ioe){
System.err.println ("Error : " + ioe);
}
double avg = sum / 10;
String a1 = Double.toString(avg);
bu = a1.getBytes();
DatagramPacket Dps = new DatagramPacket(bu, bu.length,addr, SERVICE_PORT);
socket.send(Dps);
}
catch (IOException ex){
System.err.println ("Error : " + ex);
}
}
}
public static void main(String args[]){
UDP_SERVER server = new UDP_SERVER();
server.serviceClients();
}
}

what should I do? and what inet address could I use?

答案1

得分: 0

"BindException: Address already in use" 表示另一个进程正在使用你尝试使用的端口。可能有另一个副本的你的程序正在运行,你忘记停止它了。

作为一个"快速修复",尝试使用另一个端口,例如:

public static final int SERVICE_PORT = 7777;

如果端口7没有被你的程序使用,有一些Windows工具可以帮助你找出哪个程序在使用它 - 参见 https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows

英文:

"BindException: Address already in use" means that another process is using the port you're trying to use. Could there be another copy of your program running, that you forgot to stop?

As a "quick fix", try using another port, for example:

public static final int SERVICE_PORT = 7777;

If the port 7 is not being used by your program, there are Windows tools with which you can find which program is using it - see https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows

huangapple
  • 本文由 发表于 2020年8月9日 23:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63328267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定