Hi
I have a code written for multicasting between client and server. On running this demo, The client receives only a few packets and some packets are dropped. Instead I want to ensure that all the packets are received using multicastsocket. Is it possible to implement TCP for connection establishment and then use multicastsocket. Do we have necessity of creating multiple threads for this..
Pls HELP!!!!
import java.io.*;
import java.net.*;
import javax.swing.*;
public class QuoteMultiClient1
{
public static JButton B_CONNECT = new JButton("CONNECT");
public static JFrame MainWindow=new JFrame("DatagamQuote");
public static JScrollPane SP_OUTPUT=new JScrollPane();
public static JTextArea TA_OUTPUT=new JTextArea();
static int time=5000;
public static void main(String[] args) throws Exception
{
BuildGui();
}
public static void CONNECT()
{
try
{
InetAddress IP_ADDRESS=InetAddress.getByName("224.0.0.0");
byte[] bugf=new byte[256];
//DatagramSocket dgsoc=new DatagramSocket(208,IP_ADDRESS);
DatagramPacket PACKET2=new DatagramPacket(bugf,bugf.length);
//dgsoc.receive(PACKET2);
MulticastSocket SOCK=new MulticastSocket(208);
//InetAddress IP_ADDRESS=InetAddress.getByName("224.0.0.0");
SOCK.joinGroup(IP_ADDRESS);
SOCK.receive(PACKET2);
System.out.println("received control packet");
DatagramPacket PACKET;
//DatagramPacket ACK;
for(int i=0;i<6;i++)
{
byte[] buf = new byte[600];
PACKET = new DatagramPacket(buf,buf.length);
SOCK.receive(PACKET);
String MESSAGE=new String(PACKET.getData(),0,PACKET.getLength());
TA_OUTPUT.append("\n \n Todays Quote" + MESSAGE);
}
//DatagramSocket sock=new DatagramSocket(209);
byte[] bufack=new byte[256];
String ack="Received few Quotes";
bufack=ack.getBytes();
DatagramPacket ackpkt=new DatagramPacket(bufack,bufack.length,IP_ADDRESS,108 0);
SOCK.send(ackpkt);
System.out.println("Client-end- ack sent");
SOCK.leaveGroup(IP_ADDRESS);
SOCK.close();
}
catch(IOException X)
{
System.out.println(X);
}
}
public static void BuildGui()
{
MainWindow.setSize(410,280);
MainWindow.setLocation(200,200);
MainWindow.setResizable(false);
MainWindow.setBackground(new java.awt.Color(255,255,255));
MainWindow.getContentPane().setLayout(null);
B_CONNECT.setBackground(new java.awt.Color(0,0,255));
B_CONNECT.setForeground(new java.awt.Color(255,255,255));
MainWindow.getContentPane().add(B_CONNECT);
B_CONNECT.setBounds(150,15,110,25);
TA_OUTPUT.setLineWrap(true);
SP_OUTPUT.setHorizontalScrollBarPolicy(ScrollPaneC onstants.HORIZONTAL_SCROLLBAR_NEVER);
SP_OUTPUT.setVerticalScrollBarPolicy(ScrollPaneCon stants.VERTICAL_SCROLLBAR_ALWAYS);
SP_OUTPUT.setViewportView(TA_OUTPUT);
MainWindow.getContentPane().add(SP_OUTPUT);
SP_OUTPUT.setBounds(10,45,380,180);
//MainWindow_Action();
MainWindow.setVisible(true);
MainWindow_Action();
}
public static void MainWindow_Action()
{
B_CONNECT.addActionListener(
new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
CONNECT();
}
}
);
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class QuoteMultiServer1 extends Thread {
protected DatagramSocket SOCK=null;
//protected DatagramSocket SOCK2=null;
public int QuoteCount=0;
public int Time_Interval=3000;
public int Time_Interval2=5000;
InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
//InetAddress IP_CLIENT_ADDRESS=InetAddress.getByName("224.0.0.0 ");
public String[] PirateQuote={ "dcsfffffffffffffffffff",
"deeeeeeeeeew", "fhdfkhjhhhhhhhhhhhhhhhhhhhhhhh",
"fdkjkfdghgdfgdfgfdg",
"dsdjsfsdkfdfjlfklfkd;ff;;f",
"shjfjshfskjfjhfjhdfhjhdfhdsfjh",
"jhfsjhfsdjhfjsfjksfjksdfkjsdjk",
"hehfehfhsfhdsjfjfjkdjkfjfjdjdfjkjfd",
"dfsfsfsdsfsdfsdfsf"
};
public static void main(String[] args) throws Exception
{
QuoteMultiServer1 SERVER=new QuoteMultiServer1();
SERVER.start();
}
public QuoteMultiServer1() throws IOException
{
SOCK=new DatagramSocket(1080);
SOCK.connect(IP_ADDRESS_GROUP,208);
//SOCK2=new DatagramSocket(2000);
System.out.println("Launched Multicast Serve thread");
System.out.println("Waiting fo client connection");
}
public void run()
{
while(true)
{
// System.out.println("ask client called");
try
{
askclient();
try{
sleep(7000);
}
catch(InterruptedException X)
{
X.printStackTrace();
}
byte[] BUFFER=new byte[600];
String MESSAGE=NextQuote();
BUFFER=MESSAGE.getBytes();
//InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
DatagramPacket PACKET=new DatagramPacket(BUFFER,BUFFER.length,IP_ADDRESS_GRO UP,208);
SOCK.send(PACKET);
try
{
sleep(Time_Interval);
}
catch(InterruptedException X)
{
X.printStackTrace();
}
}
catch(IOException X)
{
X.printStackTrace();
break;
}
}
try
{
//SOCK=new DatagramSocket(2000);
byte[] bufffack= new byte[256];
DatagramPacket clientackpkt=new DatagramPacket(bufffack,bufffack.length);
SOCK.receive(clientackpkt);
System.out.println("received ack");
String ackmsg=new String(clientackpkt.getData());
System.out.println("Client sent" + ackmsg);
// SOCK.close();
}
catch(Exception e)
{
}
SOCK.close();
}
protected String NextQuote()
{
String MESSAGE="";
Date TheTime=new Date();
if(QuoteCount >PirateQuote.length-1)
{
MESSAGE="\n fresh out quotes"+ TheTime.toString() + "";
System.out.println("\n All quotes ae served ");
/*try
{
SOCK=new DatagramSocket(2000);
byte[] bufffack= new byte[256];
DatagramPacket clientackpkt=new DatagramPacket(bufffack,bufffack.length);
SOCK.receive(clientackpkt);
System.out.println("received ack");
String ackmsg=new String(clientackpkt.getData());
System.out.println("Client sent" + ackmsg);
// SOCK.close();
}
catch(Exception e)
{
}*/
}
else
{
MESSAGE=(QuoteCount + 1)+ "" + PirateQuote[QuoteCount];
System.out.println("\n Serving out Quote#" + (QuoteCount+1) + "");
QuoteCount++;
}
return MESSAGE;
}
public void askclient()
{
try
{
//System.out.println("inside askclient");
byte[] BUFFER1=new byte[256];
String MESSAGE1="ARE YOU COONECTED AND READY TO RECEIVE?";
BUFFER1=MESSAGE1.getBytes();
//InetAddress IP_ADDRESS_GROUP =InetAddress.getByName("224.0.0.0");
DatagramPacket PACKET1=new DatagramPacket(BUFFER1,BUFFER1.length,IP_ADDRESS_G ROUP,208);
SOCK.send(PACKET1);
}
catch(IOException ie)
{
}
}
}