客户端将数字发送到服务器,但服务器不会将其发送回。

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

Client sends the numbers to the server, but the server doesn't send them back

问题

以下是翻译好的内容:

客户端代码:

import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        InetSocketAddress ep = new InetSocketAddress("127.0.0.1", 1234);
        String broj1, broj2;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the 1st number: ");
        broj1 = sc.nextLine();
        System.out.println();
        System.out.print("Enter the 2nd number: ");
        broj2 = sc.nextLine();
        if (broj1.contains("[a-zA-Z+]") == true && broj2.contains("[a-zA-Z+]")) {
            System.out.println("You did not enter numbers.");
            System.exit(0);
        }
        byte[] brojJedan = new byte[256];
        byte[] brojDva = new byte[256];
        brojJedan = broj1.getBytes();
        brojDva = broj2.getBytes();
        DatagramPacket packet1 = new DatagramPacket(brojJedan, brojJedan.length, ep);
        DatagramPacket packet2 = new DatagramPacket(brojDva, brojDva.length, ep);
        try (DatagramSocket Klijent = new DatagramSocket(1000)) {
            Klijent.send(packet1);
            Klijent.send(packet2);
            byte[] rezultat = new byte[256];
            DatagramPacket packetRezultat = new DatagramPacket(rezultat, rezultat.length, ep);
            Klijent.receive(packetRezultat);
            System.out.println(new String(packetRezultat.getData(), 0, packetRezultat.getLength()));
        } catch (Exception ex) {
        }
    }
}

服务器端代码:

import javax.xml.crypto.Data;
import java.awt.*;
import java.net.*;
import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] args) {
        try {
            DatagramSocket serverSocket = new DatagramSocket(1234);
            byte[] buff1 = new byte[256];
            byte[] buff2 = new byte[256];
            byte[] buffRezultat = new byte[256];
            while (true) {
                System.out.println("Client connected.");
                DatagramPacket p1 = new DatagramPacket(buff1, buff1.length);
                DatagramPacket p2 = new DatagramPacket(buff2, buff2.length);
                serverSocket.receive(p1);
                System.out.println("p1 received");
                serverSocket.receive(p2);
                System.out.println("p2 received");
                int rezultat, brojJedan, brojDva;
                brojJedan = ByteBuffer.wrap(p1.getData()).getInt();
                brojDva = ByteBuffer.wrap(p2.getData()).getInt();
                System.out.println("byte converted to int");
                rezultat = brojJedan + brojDva;
                System.out.println("numbers added");
                buffRezultat = ByteBuffer.allocate(4).putInt(rezultat).array();
                DatagramPacket pRez = new DatagramPacket(buffRezultat, buffRezultat.length);
                serverSocket.send(pRez);
                System.out.println("result sent");
            }
        } catch (Exception ex) {
        }

    }
}
英文:

I've searched for answers, but can't quite fix the code.
I want the client to send numbers to the server, the server adds up 2 numbers and sends them back. The thing is, client sends them to the server, server adds them up (at least I think it does) but it doesn't send anything back. I'm posting Client code now:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
InetSocketAddress ep = new InetSocketAddress("127.0.0.1", 1234);
String broj1, broj2;
Scanner sc = new Scanner(System.in);
System.out.print("Unesite 1. broj: ");
broj1 = sc.nextLine();
System.out.println();
System.out.print("Unesite 2. broj: ");
broj2 = sc.nextLine();
if(broj1.contains("[a-zA-Z+]")==true&&broj2.contains("[a-zA-Z+]")){
System.out.println("Niste unijeli brojeve.");
System.exit(0);
}
byte[] brojJedan = new byte[256];
byte[] brojDva = new byte[256];
brojJedan = broj1.getBytes();
brojDva = broj2.getBytes();
DatagramPacket packet1 = new DatagramPacket(brojJedan, brojJedan.length, ep);
DatagramPacket packet2 = new DatagramPacket(brojDva, brojDva.length, ep);
try(DatagramSocket Klijent = new DatagramSocket(1000)){
Klijent.send(packet1);
Klijent.send(packet2);
byte[] rezultat = new byte[256];
DatagramPacket packetRezultat = new DatagramPacket(rezultat, rezultat.length, ep);
Klijent.receive(packetRezultat);
System.out.println(new String(packetRezultat.getData(), 0, packetRezultat.getLength()));
}catch(Exception ex){}
}
}

Server code:

