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. Program that enabled applet that takes name of the user. Server returns back the number in uppercase clubbed with “HELLO” and display it on applet

  2. Program to read the ballots and count the votes cast for each candidate using an array variable count

  3. Program to show the use of Switch Statement for the same statement for several different case labels

  4. Program to show an example of Concatination of Strings

  5. Program to show the use of Nested Loops

  6. Even no or Prime no?????

  7. Program to print triangle of numbers

  8. Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side

  9. Program to read rollno, marks of sub1,sub2 and sub3 and on basis of that display result

  10. Program to find a factorial of a given number

  11. Develop a RMI based application for addition of two complex numbers

  12. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  13. An applet program to find maximum of three numbers using textField

  14. Program to show an example of using a Non-Static Nested Class

  15. RMI based application for login validation based on data saved in text file stored on the server and will display appropriate message to the client..

  16. Program of bubble sort

  17. Dialog Boxes

  18. Program to Display Output in triangular shape

  19. Program to show an example of modifying String objects

  20. Write a class whose objects holds a current value and have a method to add that value, printing the new value

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