Hi there,
I wrote this client/server code but i can't make it work.
and every time i try to debug the code the program goes in an infinite loop when executing the receive function.

This is the client code

package client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Client {
  private DatagramSocket socket;
  private DatagramPacket sendPacket;

  public Client(){
    try{
      socket = new DatagramSocket();
    }
    catch ( SocketException socketException ){
      socketException.printStackTrace();
      System.exit( 1 );
    }
  }
  public void run(){
    while (true){
      try{
        String s="sana";
        byte [] Sdata=s.getBytes();
        sendPacket = new DatagramPacket( Sdata,
            Sdata.length, InetAddress.getLocalHost(), 5000 );
        socket.send(sendPacket);
        ReceivePackets();
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }
  }
  public void ReceivePackets(){
    try{
      byte Rdata[] = new byte[ 100 ];
      DatagramPacket receivePacket = new DatagramPacket(
          Rdata, Rdata.length );
      socket.receive( receivePacket );
      System.out.println(receivePacket.getData());
    }
    catch(IOException e ){
      e.printStackTrace();
    }

  }
}

This is the server code

package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Server {
  private DatagramSocket socket;
  public Server(){
    try{
      socket =new DatagramSocket (5000);
    }
    catch(SocketException e){
      e.printStackTrace();
    }
  }
  public void run(){
    while(true){
      try{
        byte [] Rdata=new byte[500];
        DatagramPacket receivePacket =new DatagramPacket( Rdata, Rdata.length );
        socket.receive( receivePacket );
        System.out.println(receivePacket.getAddress());
        sendPacket( receivePacket );
      }
      catch(IOException e ){
        e.printStackTrace();
      }
    }
  }
  private void sendPacket(DatagramPacket receivePacket)
  {
    try{
      DatagramPacket sendPacket = new DatagramPacket(
          receivePacket.getData(), receivePacket.getLength(),
          receivePacket.getAddress(), receivePacket.getPort());
      socket.send( sendPacket );
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }
}

how to make it work?

Recommended Answers

All 5 Replies

Where is the receive "function" or do you mean method?
Where is the infinite loop that the code enters?
The only loops I see are ones that you have coded as infinite loops: while(true).
If you don't want those loop to be infinite, provide a way for them to end.

i mean the receive function
and about the infinite loop,i said when i'm trying to debug the code the execution goes into infinite loop at the receive function without any exception or error, and never execute the next line which is System.out.println

i mean the receive function

Sorry, in java there are no functions?
Is receive the name of the method? What line of the posted code are you talking about?

the code the execution goes into infinite loop

Where in your code is the loop that you are talking about?
I see two forever loops in your code. They both would be infinite loops because there is no way for the execution to leave the loops.

Do you mean that the receive() method "blocks" waiting for input? Have you read the API doc for the receive method? It says the method will block if....

You could try setting a timeout before the receive ( setSoTimeout(2000) ) and see if your code continues as expected after the timeout elapses (catches a java.net.SocketTimeoutException) - this would confirm that your code is OK but you are not receiving any UDP packets on your port (maybe a firewall or other network configuration problem)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.