Click here to Skip to main content
15,888,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First program which runs on my computer
import java.io.*;

import java.net.*;

public class NewUDP22   /*main class*/
{
    public static void main(String arg[])throws Exception
    {  

      /*calling both threads ReceiveDataClass and SendDataClass*/

              ReceiveDataClass t=new ReceiveDataClass();
              SendDataClass obj=new SendDataClass();
              t.start();/*starting threads here*/

              obj.start();
    }   
}

class ReceiveDataClass extends Thread
{
    byte receiveData[]=new byte[10024];
    DatagramSocket ds;
    DatagramPacket dp;
    public void run()
    {
        try{ 
            while(true)
            {
                //byte receiveData[]=new byte[1024];

                ds=new DatagramSocket(50000);
                dp=new DatagramPacket(receiveData,receiveData.length);
                ds.receive(dp);

                String str=new String(dp.getData());
                System.out.println("person1:      "+str);

                ds.close();
            }
        }

        catch(Exception e){}
    }
}

class SendDataClass extends Thread
{
    public void run()
    {
        try
        {
            while(true)
            {
                byte sendData[]=new byte[100924];

                DatagramPacket dp;
                InetSocketAddress sd=new InetSocketAddress("192.168.8.101",40000);

                DatagramSocket ds=new DatagramSocket();
                BufferedReader dis;

                dis = new BufferedReader(new InputStreamReader(System.in));

                String data=dis.readLine();

                System.out.println("me:    "+data);

                sendData=data.getBytes();

                dp=new DatagramPacket(sendData,sendData.length,sd);

                ds.send(dp);

                ds.close();
            }
        }
        catch(IOException e){}
    }
}

Here is the second program which runs on different system

import java.io.*;
import java.net.*;

public class NewUDP2   /*main class*/
{
    public static void main(String arg[])throws Exception
    {  
      /*calling both threads ReceiveDataClass and SendDataClass*/
      ReceiveDataClass t=new ReceiveDataClass();

        SendDataClass obj=new SendDataClass();
        t.start();/*starting threads here*/
        obj.start();
    }   
}

class ReceiveDataClass extends Thread
{
    byte receiveData[]=new byte[10024];
    DatagramSocket ds;
    DatagramPacket dp;

    public void run()
    {
        try
        { 
            while(true)
            {
                //byte receiveData[]=new byte[1024];
                ds=new DatagramSocket(40000);
                dp=new DatagramPacket(receiveData,receiveData.length);
                ds.receive(dp);
                String str=new String(dp.getData());
                System.out.println("person1:      "+str);

                ds.close();
           }

        }catch(Exception e){}
    }
}

class SendDataClass extends Thread
{
    public void run()
    {
        try
        {
            while(true)
            {
                byte sendData[]=new byte[100924];
                DatagramPacket dp;


                InetSocketAddress  sd=new  InetSocketAddress("192.168.8.101",50000);

                DatagramSocket ds=new DatagramSocket();

                BufferedReader dis;

                dis = new BufferedReader(new InputStreamReader(System.in));

                String data=dis.readLine();

                System.out.println("me:    "+data);

                sendData=data.getBytes();

                dp=new DatagramPacket(sendData,sendData.length,sd);

                ds.send(dp);

                ds.close();

            }
        }catch(IOException e){}
    }


What I have tried:

When I run this both program on two different system then I can send the message on second system and He is also receiving the message. But when message is being sent by Remote System to my System then I am not able to get any message, even though message has sent by Remote System. I tried all ways which I could do. This is for a project in my university.
Posted
Updated 10-Apr-17 4:16am
v2
Comments
CHill60 10-Apr-17 9:53am    
You might find out more about what is happening if you removed the empty catch blocks - never just "swallow" exceptions like this

Your system sends to this address:
Java
InetSocketAddress sd=new InetSocketAddress("192.168.8.101",40000);
The other system sends to:
Java
InetSocketAddress  sd=new  InetSocketAddress("192.168.8.101",50000);
Did you see it now? The other systems sends the data to himself on port 50000. You have to specify the IP address of your system.

Note also that for receiving it would be better to move the socket creations and closing of the sockets out of the while(true) loops.
 
Share this answer
 
all coding is correct. only i did change ip add and the important thing whenever you run a udp program just close firewall or any type of antivirus that is available in your system.
Now it is working
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900