Amr Hamada
Amr Hamada

Reputation: 63

Java - creating threads for different tasks

public class server implements Runnable {

private static final int initialize_server_Port = 8080 ;
private static final int store_server_Port =  8181;
private static final int search_server_Port = 8282;
private static String tname ;

private static InetAddress address ;

protected static Hashtable<String , InetAddress> file_location = new Hashtable<String ,InetAddress>();
server(String tname){
    this.tname = tname ;
}

public void run(){

   try{
    if("storeRecords".equals(tname))
        storeRecord();}catch(IOException e ){
            System.out.println("error: unable to create store socket");
        }
    try{
        if("searchRecords".equals(tname))
        searchRecord();}catch(IOException e){
            System.out.println(e);


        }

   // if("search".equals(tname))
     //   searchRecord();

}
public void start(){
   Thread thread = new Thread(this , tname );
    thread.start();
}
public static void storeRecord() throws IOException{
    System.out.println("store out");
    DatagramSocket serverSocket = new DatagramSocket(store_server_Port);
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];

    while(true){

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    String file = new String( receivePacket.getData());
        file = file.trim();
        System.out.println("RECEIVED: " + file);
    InetAddress IPAddress = receivePacket.getAddress();
        address = IPAddress;
    int port = receivePacket.getPort();
        System.out.println(file);
        file_location.put(file , IPAddress);
    String confirmation= "Successfully uploaded";
    sendData = confirmation.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
    serverSocket.send(sendPacket);


    }

}

public static void searchRecord() throws IOException{

    System.out.println("search out");
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];
    DatagramSocket serverSocket = new DatagramSocket(search_server_Port);
    while(true){

        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String file = new String( receivePacket.getData());
        file = file.trim();
        System.out.println("RECEIVED: " + file);
        InetAddress IPAddress = receivePacket.getAddress();
        address = IPAddress;
        int port = receivePacket.getPort();
        boolean found = file_location.containsKey(file);
        if(found == true ){
      InetAddress query =  file_location.get(file);
        String confirmation= "found";
        sendData = confirmation.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);}
        else if (found != true){
            String confirmation= "404";
            sendData = confirmation.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
        }



    }
}



protected static String server_IP ;

public static void main(String[] args) throws IOException {

   server storeThread = new server("storeRecords"); //**************
    storeThread.start();
    server searchThread = new server("searchRecords");
    searchThread.start(); //*********************************
       //     PrintWriter pw = null ;
//Socket socket = null ;

    try {
        InetAddress iAddress = InetAddress.getLocalHost();
        server_IP = iAddress.getHostAddress();
        System.out.println("Server IP address : " +server_IP);
    } catch (UnknownHostException e) {
    }

    DatagramSocket serverSocket = new DatagramSocket(initialize_server_Port);
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];


        while (true) {
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            System.out.println("RECEIVED: " + sentence);
            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();
            String confirmation= "Successfully initialized";
            sendData = confirmation.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);

        }



}
}

So right now the output is

 search out
 search out

what im trying to do is create three threads, one thread running the main, 2nd thread running storeRecord which should printout Store out and 3rd running searchRecord , what am i doing wrong here?

Upvotes: 0

Views: 67

Answers (1)

jop
jop

Reputation: 2316

In your code, tname is a static variable, hence, shared by all instances of server. It is assigned twice in two calls to the constructor. Making it an instance variable (i.e., removing the static) should do what you want.

Upvotes: 1

Related Questions