import java.awt.*;
import java.net.*;
import java.nio.ByteBuffer;
public class Main{
public static void main(String[] args) {
try {
DatagramSocket serverSocket = new DatagramSocket(1234);
byte[] buff1 = new byte[256];
byte[] buff2 = new byte[256];
byte[] buffRezultat = new byte[256];
while(true){
System.out.println("Client connected.");
DatagramPacket p1 = new DatagramPacket(buff1,buff1.length);
DatagramPacket p2 = new DatagramPacket(buff2, buff2.length);
serverSocket.receive(p1);
System.out.println("p1 recieved");
serverSocket.receive(p2);
System.out.println("p2 recieved");
int rezultat, brojJedan, brojDva;
brojJedan = ByteBuffer.wrap(p1.getData()).getInt();
brojDva = ByteBuffer.wrap(p2.getData()).getInt();
System.out.println("byte converted to int");
rezultat = brojJedan+brojDva;
System.out.println("numbers added");
buffRezultat = ByteBuffer.allocate(1).putInt(rezultat).array();
DatagramPacket pRez = new DatagramPacket(buffRezultat, buffRezultat.length);
serverSocket.send(pRez);
System.out.println("res sent");
}
}catch(Exception ex){}
}
}```
</details>
# 答案1
**得分**: 2
你的代码存在一些问题:
客户端部分:
1. 在客户端将一个 `String` 放入输出的 `byte[]` 中,但在服务器端期望接收一个 `int`。
2. 在使用 `DatagramSocket` 时使用了端口 1000,实际上不应该使用任何端口。
3. 在服务器端期望从输入的 `byte[]` 中接收一个 `String`,但实际上在其中放入了一个 `int`。
服务器端部分:
1. 为了从 `byte[]` 中接收一个 `int`(32 位),你需要读取四个字节(每个字节8位)。
2. 为了通过 `byte[]` 发送一个 `int`,你需要分配四个字节的空间。
3. 从服务器发送回客户端的 `DatagramPacket` 需要知道客户端的地址和端口。
这份代码是有效的:
```java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
InetSocketAddress ep = new InetSocketAddress("127.0.0.1", 1234);
String broj1, broj2;
Scanner sc = new Scanner(System.in);
System.out.print("Unesite 1. broj: ");
broj1 = sc.nextLine();
System.out.println();
System.out.print("Unesite 2. broj: ");
broj2 = sc.nextLine();
if (broj1.contains("[a-zA-Z+]") && broj2.contains("[a-zA-Z+]")) {
System.out.println("Niste unijeli brojeve.");
System.exit(0);
}
byte[] brojJedan = ByteBuffer.allocate(4).putInt(Integer.parseInt(broj1)).array();
byte[] brojDva = ByteBuffer.allocate(4).putInt(Integer.parseInt(broj2)).array();
DatagramPacket packet1 = new DatagramPacket(brojJedan, brojJedan.length, ep);
DatagramPacket packet2 = new DatagramPacket(brojDva, brojDva.length, ep);
try (DatagramSocket Klijent = new DatagramSocket()) {
Klijent.send(packet1);
Klijent.send(packet2);
byte[] rezultat = new byte[256];
DatagramPacket packetRezultat = new DatagramPacket(rezultat, rezultat.length, ep);
Klijent.receive(packetRezultat);
System.out.println(ByteBuffer.wrap(packetRezultat.getData(), 0, 4).getInt());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;
public class Server {
public static void main(String[] args) {
try {
DatagramSocket serverSocket = new DatagramSocket(1234);
byte[] buff1 = new byte[256];
byte[] buff2 = new byte[256];
byte[] buffRezultat = new byte[256];
while (true) {
System.out.println("Client connected.");
DatagramPacket p1 = new DatagramPacket(buff1, buff1.length);
DatagramPacket p2 = new DatagramPacket(buff2, buff2.length);
serverSocket.receive(p1);
System.out.println("p1 received");
serverSocket.receive(p2);
System.out.println("p2 received");
int rezultat, brojJedan, brojDva;
brojJedan = ByteBuffer.wrap(p1.getData(), 0, 4).getInt();
brojDva = ByteBuffer.wrap(p2.getData(), 0, 4).getInt();
System.out.println("byte converted to int " + brojDva + " " + brojJedan);
rezultat = brojJedan + brojDva;
System.out.println("numbers added " + rezultat);
buffRezultat = ByteBuffer.allocate(4).putInt(rezultat).array();
DatagramPacket pRez = new DatagramPacket(buffRezultat, buffRezultat.length, p2.getAddress(), p2.getPort());
serverSocket.send(pRez);
System.out.println("res sent");
}
} catch (Exception ex) {
}
}
}

尽管这份代码在某些输入情况下可能会工作,但仍然存在一些弱点:

  1. 输入验证只检查是否使用了字母,这并不一定能确保你得到一个数字(查看这个 Stack Overflow 帖子)。

  2. 为了传输两个 int,你不需要发送两个 DatagramPacket,一个足够了。

  3. 使用标准的输入/输出流将有助于你在字节数组上进行写入和读取。

  4. 实现有意义的异常消息将有助于你编写代码并让用户使用软件。

英文:

Your code contains a number of problems:

On the client side:

  1. You are putting a String into the outbound byte[] while expecting an int on the server side.

  2. You are using port 1000 with the DatagramSocket while you shouldn't use any.

  3. You are expecting a String from the inbound byte[] while putting an int into it on the server side.

On the server side:

  1. In order to receive one int (32 bits) from a byte[] you have to read four bytes (8 bits each).

  2. In order to send one &#236;nt via a byte[] you have to allocate four bytes.

  3. The DatagramPacket you send back from the server to the client needs to know the client's address and port.

This code works:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
InetSocketAddress ep = new InetSocketAddress(&quot;127.0.0.1&quot;, 1234);
String broj1, broj2;
Scanner sc = new Scanner(System.in);
System.out.print(&quot;Unesite 1. broj: &quot;);
broj1 = sc.nextLine();
System.out.println();
System.out.print(&quot;Unesite 2. broj: &quot;);
broj2 = sc.nextLine();
if(broj1.contains(&quot;[a-zA-Z+]&quot;)&amp;&amp;broj2.contains(&quot;[a-zA-Z+]&quot;)){
System.out.println(&quot;Niste unijeli brojeve.&quot;);
System.exit(0);
}
byte[] brojJedan = ByteBuffer.allocate(4).putInt(Integer.parseInt(broj1)).array();
byte[] brojDva = ByteBuffer.allocate(4).putInt(Integer.parseInt(broj2)).array();
DatagramPacket packet1 = new DatagramPacket(brojJedan, brojJedan.length, ep);
DatagramPacket packet2 = new DatagramPacket(brojDva, brojDva.length, ep);
try(DatagramSocket Klijent = new DatagramSocket()){
Klijent.send(packet1);
Klijent.send(packet2);
byte[] rezultat = new byte[256];
DatagramPacket packetRezultat = new DatagramPacket(rezultat, rezultat.length, ep);
Klijent.receive(packetRezultat);
System.out.println(ByteBuffer.wrap(packetRezultat.getData(), 0, 4).getInt());
}catch(Exception e){
e.printStackTrace();
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;
public class Server {
public static void main(String[] args) {
try {
DatagramSocket serverSocket = new DatagramSocket(1234);
byte[] buff1 = new byte[256];
byte[] buff2 = new byte[256];
byte[] buffRezultat = new byte[256];
while (true) {
System.out.println(&quot;Client connected.&quot;);
DatagramPacket p1 = new DatagramPacket(buff1, buff1.length);
DatagramPacket p2 = new DatagramPacket(buff2, buff2.length);
serverSocket.receive(p1);
System.out.println(&quot;p1 recieved&quot;);
serverSocket.receive(p2);
System.out.println(&quot;p2 recieved&quot;);
int rezultat, brojJedan, brojDva;
brojJedan = ByteBuffer.wrap(p1.getData(), 0, 4).getInt();
brojDva = ByteBuffer.wrap(p2.getData(), 0, 4).getInt();
System.out.println(&quot;byte converted to int&quot; + brojDva + &quot; &quot; + brojJedan);
rezultat = brojJedan + brojDva;
System.out.println(&quot;numbers added &quot; + rezultat);
buffRezultat = ByteBuffer.allocate(4).putInt(rezultat).array();
DatagramPacket pRez = new DatagramPacket(buffRezultat, buffRezultat.length, p2.getAddress(), p2.getPort());
serverSocket.send(pRez);
System.out.println(&quot;res sent&quot;);
}
} catch (Exception ex) {
}
}
}

While this code would work (for certain input), it still does have a few weak spots:

  1. The input validation only checks if letters are not used. This does not necessarily give you a number (check out this SO post).

  2. You don't need to send two DatagramPacket s in order to transfer two int s; one is enough.

  3. Using standard I/O streams would help you writing to and reading from byte arrays.

  4. Implementing meaningful exception messages would help you write the code and users use the software.

huangapple
  • 本文由 发表于 2020年10月19日 03:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64417520.html
匿名

发表评论

匿名网友

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

确定