让 UDPServer 发送给 UDPClient
Make UDPServer send to UDPClient
我正在尝试向客户端发送数据,但我还没有弄明白
这是我的代码。
UDP 客户端:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket datagramSocket = new DatagramSocket( );
InetAddress ipServer = InetAddress.getByName("localhost");
byte[] sendDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagram = new byte[1054];
byte[] receiveDatagramB = new byte[1055];
byte[] receiveDatagramC = new byte[1056];
while (true) {
System.out.println("Ingrese X");
String msgA = input.readLine();
System.out.println("Ingrese B");
String msgB = input.readLine();
System.out.println("Ingrese C");
String msgC = input.readLine();
sendDatagram = (msgA.getBytes());
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, ipServer, 6667);
datagramSocket.send(sendPacket);
sendDatagramB = (msgB.getBytes());
DatagramPacket sendPacketB = new DatagramPacket(sendDatagramB, sendDatagramB.length, ipServer, 6667);
datagramSocket.send(sendPacketB);
sendDatagramC = (msgC.getBytes());
DatagramPacket sendPacketC = new DatagramPacket(sendDatagramC, sendDatagramC.length, ipServer, 6667);
datagramSocket.send(sendPacketC);
}
UDP 服务器:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket datagramSockets = new DatagramSocket( );
InetAddress ipServer = InetAddress.getByName("localhost");
byte[] sendDatagrams = new byte[1054];
byte[] receiveDatagrams = new byte[1054];
DatagramSocket datagramSocket = new DatagramSocket(6667);
byte[] sendDatagram = new byte[1054];
byte[] receiveDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] receiveDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagramC = new byte[1056];
System.out.println("Server Start");
while (true){
DatagramPacket receivePacket = new DatagramPacket(receiveDatagram,receiveDatagram.length);
datagramSocket.receive(receivePacket);
String receive = new String(receivePacket.getData());
System.out.println(receive+"x");
InetAddress ipClient = receivePacket.getAddress();
int portClient = receivePacket.getPort();
receiveDatagram = new byte[1054];
String msgClient = "Hola Mundo";
DatagramPacket receivePacketB = new DatagramPacket(receiveDatagramB,receiveDatagramB.length);
datagramSocket.receive(receivePacketB);
String receiveB = new String(receivePacketB.getData());
System.out.println(receiveB+"y");
InetAddress ipClientB = receivePacketB.getAddress();
int portClientB = receivePacketB.getPort();
receiveDatagramB = new byte[1055];
String msgClientB = "Hola Mundo";
DatagramPacket receivePacketC = new DatagramPacket(receiveDatagramC,receiveDatagramB.length);
datagramSocket.receive(receivePacketC);
String receiveC = new String(receivePacketC.getData());
System.out.println(receiveC+"z");
InetAddress ipClientC = receivePacketC.getAddress();
int portClientC = receivePacketC.getPort();
receiveDatagramC = new byte[1056];
String msgClientC = "Hola Mundo";
Double x1 = (-portClientB - (Math.sqrt((portClient*portClientB - ((4*portClient*portClientC))))))/ (2 * portClientC);
Double x2 = (-portClientB + (Math.sqrt((portClient*portClientB - ((4*portClient*portClientC))))))/ (2 * portClientC);
String xa = String.valueOf(x1);
String xb = String.valueOf(x2);
System.out.println("El Resultado de x1 es = "+" "+x1);
System.out.println("El Resultado de x1 es = "+" "+x2);
}
我想将字符串 XA
和 XB
发送给客户端。
我不知道你到底在用 udp 套接字做什么,但通常服务器必须支持所有请求,包括来自客户端的协议。
这是我的 udp 套接字服务器从您的源代码中看到的样子:
package justtest;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(6667);
byte[] sendDatagram = new byte[1054];
byte[] receiveDatagram = new byte[1054];
System.out.println("Server Start");
try
{
while (true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveDatagram, receiveDatagram.length);
datagramSocket.receive(receivePacket);
//System.out.println("len" + receivePacket.getLength());
String receive = new String(receivePacket.getData());
System.out.println(receive.trim() + " received...");
//////// Do it what you want here //////////
//////// done //////////////////////////////
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String msgClient = "Hola Mundo";
System.arraycopy(msgClient.getBytes(), 0, sendDatagram, 0, msgClient.getBytes().length);
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, IPAddress, port);
datagramSocket.send(sendPacket);
}
}
finally
{
datagramSocket.close();
}
}
}
那么,udp客户端就可以做你该做的了。
package justtest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
InetAddress ipServer = InetAddress.getByName("localhost");
DatagramSocket datagramSocket = null;
DatagramSocket datagramSocketA = null;
DatagramSocket datagramSocketB = null;
byte[] sendDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagram = new byte[1054];
byte[] receiveDatagramB = new byte[1055];
byte[] receiveDatagramC = new byte[1056];
while (true)
{
System.out.println();
System.out.println("Ingrese X");
String msgA = input.readLine();
//sendDatagram = (msgA.getBytes());
System.arraycopy(msgA.getBytes(), 0, sendDatagram, 0, msgA.getBytes().length);
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, ipServer, 6667);
DatagramPacket recvPacket = new DatagramPacket(sendDatagram, receiveDatagram.length, ipServer, 6667);
datagramSocket = new DatagramSocket();
datagramSocket.send(sendPacket);
datagramSocket.receive(recvPacket);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagram).trim(), new String(recvPacket.getData()).trim());
System.out.println();
System.out.println("Ingrese B");
String msgB = input.readLine();
//sendDatagramB = (msgB.getBytes());
System.arraycopy(msgB.getBytes(), 0, sendDatagramB, 0, msgB.getBytes().length);
DatagramPacket sendPacketB = new DatagramPacket(sendDatagramB, sendDatagramB.length, ipServer, 6667);
DatagramPacket recvPacketB = new DatagramPacket(receiveDatagramB, receiveDatagramB.length, ipServer, 6667);
datagramSocketA = new DatagramSocket();
datagramSocketA.send(sendPacketB);
datagramSocketA.receive(recvPacketB);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagramB).trim(), new String(recvPacketB.getData()).trim());
System.out.println();
System.out.println("Ingrese C");
String msgC = input.readLine();
//sendDatagramC = (msgC.getBytes());
System.arraycopy(msgC.getBytes(), 0, sendDatagramC, 0, msgC.getBytes().length);
DatagramPacket sendPacketC = new DatagramPacket(sendDatagramC, sendDatagramC.length, ipServer, 6667);
DatagramPacket recvPacketC = new DatagramPacket(receiveDatagramC, receiveDatagramC.length, ipServer, 6667);
datagramSocketB = new DatagramSocket();
datagramSocketB.send(sendPacketC);
datagramSocketB.receive(recvPacketC);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagramC).trim(), new String(recvPacketC.getData()).trim());
datagramSocket.close();
datagramSocketA.close();
datagramSocketB.close();
}
}
}
我认为重要的是您将使用代码做什么。
祝你好运。
我正在尝试向客户端发送数据,但我还没有弄明白 这是我的代码。
UDP 客户端:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket datagramSocket = new DatagramSocket( );
InetAddress ipServer = InetAddress.getByName("localhost");
byte[] sendDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagram = new byte[1054];
byte[] receiveDatagramB = new byte[1055];
byte[] receiveDatagramC = new byte[1056];
while (true) {
System.out.println("Ingrese X");
String msgA = input.readLine();
System.out.println("Ingrese B");
String msgB = input.readLine();
System.out.println("Ingrese C");
String msgC = input.readLine();
sendDatagram = (msgA.getBytes());
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, ipServer, 6667);
datagramSocket.send(sendPacket);
sendDatagramB = (msgB.getBytes());
DatagramPacket sendPacketB = new DatagramPacket(sendDatagramB, sendDatagramB.length, ipServer, 6667);
datagramSocket.send(sendPacketB);
sendDatagramC = (msgC.getBytes());
DatagramPacket sendPacketC = new DatagramPacket(sendDatagramC, sendDatagramC.length, ipServer, 6667);
datagramSocket.send(sendPacketC);
}
UDP 服务器:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket datagramSockets = new DatagramSocket( );
InetAddress ipServer = InetAddress.getByName("localhost");
byte[] sendDatagrams = new byte[1054];
byte[] receiveDatagrams = new byte[1054];
DatagramSocket datagramSocket = new DatagramSocket(6667);
byte[] sendDatagram = new byte[1054];
byte[] receiveDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] receiveDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagramC = new byte[1056];
System.out.println("Server Start");
while (true){
DatagramPacket receivePacket = new DatagramPacket(receiveDatagram,receiveDatagram.length);
datagramSocket.receive(receivePacket);
String receive = new String(receivePacket.getData());
System.out.println(receive+"x");
InetAddress ipClient = receivePacket.getAddress();
int portClient = receivePacket.getPort();
receiveDatagram = new byte[1054];
String msgClient = "Hola Mundo";
DatagramPacket receivePacketB = new DatagramPacket(receiveDatagramB,receiveDatagramB.length);
datagramSocket.receive(receivePacketB);
String receiveB = new String(receivePacketB.getData());
System.out.println(receiveB+"y");
InetAddress ipClientB = receivePacketB.getAddress();
int portClientB = receivePacketB.getPort();
receiveDatagramB = new byte[1055];
String msgClientB = "Hola Mundo";
DatagramPacket receivePacketC = new DatagramPacket(receiveDatagramC,receiveDatagramB.length);
datagramSocket.receive(receivePacketC);
String receiveC = new String(receivePacketC.getData());
System.out.println(receiveC+"z");
InetAddress ipClientC = receivePacketC.getAddress();
int portClientC = receivePacketC.getPort();
receiveDatagramC = new byte[1056];
String msgClientC = "Hola Mundo";
Double x1 = (-portClientB - (Math.sqrt((portClient*portClientB - ((4*portClient*portClientC))))))/ (2 * portClientC);
Double x2 = (-portClientB + (Math.sqrt((portClient*portClientB - ((4*portClient*portClientC))))))/ (2 * portClientC);
String xa = String.valueOf(x1);
String xb = String.valueOf(x2);
System.out.println("El Resultado de x1 es = "+" "+x1);
System.out.println("El Resultado de x1 es = "+" "+x2);
}
我想将字符串 XA
和 XB
发送给客户端。
我不知道你到底在用 udp 套接字做什么,但通常服务器必须支持所有请求,包括来自客户端的协议。
这是我的 udp 套接字服务器从您的源代码中看到的样子:
package justtest;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(6667);
byte[] sendDatagram = new byte[1054];
byte[] receiveDatagram = new byte[1054];
System.out.println("Server Start");
try
{
while (true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveDatagram, receiveDatagram.length);
datagramSocket.receive(receivePacket);
//System.out.println("len" + receivePacket.getLength());
String receive = new String(receivePacket.getData());
System.out.println(receive.trim() + " received...");
//////// Do it what you want here //////////
//////// done //////////////////////////////
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String msgClient = "Hola Mundo";
System.arraycopy(msgClient.getBytes(), 0, sendDatagram, 0, msgClient.getBytes().length);
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, IPAddress, port);
datagramSocket.send(sendPacket);
}
}
finally
{
datagramSocket.close();
}
}
}
那么,udp客户端就可以做你该做的了。
package justtest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
InetAddress ipServer = InetAddress.getByName("localhost");
DatagramSocket datagramSocket = null;
DatagramSocket datagramSocketA = null;
DatagramSocket datagramSocketB = null;
byte[] sendDatagram = new byte[1054];
byte[] sendDatagramB = new byte[1055];
byte[] sendDatagramC = new byte[1056];
byte[] receiveDatagram = new byte[1054];
byte[] receiveDatagramB = new byte[1055];
byte[] receiveDatagramC = new byte[1056];
while (true)
{
System.out.println();
System.out.println("Ingrese X");
String msgA = input.readLine();
//sendDatagram = (msgA.getBytes());
System.arraycopy(msgA.getBytes(), 0, sendDatagram, 0, msgA.getBytes().length);
DatagramPacket sendPacket = new DatagramPacket(sendDatagram, sendDatagram.length, ipServer, 6667);
DatagramPacket recvPacket = new DatagramPacket(sendDatagram, receiveDatagram.length, ipServer, 6667);
datagramSocket = new DatagramSocket();
datagramSocket.send(sendPacket);
datagramSocket.receive(recvPacket);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagram).trim(), new String(recvPacket.getData()).trim());
System.out.println();
System.out.println("Ingrese B");
String msgB = input.readLine();
//sendDatagramB = (msgB.getBytes());
System.arraycopy(msgB.getBytes(), 0, sendDatagramB, 0, msgB.getBytes().length);
DatagramPacket sendPacketB = new DatagramPacket(sendDatagramB, sendDatagramB.length, ipServer, 6667);
DatagramPacket recvPacketB = new DatagramPacket(receiveDatagramB, receiveDatagramB.length, ipServer, 6667);
datagramSocketA = new DatagramSocket();
datagramSocketA.send(sendPacketB);
datagramSocketA.receive(recvPacketB);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagramB).trim(), new String(recvPacketB.getData()).trim());
System.out.println();
System.out.println("Ingrese C");
String msgC = input.readLine();
//sendDatagramC = (msgC.getBytes());
System.arraycopy(msgC.getBytes(), 0, sendDatagramC, 0, msgC.getBytes().length);
DatagramPacket sendPacketC = new DatagramPacket(sendDatagramC, sendDatagramC.length, ipServer, 6667);
DatagramPacket recvPacketC = new DatagramPacket(receiveDatagramC, receiveDatagramC.length, ipServer, 6667);
datagramSocketB = new DatagramSocket();
datagramSocketB.send(sendPacketC);
datagramSocketB.receive(recvPacketC);
System.out.printf("Send...[%s]/ recv...[%s]", new String(sendDatagramC).trim(), new String(recvPacketC.getData()).trim());
datagramSocket.close();
datagramSocketA.close();
datagramSocketB.close();
}
}
}
我认为重要的是您将使用代码做什么。
祝你好运。