Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in sending a packet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was making a code, which can accept packets, as well as receive packets, but due to some problem i m getting packet received but unable to send packet

this is the main source code


import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class net9 extends Frame implements ActionListener,WindowListener
{
static DatagramSocket ds2;
JButton a=new JButton("Send");
JLabel b=new JLabel("Two Way Chatting");
static JTextField c=new JTextField(30);
static JTextArea d=new JTextArea(10,30);
net9() throws Exception
{

setLayout(new FlowLayout(FlowLayout.CENTER));
setBackground(Color.GREEN);
setForeground(Color.BLUE);
setSize(400,300);
setLocation(50,50);
addWindowListener(this);
add(b);
add(d);
add(c);
add(a);
a.addActionListener(this);
setVisible(true);

}


public void windowClosing(WindowEvent w)
{
System.exit(1);
}
public void windowDeiconified(WindowEvent w){}
public void windowIconified(WindowEvent w){}
public void windowClosed(WindowEvent w){}
public void windowActivated(WindowEvent w){}
public void windowDeactivated(WindowEvent w){}
public void windowOpened(WindowEvent w){}


public void actionPerformed(ActionEvent ae)

{
String z= c.getText();
byte m[]=z.getBytes();
try{
InetAddress ia =InetAddress.getLocalHost();
System.out.println(ia);

DatagramPacket dp3=new DatagramPacket(m,m.length,ia,45);
ds2.send(dp3);} catch(Exception e){}
c.setText("");
d.append("sent--"+z+'\n');
System.out.println(""+z);

}

public static void main(String args[])throws Exception
{


DatagramSocket ds2=new DatagramSocket(1);
net9 w=new net9();
while(true)

{
byte u[]=new byte[100];
DatagramPacket dp2=new DatagramPacket(u,u.length);
ds2.receive(dp2);
String g=new String(dp2.getData(),0,dp2.getLength());
System.out.println(g);
d.append("Recieved--"+g+'\n');
}
}
}


Source code of program which receive packets

//
to receive packet (U.D.P.)

Source code of program that send packet







Please help me out
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic