Sever.java file

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

/**
 * Simple server use to teach University of 
 * Northumbria's Operating Systems and Networks modules. 
 * To be used with compatible simple client.
 * <PRE>
 * Protocol: 
 *    client <---message--- server
 *    message = <message body> CRLF
 * </PRE>
 *
 * @author Adrian P Robson & Frank Hindle
 * @version Server 1.2,  16 January 2010
 */
public class Server {
   
 //  private static String message1 = "Hello, from Alex. You are client number ";
 private static String message1 = "The Candidates List is  ";
    
    
   private static class ServeClient extends Thread {
      
      private Socket sock;
     private String data;
   
      public ServeClient( Socket s, String data ) { 
         sock = s; 
         data= "";
      }

      public void run() {  
         System.out.println("** Server thread started"); 
         try {
            OutputStream outToClient = sock.getOutputStream();
            String messageOut = new String(message1 + data+ "\r\n");   
                     // CR+LF added at end of message to enable reading from remote socket using readLine
            outToClient.write(messageOut.getBytes());
            System.out.println("** Message sent"); 
         }
         catch (IOException x ) {
            System.out.println("** Problem serving client: " + x ); 
         }
         System.out.println("** Server thread finished"); 
      }

   }

   private static int getPortNumber()
   {  /**
      * input port number from keyboard and validate it
      **/
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      try {
         int p = 0;
         boolean incorrect;
         do {
            System.out.print("Enter port number: ");
            try {
               p = Integer.parseInt(in.readLine());
               if ((p < 1024) || (p > 65535)) {
                   System.out.println("Port number out of range");
                   incorrect=true;
		   } else {
                   incorrect=false;
               }
            }
            catch ( NumberFormatException x ) {
               System.out.println("Port should be integer");
               incorrect = true;
            }
         } while ( incorrect );
         return p;
      }
      catch (IOException x ) {
         throw new Error("unexpected exception -> " + x);
      }
   }
    
   public static void main( String[] args ) throws Exception
   {
     // int clientNum =0;
      int port = getPortNumber();
      Scanner CD = new Scanner(new FileReader("Candidates.txt"));
      try {
         ServerSocket welcomeSocket = new ServerSocket(port); // creates server socket with port number bound to the socket
         System.out.println("ServerSocket created"); 
        while (CD.hasNext()) {
            String line = CD.nextLine();
            System.out.println("Ready to acccept connections"); 
            Socket connectedSocket = welcomeSocket.accept(); // execution will wait here until a connection request is received
            //clientNum++;
            System.out.println("Connection accepted from " + connectedSocket.getInetAddress().getHostAddress());
            //System.out.println("Now starting server thread for client " + clientNum); 
           // Thread t = new ServeClient(connectedSocket, clientNum);
           System.out.println("Now starting server thread for client " + line); 
           Thread t = new ServeClient(connectedSocket, line);
            t.start();
         }
      }
      catch (IOException x ) {
         System.out.println("Sockets problem: " + x );
      } 
 }
}

Client.java

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

/**
 * Simple client used to teach University of 
 * Northumbria's Operating Systems and Networks modules. 
 * To be used with compatible simple server.
 * <PRE>
 * Protocol: 
 *    client <---message--- server
 *    message = <message body> CRLF
 * </PRE>
 * @author  Adrian P Robson & Frank Hindle
 * @version Client 1.1, 16 January 2010
 */

public class Client {

   private static InetSocketAddress getHostAndPort()
   {  //inputs, from the keyboard, host and port number for the server 

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      try {
         System.out.print("Enter name or IP adddress of server: ");
         String h = in.readLine();
         int p = 0;
         boolean incorrect;
         InetSocketAddress addr = null;
         do {
            System.out.print("Enter portnumber: ");
            try {
               p = Integer.parseInt(in.readLine());
               try {
                  addr = new InetSocketAddress(h,p);
                  incorrect = false;
               }
               catch ( IllegalArgumentException x ) {
                  System.out.println("Port out of range");
                  incorrect = true;
               }
            }
            catch ( NumberFormatException x ) {
               System.out.println("Port should be integer");
               incorrect = true;
            }
         } while ( incorrect );
            return addr;
      }
      catch (IOException x ) {
         throw new Error("unexpected exception -> " + x);
      }
   }  // end of getHostAndPort
    
   public static void main(String[] args) 
   {
      Socket socket = new Socket();
      String message = null;
      String remoteHost = null;
      System.out.println("Client program starting:");

      // get and check the server address 
      InetSocketAddress addr = getHostAndPort();
      if ( addr.isUnresolved() ) {
         System.out.print("Socket address unresolved - "); 
         System.out.println("bad host = " + addr.getHostName() );
         System.exit(1);
      }
      System.out.print("Socket address resolved as "); 
      System.out.println(addr.getHostName() + 
                            "/" + addr.getPort());

      //  now connect to the server
      try {
         System.out.println("Attempting to connect to server"); 
         socket.connect(addr);
         remoteHost= socket.getInetAddress().getHostName();
         System.out.println("Connection made to " + remoteHost ); 
      }
      catch (Exception x) {
         System.out.println("Unable to connect to server - " + x);
         System.exit(1);
      }

      // now read a message from the server
      try {
         //message from server is a single line ending with CRLF so use a Buffered reader's readline method 
         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream() ));
         message= inFromServer.readLine();

     /*************** Alternative, non-buffered read from socket: 
     *    byte [] buff = new byte[4000];   // buffer for messages read from the socket
     *    InputStream inFromServer = socket.getInputStream();
     *    // now read from the socket
     *    int len = inFromServer.read(buff);
     *    // convert contents of buffer to a string
     *    message = new String(buff, 0, len-2);  // assume CRLF was at end of message, so remove these two characters
     **********/

         System.out.println();
         System.out.println("Message from server is >" + message + "<");
         System.out.println();
      }
      catch (IOException x ) {
         System.out.println("Problem encountered -> " + x );

      }
      System.out.println("Simple sockets client finished");
      System.out.println();
   }  // end of main
}

Candidates.txt
ID Fname LName
12 Alex Yui
24 Andy Lui


Can somebody help me , when i run it take nothings

the data variable is not set in the Server class that is whay you receive this message from theclient
Message from server is >The Candidates List is null<
try to give it some vlue int the Server class.
Hope it helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.