Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Socket ProgrammingRSS Feeds

Program of UDP news server

Posted By: Easy Tutor     Category: Java     Views: 3058

Write a program of UDP news server.

Code for Program of UDP news server in Java

// Prac client

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

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

    Prac41Client()
    {
        super("News Client");
    }
    publicvoid 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();
    }
    publicstaticvoid main(String args[]) throws Exception
    {
        Prac41Client ob=new Prac41Client();    
        ob.Setup(Integer.parseInt(args[0]));
    }
    publicvoid run()
    {
        while(true)
        {
            try
            {
                DatagramSocket ClientSoc=new DatagramSocket(ClinetPortNumber);
                String Command="GET";
                
                byte Sendbuff[]=newbyte[1024];
                Sendbuff=Command.getBytes();
    
                InetAddress ServerHost=InetAddress.getLocalHost();
                ClientSoc.send(new DatagramPacket(Sendbuff,Sendbuff.length,ServerHost,5217));

                byte Receivebuff[]=newbyte[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
{
    publicstaticvoid 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[]=newbyte[1024];
    byte Sendbuff[]=newbyte[1024];
    DatagramPacket dp;

    AcceptClientRequest(DatagramSocket Soc)
    {
        ServerSoc=Soc;
        dp=new DatagramPacket(Receivebuff,Receivebuff.length);
        try
        {
            ServerSoc.receive(dp);
        }
        catch(Exception ex)
        {
        }
        start();    
    }
    
    publicvoid 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();
        }

    }        
}
  
Share: 


Didn't find what you were looking for? Find more on Program of UDP news server Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of UDP news server is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!