I am using the Netbeans IDE trying to make a UDP connection between a client and a server, it is a simple program that UDPClient sends a String to a UDPServer , and the server uses and sends it back to the client. I made client and server sides in separated projects.
my class code for client UDPClient:
package udpclient;
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException{
BufferedReader user_in = new BufferedReader(
new InputStreamReader(System.in));
DatagramSocket socket = new DatagramSocket();
byte[] inData = new byte[1024];
byte[] outData = new byte[1024];
InetAddress IP = InetAddress.getByName("localhost");
System.out.println("Enter Data to send to server: ");
outData = user_in.readLine().getBytes();
DatagramPacket sendPkt = new DatagramPacket(outData, outData.length, IP, 9876);
socket.send(sendPkt);
DatagramPacket recievePkt = new DatagramPacket(inData, inData.length);
socket.receive(recievePkt);
System.out.println("Replay from Server: "+recievePkt.getData());
}
}
and my server side class UDPServer:
package udpserver;
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws IOException{
DatagramSocket socket = new DatagramSocket();
byte[] inServer = new byte[1024];
byte[] outServer = new byte[1024];
DatagramPacket rcvPkt = new DatagramPacket(inServer,inServer.length);
socket.receive(rcvPkt);
System.out.println("Packet Received!");
InetAddress IP = rcvPkt.getAddress();
int port = rcvPkt.getPort();
String temp = new String(rcvPkt.getData());
temp = temp.toUpperCase();
outServer = temp.getBytes();
DatagramPacket sndPkt = new DatagramPacket(outServer, outServer.length, IP, port);
socket.send(sndPkt);
}
}
make in the counter that the program is working properly and does not produce errors. the server does not receive the packet at all, it does not interact with the client. why did this happen?
source
share