Java Blog

 
 
 
// Prac client

import java.net.*;
import java.awt.*;

public class Prac41Client extends Frame implements Runnable
{
    Label lblNewsHeadline;
    String NewsMsg;
    Thread t=null;
    int ClinetPortNumber;

    Prac41Client()
    {
        super(\"News Client\");
    }
    public void Setup(int ClientPort)
    {
        ClinetPortNumber=ClientPort;
        setSize(500,50);
        lblNewsHeadline=new Label(\"Retrieving News From News.Com\");
        add(lblNewsHeadline);        
        show();
        t=new Thread(this);
        t.start();
    }
    public static void main(String args[]) throws Exception
    {
        Prac41Client ob=new Prac41Client();    
        ob.Setup(Integer.parseInt(args[0]));
    }
    public void run()
    {
        while(true)
        {
            try
            {
                DatagramSocket ClientSoc=new DatagramSocket(ClinetPortNumber);
                String Command=\"GET\";

                byte Sendbuff[]=new byte[1024];
                Sendbuff=Command.getBytes();

                InetAddress ServerHost=InetAddress.getLocalHost();
                ClientSoc.send(new DatagramPacket(Sendbuff,Sendbuff.length,ServerHost,5217));

                byte Receivebuff[]=new byte[1024];
                DatagramPacket dp=new DatagramPacket(Receivebuff,Receivebuff.length);
                ClientSoc.receive(dp);


                NewsMsg=new String(dp.getData(),0,dp.getLength());
                System.out.println(NewsMsg);
                lblNewsHeadline.setText(NewsMsg);

                Thread.sleep(5000);            
                ClientSoc.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }    
}


// Prac Server

import java.net.*;
import java.util.*;
import java.io.*;

class Prac41Server
{
    public static void main(String args[]) throws Exception    
    {
        DatagramSocket ServerSoc=new DatagramSocket(5217);        
        while(true)
        {        
            AcceptClientRequest ob=new AcceptClientRequest(ServerSoc);
        }
    }
}

class AcceptClientRequest extends Thread
{

    DatagramSocket ServerSoc;
    byte Receivebuff[]=new byte[1024];
    byte Sendbuff[]=new byte[1024];
    DatagramPacket dp;

    AcceptClientRequest(DatagramSocket Soc)
    {
        ServerSoc=Soc;
        dp=new DatagramPacket(Receivebuff,Receivebuff.length);
        try
        {
            ServerSoc.receive(dp);
        }
        catch(Exception ex)
        {
        }
        start();    
    }

    public void run()
    {
        try
        {    
            InetAddress ClientHost=dp.getAddress();
            int ClientPort=dp.getPort();            
            System.out.println(ClientHost);

            FileReader fin=new FileReader(\"NewsDataFile.txt\");
            BufferedReader br=new BufferedReader(fin);

            StringBuffer News=new StringBuffer();
            String NewsHeadLine;

            while((NewsHeadLine=br.readLine())!=null)
            {
                News.append(\"|\" + NewsHeadLine + \" |\");
            }


            Sendbuff=News.toString().getBytes();
            System.out.println(News.toString());
            DatagramPacket sendPacket=new DatagramPacket(Sendbuff,Sendbuff.length,ClientHost,ClientPort);
             ServerSoc.send(sendPacket);
            System.out.println(\"Packet Sent ...\");

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }        
}

    Related Post:
  1. An applet program to perform selection sort

  2. Program to show the use of Bitwise Operator (~) Complement

  3. Program to show the use of Do-While Loop

  4. Program to show the use of Nested Loops

  5. Program to show the use of Conditional AND Operator (&&) in If Statements

  6. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  7. Program to show an example of using Abstract Class and Abstract Method

  8. An applet program to add two numbers where data is entered in TextField

  9. An applet program that displays random dot

  10. Program to show the use of the Continue Statement

  11. Program to compute the sum of the digits of a given integer number. eg: I/o=4 then O/p=4+3+2+1=10

  12. Program to show an example of getting at characters in a String

  13. Develop a game application in CORBA for which the player will guess a number between 1 to 100, which will be compared to the random number generated b

  14. Application which takes your marks of six subjects from the client side and displays the percentage that you have obtained

  15. An applet program to display alternate filled and empty circle that to in row and column format

  16. Program to show an example of searching Strings for subStrings

  17. RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

  18. Corba program to Write a Echo server and client with UDP server and client

  19. Application for counting factorial of a given number

  20. An applet program to give demo of getDocumentBase() and getCodeBase() methods

 
 
Didn't find what you were looking for? Find more on Program of UDP news server