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.
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.
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) { } } } }
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); } }
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);
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.
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
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/ }}}
Bookmarks