Java Tutorial

 
 
 
 
// Server

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

public class news_server
{
 public static void main(String[] args) throws IOException 
 {
  int ch1;
  String str;
    FileReader fr = new FileReader(\"news.txt\");
    BufferedReader br = new BufferedReader(fr);


  Socket C = new Socket(\"localhost\",4000);
  OutputStream out = C.getOutputStream();

  DataOutputStream Dos = new DataOutputStream(out);


  while( (str = br.readLine()) != null)
  {
         Dos.writeUTF(\"Latest News (From Server ) :: \" + str);
  }
  Dos.flush();
  fr.close();
 }
}


// Client

//                               Client 
//               =====    


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

public class news_client
{
 public static void main(String[] args) throws IOException 
 {
  ServerSocket S = new ServerSocket(4000);
  Socket Client = S.accept();
  InputStream in = Client.getInputStream();
  DataInputStream Dis = new DataInputStream(in);
  System.out.println(Dis.readUTF());
  Client.close();
 }
}


// OUTPUT

News.txt
=========================
India won the Worldcup by beating
Australia by 6 wkt.
===========================


//  Server
C:\\jdk1.1.3\\bin>javac news_server.java

C:\\jdk1.1.3\\bin>java news_server


//  Client
C:\\jdk1.1.3\\bin>java news_client
Latest News (From Server ) :: India won the Worldcup by beating
Australia by 6 wkt.