Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Client and server  (Read 1980 times)

0 Members and 1 Guest are viewing this topic.

killer.999919

  • Newbie
  • *
  • Posts: 1
    • View Profile
Client and server
« on: March 03, 2014, 07:03:08 am »
Hey guys the following below are the codes I have for Client and Sever. I would like for the client to send a request containing the message "Client " to a first server. This first server adds to this message the word
“Server1 " and sends it to the client. The client then sends the message received from the first server to a second server. The second server adds the message "Server2" and sends back the message to the client. The final message that reaches the client should be "Client Server1 Server2". And I am lost as in how to make the server reply back cause the server code if you see below is for a echo server. I know the code im running below is right cause I tested it where the client sends a message and the server replies the same message back

Client code:
import java.net.*;
import java.io.*;

public class UDPClient{    

        public static void main(String args[]) {  
            // args[0] = message to be sent to the server;
            // args[1] = IP address of the server
         
                DatagramSocket aSocket=null;
        try {
                        aSocket=new DatagramSocket();
                        byte [] m = args[0].getBytes();
                        InetAddress aHost = InetAddress.getByName(args[1]);
                        int serverPort = 6789;
                       
                        DatagramPacket request = new DatagramPacket(m,args[0].length(), aHost, serverPort);
                        aSocket.send(request);                                         
                       
                        byte[] buffer = new byte[1000];
                        DatagramPacket reply = new DatagramPacket(buffer,buffer.length);
                        aSocket.receive(reply);        
                        System.out.println("Reply: " + new String(reply.getData(), 0, reply.getLength()));
                       
        }catch (SocketException e){System.out.println("Socket: " + e.getMessage());
                }catch (IOException e){System.out.println("IO: " + e.getMessage());
        }finally {
                        if(aSocket != null) aSocket.close();
                }
        }
}

                       
 

Server Code:
import java.net.*;
import java.io.*;

public class UDPServer{    
   public static void main(String args[]) {            
       DatagramSocket aSocket = null;

    try{               
        aSocket = new DatagramSocket(6789);  
            byte[] buffer = new byte[1000];                    
            while(true){                               
                DatagramPacket request = new DatagramPacket(buffer,
                                   buffer.length);
aSocket.receive(request);

                System.out.println("Server is ready and waiting for requests ... ");
                DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(), request.getAddress(), request.getPort());
                aSocket.send(reply);   

             }         
        }catch (SocketException e){System.out.println("Socket: " + e.getMessage());
        }catch (IOException e) {System.out.println("IO: " + e.getMessage());
        }finally {
                if(aSocket != null) aSocket.close();
        }
   }
}

 

 

anything