我已经搜索了答案,但无法完全解决该代码。 我希望客户端将数字发送到服务器,服务器将2个数字加起来并发回。问题是,客户端将它们发送到服务器,服务器将它们加起来(至少我认为是这样),但是它什么也没有发回。我现在正在发布客户端代码:
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){}
}
}
服务器代码:
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){}
}
}```
答案 0 :(得分:2)
您的代码包含许多问题:
在客户端:
您正在将String
放入出站byte[]
,同时期望服务器端有int
。
您正在将端口1000与DatagramSocket
一起使用,而不应该使用任何端口。
您期望从入站String
获得byte[]
,同时在服务器端将int
放入其中。
在服务器端:
要从int
接收一个byte[]
(32位),您必须读取四个字节(每个8位)。
要通过ìnt
发送一个byte[]
,您必须分配四个字节。
从服务器发送回客户端的DatagramPacket
需要知道客户端的地址和端口。
此代码有效:
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 recieved");
serverSocket.receive(p2);
System.out.println("p2 recieved");
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) {
}
}
}
虽然此代码可以工作(对于某些输入),但它仍然存在一些弱点:
输入验证仅检查是否不使用字母。这并不一定给您提供电话号码(请查看此post)。
您无需发送两个DatagramPacket
即可传输两个int
;一个就够了。
使用标准I / O流将帮助您写入和读取字节数组。
实施有意义的异常消息将帮助您编写代码和用户使用该软件。