aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Problem in sending a packet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Problem in sending a packet" Watch "Problem in sending a packet" New topic
Author

Problem in sending a packet

Vardan Gupta
Greenhorn

Joined: Jun 02, 2009
Posts: 11
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
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42954
    
  73
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in sending a packet