Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in sending a packet

 
Vardan Gupta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic