I have been working on this program since last three-four days..
But i am not able find the error in it...
plz help..
What i am trying to do in this program is , client-server connection & data
transfer using DatagramSocket , DatagramPacket........
See..
my receiver.java file is not able to receive the packets from my
sender.java when executed
*this is my client code .....*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stopwait;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author sonawanesd
*/
public class Receiver
{
static String string_ack = new String("Sunil");
public static void main(String args[])
{
try
{
byte[] b = new byte[8];
//boolean finished = false;
byte[]ack = string_ack.getBytes();
int i = 0;
//byte[] send_data = new byte[1024];
//byte[] receive_data = new byte[1024];
InetAddress IPAddress = InetAddress.getLocalHost();
DatagramPacket dgp = new DatagramPacket(b,
b.length,InetAddress.getByName("sunil"), 9876);
//DataInputStream dis = new DataInputStream();
DatagramSocket dgs = new DatagramSocket();
//dgs.connect(InetAddress.getLocalHost(),2000);
//dgp1.setData(ack);
while(i<8)
{
System.out.println("Server Started");
Thread.sleep(4000);
dgs.receive(dgp);
System.out.println("Receiving Data packets"+"
"+dgp.toString());
DatagramPacket dgp1 = new DatagramPacket(ack,
ack.length,InetAddress.getByName("sunil"), 9876);
dgs.send(dgp1);
// try
// {
// }
// catch(Exception e)
// {
// e.printStackTrace();
// }
i++;
//}
}
dgs.close();
}
catch (Exception ex)
{
//Logger.getLogger(Sender.class.getName()).log(Level.SEVERE,
null, ex);
}}
}
*this is my server code*
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stopwait;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author sonawanesd */
public class Sender
{
static int flag=0;
static byte []check_ack = {0};
public static void main(String args[])
{
try
{
byte [] b = {1,2,3,4,5,6,7,8};
int i=0,j=0;
InetAddress IPAddress = InetAddress.getLocalHost();
DatagramPacket dgp1 = new DatagramPacket(check_ack,
check_ack.length,InetAddress.getByName("sunil"),9876);
//DataInputStream dis = new DataInputStream();
DatagramSocket dgs = new
DatagramSocket(9876,InetAddress.getLocalHost());
//dgs.connect(InetAddress.getLocalHost(),2000);
/*if(dgs.isConnected())
{
System.out.println("Working");
}*/
while(i<8)
{
Thread.sleep(2000);
DatagramPacket dgp = new DatagramPacket(b, b.length,
InetAddress.getByName("sunil"), 9876);
dgs.send(dgp);
System.out.println("Sending the data Packets"+"
"+dgp.toString());
// try
//{
// }
//catch (InterruptedException ex)
//{
//
Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
//}
//byte buf = 1;
dgs.receive(dgp1);
//dgp1.setData(b,0,1);
String www = new String(dgp1.getData());
//System.out.println(check_ack.length);
if(www.equals("sunil"))
{
System.out.println("Acknowledment Receieved");
//flag = 1;
}
else
{
System.out.println("Acknowledment NOT Receieved");
}
i++;
//}
//dgs.close();
}
}
catch (Exception ex)
{
Logger.getLogger(Sender.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
*Thanks in Advance.........*
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---