Results 1 to 11 of 11
  1. #1
    Engineera is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Angry multiservers thread

    hello every body ..... :(:(:(

    Please all visitors ... I’m from Kuwait and have very difficult project ... must be submitted on next Thursday …. I don’t know part of this project, when we make the server multiple … in the program I put it …. We have only one server and multiple clients … I want to be multi servers and multi client…
    Please heeeeeeeeeeeeeeeeeeeeeelp me :(

    Only ... I want ... to correct this program to satisfy the condition
    import java.net.*;
    import java.io.*;

    public class EchoServer
    {
    ServerSocket m_ServerSocket;


    public EchoServer()
    {
    try
    {
    // Create the server socket.
    m_ServerSocket = new ServerSocket(12111);
    }
    catch(IOException ioe)
    {
    System.out.println("Could not create server socket at 12111. Quitting.");
    System.exit(-1);
    }

    System.out.println("Listening for clients on 12111...");

    // Successfully created Server Socket. Now wait for connections.
    int id = 0;
    while(true)
    {
    try
    {
    // Accept incoming connections.
    Socket clientSocket = m_ServerSocket.accept();

    // accept() will block until a client connects to the server.
    // If execution reaches this point, then it means that a client
    // socket has been accepted.

    // For each client, we will start a service thread to
    // service the client requests. This is to demonstrate a
    // multithreaded server, although not required for such a
    // trivial application. Starting a thread also lets our
    // EchoServer accept multiple connections simultaneously.

    // Start a service thread

    ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
    cliThread.start();
    }
    catch(IOException ioe)
    {
    System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
    ioe.printStackTrace();
    }
    }
    }

    public static void main (String[] args)
    {
    new EchoServer();
    }


    class ClientServiceThread extends Thread
    {
    Socket m_clientSocket;
    int m_clientID = -1;
    boolean m_bRunThread = true;

    ClientServiceThread(Socket s, int clientID)
    {
    m_clientSocket = s;
    m_clientID = clientID;
    }

    public void run()
    {
    // Obtain the input stream and the output stream for the socket
    // A good practice is to encapsulate them with a BufferedReader
    // and a PrintWriter as shown below.
    BufferedReader in = null;
    PrintWriter out = null;

    // Print out details of this connection
    System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " +
    m_clientSocket.getInetAddress().getHostName());

    try
    {
    in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()) );
    out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream( )));

    // At this point, we can read for input and reply with appropriate output.

    // Run in a loop until m_bRunThread is set to false
    while(m_bRunThread)
    {
    // read incoming stream
    String clientCommand = in.readLine();

    System.out.println("Client Says :" + clientCommand);


    if(clientCommand.equalsIgnoreCase("quit"))
    {
    // Special command. Quit this thread
    m_bRunThread = false;
    System.out.print("Stopping client thread for client : " + m_clientID);
    }
    else
    {
    // Echo it back to the client.
    out.println(clientCommand);
    out.flush();
    }
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    // Clean up
    try
    {
    in.close();
    out.close();
    m_clientSocket.close();
    System.out.println("...Stopped");
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }
    }
    }

    EchoClient.java : The client application

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

    // A client for our multithreaded EchoServer.
    public class EchoClient
    {
    public static void main(String[] args)
    {
    // First parameter has to be machine name
    if(args.length == 0)
    {
    System.out.println("Usage : EchoClient <serverName>");
    return;
    }

    Socket s = null;

    // Create the socket connection to the EchoServer.
    try
    {
    s = new Socket(args[0], 12111);
    }
    catch(UnknownHostException uhe)
    {
    // Host unreachable
    System.out.println("Unknown Host :" + args[0]);
    s = null;
    }
    catch(IOException ioe)
    {
    // Cannot connect to port on given host
    System.out.println("Cant connect to server at 12111. Make sure it is running.");
    s = null;
    }

    if(s == null)
    System.exit(-1);

    BufferedReader in = null;
    PrintWriter out = null;

    try
    {
    // Create the streams to send and receive information
    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

    // Since this is the client, we will initiate the talking.
    // Send a string.
    out.println("Hello");
    out.flush();
    // receive the reply.
    System.out.println("Server Says : " + in.readLine());

    // Send a string.
    out.println("This");
    out.flush();
    // receive a reply.
    System.out.println("Server Says : " + in.readLine());

    // Send a string.
    out.println("is");
    out.flush();
    // receive a reply.
    System.out.println("Server Says : " + in.readLine());

    // Send a string.
    out.println("a");
    out.flush();
    // receive a reply.
    System.out.println("Server Says : " + in.readLine());

    // Send a string.
    out.println("Test");
    out.flush();
    // receive a reply.
    System.out.println("Server Says : " + in.readLine());

    // Send the special string to tell server to quit.
    out.println("Quit");
    out.flush();
    }
    catch(IOException ioe)
    {
    System.out.println("Exception during communication. Server probably closed connection.");
    }
    finally
    {
    try
    {
    // Close the streams
    out.close();
    in.close();
    // Close the socket before quitting
    s.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    }

    Thanks … and I wish to help me :mad:

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Just glancing through your code, you seem to be doing the right sorts of things. What is it doing or not doing?

    By the way, put your code in a code block. That will keep the indentation and make your code easier to read.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question ?

    Comments on line 39-49 say this is a multi-threaded server, not a multiple server. Possible language barrier + wording, but like Steve says, what is it doing and not doing that it is supposed to be doing and not doing.
    Java Code:
    Socket clientSocket = m_ServerSocket.accept();
    is just recoded to pass the accept to a Thread, which is done at:
    Java Code:
    ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
    cliThread.start();
    so
    Java Code:
    s = new Socket(args[0], 12111);
    should drive 188-299 as a sample code, test harness if the server and client can see each other......
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Engineera is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Unhappy

    Thanks a lot Nicholas Jordan and Steve11235 for helping me …. I wish you complete your help …. And forgive me for my poor language … but I try hard to explain what I want …. My doctor sees this program … and said to me its implemented multi client thread with one server …. He want only to correct this code to be multi server to multi client thread ……
    Please help me … because tomorrow … I will submit this code….

    :(:(:(:(

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I think your code is doing just that. Your server should wait for a client to connect and then pass the connection to a thread for processing. The server can support multiple clients at the same time.

    I suggest spending some time looking at the thread and socket channel API's, to gain a good understanding of what they do for you...

    Threaded servers are advanced applications. "By the end of the week deadlines" can't be met...

  6. #6
    Engineera is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Unhappy

    dear Steve11235

    my be Nicholas Jordan understood what i want ..........

    please Nicholas Jordan heeeeelp me :(:(:(:(:(

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation when it happens in the real world it cannot happen.

    Yes, I grasp the issue. I ( un-fortunately ) am what in my native language is called: Bringer of Bad News.

    You, nor anyone else within reason, will re-implement a Server to be multi-server + multi-thread + multi-client in one day.

    That is why we keep our skills strong on doing several job employment arena -> if you get in this situation, there is nothing that can be done. Practice the skill now, when it happens in the real world it cannot happen.

    Steve, he wants to implement something like a transaction system that can be load-balanced across an entire server farm. Not exactly a one day project, time for him to learn. Not fun, let me take the hickey if need be: That is what I do as a practiced tradecraft.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    OK, what happens when you run the server? Does it run without generating any exceptions?

    Once the server is running, what happens when you run the client?

    Nicholas- Do people actually pay you for that? Sounds like fun ;-)
    Last edited by Steve11235; 01-14-2009 at 06:33 PM. Reason: Should have read the prior post first...

  9. #9
    Engineera is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Unhappy

    Nicholas Jordan and Steve11235 …. I sent email to my doctor now … and I asked how to correct this program to implement multi server thread … he said.. The only correction that I want … is to add while loop or if statement to the code to implement this situation …. But I don’t know where am I will add this while loop to my code ….
    Heeeeeeeeeeeeeeeeeeeeelp me please ….

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Well, it least he doesn't want a server cluster.

    Your server already has a loop, so it will continue to accept connections and create threads, at least in theory. Have you tried running it? (hint, hint)

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Read Steve's instructions, several times, carefully.

    Also, doctor is correct but should be " Instructor " so that you do not confuse us.

    Basic response: "Have you tried running it? ( hint )( hint )"

    In other words, code above could in fact be run on multiple machines, cluster of farms in epicenters - yes, Global Streams Piggyback.

    The day pigs can fly.......

    Wow!

    So what exactly is the issue?....

    It is suprising how technical minds translate across cultural divides and national boundaries..

    Hint: How would you track a session, or where a session started, or the progress of the session? Could that be done right after the connection was accepted?

    Hint, hint,....fun with newbies!
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 10:06 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •