Results 1 to 8 of 8

Thread: Datagram sending in Java

  1. #1
    Join Date
    Nov 2009
    Posts
    678

    Datagram sending in Java

    Hi, Can anyone tell me how to send a datagram in a network application? I had tried so much, but I am not able to achieve it, can you please help me to do this? I am waiting for your reply.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Datagram sending in Java

    Hi, use the code below to send as well as receive the datagram in java by using the UDP datagram service of java.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    
    public class RecUDPClient
    {
    	JFrame frm;
    	JPanel pnl;
    	JTextField t1, t2;
    	JTextArea rarea;
    	JScrollPane pane;
    	JLabel l1;
    	JButton b1;
    	public static void main(String[] args) 
    	{
    		RecUDPClient rudpcl = new RecUDPClient();
    	}
    	public RecUDPClient()
    	{
    		frm = new JFrame("Text Client checking");
    		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frm.setUndecorated(true);
    		frm.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    		pnl = new JPanel();
    		pnl.setLayout(null);
    		l1 = new JLabel("Desination IP:");
    		l1.setBounds(10, 20, 100, 30);
    		pnl.add(l1);
    		t1 = new JTextField(20);
    		t1.setBounds(125, 25, 150, 20);
    		pnl.add(t1);
    		l1 = new JLabel("Destination Port:");
    		l1.setBounds(10, 50, 100, 30);
    		pnl.add(l1);
    		t2 = new JTextField(10);
    		t2.setBounds(125, 55, 100, 20);
    		pnl.add(t2);
    		rarea = new JTextArea();
    		pane = new JScrollPane(rarea);
    		pane.setBounds(10, 100, 300, 200);
    		pnl.add(pane);
    		b1 = new JButton("Send");
    		b1.setBounds(235, 310, 75, 30);
    		b1.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent ae)
    			{
    				new SendRequest();
    			}
    		});
    		pnl.add(b1);
    		frm.add(pnl);
    		frm.setSize(400, 400);
    		frm.setVisible(true);
    	}
    	public class SendRequest
    	{
    		SendRequest()
    		{
    			try
    			{
    				DatagramSocket socket;
    				DatagramPacket packet;
    				InetAddress address;
    				socket = new DatagramSocket();
    				String dip = t1.getText();
    				address = InetAddress.getByName(dip);
    				String port = t2.getText();
    				int pnum = Integer.parseInt(port);
    				String mess = rarea.getText();
    				byte message[] = mess.getBytes();
    				packet = new DatagramPacket(message, message.length, address, pnum);
    				socket.send(packet);
    				rarea.setText("");
    				packet = new DatagramPacket(message, message.length);
    				socket.receive(packet);
    				String recmessage = new String(packet.getData());
    				rarea.append("Received from server: " + recmessage);
    			    	socket.close();
    			}
    			catch(IOException io)
    			{
    			}
    		}
    	}
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Datagram sending in Java

    Hi, the simple code to send a datagram in java is given below. You can get the clear vision by only looking to it. I don't think that there will be any need of explanation regarding the code. Just make use of it.
    Code:
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class Main 
    {
      public static void main(String[] argv) throws Exception 
    {
        InetAddress intaddress = InetAddress.getLocalHost();
        int port = 8080;
        byte[] outbuf = new byte[1024];
        int len = 1024;
    
        DatagramPacket request = new DatagramPacket(outbuf, len, intaddress, port);
        DatagramSocket dgsocket = new DatagramSocket();
        dgsocket.send(request);
      }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Datagram sending in Java

    Hi, I don't know the code which will help you to send datagram in java, but I know some methods and I also have some code fragment, which can be useful to you. Take a look on it.

    Code:
    DatagramConnection sender = (DatagramConnection)Connector.open("datagram://target:32767");
    
    Datagram dgram = sender.newDatagram(64);
    sender.send(dgram); 
    
    dgram = sender.newDatagram(64, "datagram://anotherHost:12345");
    sender.send(dgram); 
    
    byte[] buffer = dgram.getData( );
    String message = "Hello, world\n";
    byte[] dataBytes = message.getBytes( );
    System.arraycopy(dataBytes, 0, buffer, 0, dataBytes.
    dgram.setLength(dataBytes.length);
    sender.send(dgram);

  5. #5
    Join Date
    Jul 2010
    Posts
    7

    Re: Datagram sending in Java

    hiya just a question, if i wanted to save data from a packet how can it be done

    Thanks

  6. #6
    Join Date
    Dec 2007
    Posts
    2,291

    Re: Datagram sending in Java

    Quote Originally Posted by rav1 View Post
    hiya just a question, if i wanted to save data from a packet how can it be done

    Thanks
    You will need to download some tool called SmartSniff v1.62 to capture TCP/IP packets on your network adapter. If you want the save the captured packets for viewing them later, use "Save Packets Data To File" option from the File menu. You can also use SmartSniff with the capture driver of Microsoft Network Monitor, if it's installed on your system.

  7. #7
    Join Date
    Jul 2010
    Posts
    7

    question

    Thanks for the reply, but is there any code that I could add that would get the info, i have seen it be done before cant remember how its done

    by the way is there any way that I could recieve data as well as send data, i dnt think the above code #1 works as the connection just searches

  8. #8
    Join Date
    Jun 2006
    Posts
    623

    Re: Datagram sending in Java

    Below is a method that shows how to use pypcap and dpkt to monitor network traffic and grep with regular expression. Python 2.3 required pypcap: http://code.google.com/p/pypcap/ dpkt: http://code.google.com/p/dpkt/

    Code:
    ## {{{ http://code.activestate.com/recipes/576678/ (r1)
    #!/usr/bin/python
    # this is a simple example to sniff on port 80 for magic CAFEBABE. 
    # it has to run either sudo root on any Unix or with windows admin right. 
    # author email: pythonrocks@gmail.com. 
    import dpkt, pcap
    import re
    import sys
    
    pattern=re.compile('.*CAFEBABE.*')
    
    def __my_handler(ts,pkt,d):
    
    
        tcpPkt=dpkt.tcp.TCP(pkt)
        data=tcpPkt.data
    
        # let's find any java class pass 
        searched=pattern.search(data)
    
        if searched:
          d['hits']+=1
          print 'counters=',d['hits']
    
    pc = pcap.pcap()
    pc.setfilter('tcp and dst port 80')
    
    print 'listening on %s: %s' % (pc.name, pc.filter)
    ## end of http://code.activestate.com/recipes/576678/ }}}

Similar Threads

  1. Sending SMS with a Java application
    By Bricklayer in forum Software Development
    Replies: 8
    Last Post: 13-08-2010, 10:20 AM
  2. Java API for sending SMS
    By Ekanga in forum Software Development
    Replies: 4
    Last Post: 17-07-2010, 06:49 AM
  3. Sending a POST Request in Java
    By Sheenas in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 10:52 PM
  4. Sending SOAP Request in java
    By samualres in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 06:55 PM
  5. Problem in sending mail - java program
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-01-2010, 01:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,145,464.99065 seconds with 16 queries