Results 1 to 7 of 7
  1. #1
    ibtehal is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default blocked between server and clients

    Hi,
    I have one server that creates thread for each client ,and in the client side each client creates thread to send multiple objects . It works for first requests but then my server wait from the client and the client wait response from the server. I have use synchronized in run( ) in server side and client side but it does not work.
    Server code:
    class ServerThread extends Thread
    {

    private Socket socket;
    private ObjectInputStream objIn; //to read
    private ObjectOutputStream objOut; // to write

    public ServerThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout
    )throws IOException
    {
    objIn =objin ; //read object
    objOut = objout; //write object
    socket =ss;
    start(); // Calls run()

    }//end constructor

    public synchronized void run()
    {
    try{
    while(true)
    { //accept objects and processd
    }
    }
    catch(Exception e){
    System.out.println( "Disconnected");}

    }//end run
    }//class Thread

    public class server

    { protected static int PORT ;
    static ServerSocket s;
    static Socket client;
    public static void main(String[] args) throws IOException
    {

    try {
    PORT = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
    System.out.println("port = 1500 (default)");
    PORT = 1500;
    }


    s = new ServerSocket(0);


    try
    {
    while (true)
    {
    client = s.accept();
    System.out.println("Client Accepted");
    x++;
    try{
    ObjectOutputStream outToClient1 = new ObjectOutputStream(client.getOutputStream());
    ObjectInputStream inFromClient1 = new ObjectInputStream(client.getInputStream());
    new ServerThread(client, inFromClient1, outToClient1);
    clients.addElement(client);
    }//end try
    catch (IOException e)
    {System.out.println("\nUnable to set up port!");
    client.close();
    }//end catch
    }//end while

    }//end main try
    catch (Exception e)
    {System.out.println("\nUnable to set up port!");}

    finally
    {
    s.close(); //finally close socket

    }//end finally

    }//end main
    }//end server class

    Client code:
    class ClientThread extends Thread
    {


    private Socket socket;
    private ObjectOutputStream outToServer;
    private ObjectInputStream inFromServer;


    public ClientThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout , int cn
    )throws IOException
    {
    inFromServer =objin ; //read object
    outToServer = objout; //write object
    socket =ss;
    myid =cn;
    start(); // Calls run()

    }//end constructor

    public synchronized void run()
    {

    for(int u=0;u<=5;u++)
    {
    //Send objects
    }
    }
    }//end thread
    public class client {
    public static void main(String[] args) throws IOException
    {
    String host = "localhost";
    int PORT=2911 ,x=1; ;
    Socket link=null;
    ObjectOutputStream outToServer1;
    ObjectInputStream inFromServer1;
    if(args.length == 2)
    {
    host = args[0];
    try {
    PORT = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
    System.out.println("server port = 1500 (default)");}
    PORT = 1500;
    } //end if

    while( x<=5)
    {

    try {
    link = new Socket(host, PORT);
    }//end try
    catch (UnknownHostException e)
    { System.out.println(e);}


    try{

    outToServer1 = new ObjectOutputStream(link.getOutputStream());
    inFromServer1 = new ObjectInputStream(link.getInputStream());
    new ClientThread(link, inFromServer1, outToServer1, x);
    }//end try
    catch (IOException e)
    {System.out.println("\nUnable to set up port! " +e);
    }//end catch
    x++;

    } //end while
    }//end main
    }//end client class

    thanks:)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Please correct all compile time errors before posting your code unless that is what the question is about. If you don't understand why you are getting the errors, ASK.
    Don't assume that we'll do that work for you.

  3. #3
    ibtehal is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    I do not have any compile error and I did not to ask you to do my work , my question was clear is about the blocking between the server and the client after short time in the run time. Just I want to show you what is my programme structure.

    Thanks;)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    The code you posted gets compiler errors.
    Where is myid defined?
    Where is x defined in server?

    If this is NOT the code you are executing, how can anyone help you find problems?

    It would make your program more readable if you used code tags to preserve the indentations.

    The synchronized tag for method(s) only works with a single instance of a class. The object has a queue and the system controls entry to a synch method so that only one is entered at a time. All others are queued until the previous one leaves.

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

    Default Uh, we have some work to do.

    Quote Originally Posted by ibtehal View Post
    .... does not work.
    Threading is deeply challenging to get correct code.
    Java Code:
    ///....snip
         start(); // Calls run()
    ///....snip
    No it doesn't, not as you have it here.
    Java Code:
    public synchronized void  run()
    Do what?
    Java Code:
    //accept objects and processd
    How?
    Java Code:
    s = new ServerSocket(0);
    Maybe, I have not written a server so I do not know.
    Java Code:
          
        try
        {
            while (true)
            {
                client = s.accept();
                System.out.println("Client Accepted");      
                x++;
                try{ 
                ObjectOutputStream   outToClient1 = new ObjectOutputStream(client.getOutputStream());
                ObjectInputStream inFromClient1 =  new ObjectInputStream(client.getInputStream());
                new ServerThread(client, inFromClient1, outToClient1);   
                clients.addElement(client);
            }
        }// ... snip ...
    Norm, I would think - until we get clearer code - this to be where the sync should happen so that incoming is dispatched to the ServerThread(client, inFromClient1, outToClient1); clients.addElement(client); in a non-garbled manner. Trying to sync the entire run method does not sound like an efficient design. We have to make the design such that there is one and only one thread at a time in the dispatcher. To do otherwise requires dedicated equipment beyond the scope of source code.
    Java Code:
    /// Start is in the wrong place.
    start(); // Calls run()
    This is shown correctly in the docs for Thread class.
    Java Code:
    //Send objects
    How?
    Java Code:
    /* USE OF THIS CODE CONSTITUTES DISCUSSION OF OPINION IN A TECHNICAL FORUM
     * NO SUITABILITY FOR ANY USE IS IMPLIED BY READING THE OPINION PROVIDED
     *
     * "The supposed "cost" of synchronization is usually really trivial compared
     * to the energy people spend trying to avoid it." - Master Wong.
     *
     * Simple client/server Socket selection program.
     * Select a port to invoke the public Socket(InetAddress,int port) with.
     * The Dynamic and/or Private Ports are those from 49152 through 65535
     * Source:  http://www.iana.org/assignments/port-numbers
     * The port numbers are divided into three ranges: the Well Known Ports,
     * the Registered Ports, and the Dynamic and/or Private Ports.
     */
                 int PORT=52911;//
    Try to select a port using rfc's not reduced foulup code.

    We need to see the exception message:
    Java Code:
    // Print message the exception is giving.
    System.out.println("Unable to set up port:   ");
    System.out.println(System.getProperty("line.terminator"));
    System.out.println(e.getMessage());
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Nicholas,
    Do you understand what the program is supposed to do?
    There are some clients and a server and they are supposed to interact somehow.
    I think the problem needs a better description.

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

    Default simple server sample

    Quote Originally Posted by Norm View Post
    Do you understand what the program is supposed to do?
    Simple server/client interaction. Given an instruction pointer executing in code on a browser somewhere, some data is stacked up and shipped via socket. As activity enters the socket.accept, we have basically a hung machine for a moment. That is unless some decent engineering has been done. At that point, what is shown in code as an input stream amounts to a data structure resembling a file. It is extremely simple, but most cannot disentangle actual hardware running from source code we see here.

    At any moment, another instruction pointer can walk into this code while the previous accept call is still running. So we have to sync somewhere. Wrap code just past accept in synchronized this. Right there, do a new Object( fileIn, fileOut ) and start it and basically get outta Dodge.

    The new Object, because it is a Thread, has it's own 'area' to work in. To do it correctly so that it does not overrrun the machine requires connection pooling and setting variables to null when done. gc is slow and clunky and has unsolvable portability issues. This code does not need gc

    Poster's code is typical first attempt. Looks like someone who is trying.

    Quote Originally Posted by Norm View Post
    There are some clients and a server and they are supposed to interact somehow. I think the problem needs a better description.
    Simple sample server in JDK-5 very remarkable. See: A Simple NIO-based HTTP/HTTPS Server Example in
    Java Code:
    C:\Program Files\Java\jdk1.5.0_12\sample\nio\server\README.txt
    Reference implementation, what poster's code does is put both client and server in the same place so that one can do isolated prototyping.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. SQL server
    By elish11 in forum JDBC
    Replies: 2
    Last Post: 08-20-2008, 06:00 PM
  2. Replies: 2
    Last Post: 07-15-2008, 06:11 PM
  3. One server to another server redirection
    By chaudhuri_abhi in forum Java Servlet
    Replies: 1
    Last Post: 02-11-2008, 07:05 PM
  4. WAS Server
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-25-2007, 05:38 AM
  5. Multple Clients
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 06:37 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
  •