A socket in several threads

This is an academical question. I'm satudying Telecommunications and tomorrow I have an exam about Concurrent Programming.
My questions is quite simple and you can ommit this introduction about what is the exercise if you prefer.
We have seen some ways to pass messages between threads. Once we use an active monitor to take control over the mutex. In this case the active monitor consists of a simple Thread which answers proxys threads petitons for accesing to the mutex. To communicate with the Active Monitor we use s-pipes, which are just a pair of sockets connected where you send messages from one socket and read them from the other. For example we have an array of two sockets, connected: data[0] and data[1].
The proxys thread have all of them the socket data[0] that they use for sending petitions in accesing mutex to the Active Monitor. Anything wrong till here?
We use a BufferedReader (I think) for sending messages through the socket.
Question: Can all these threads use this socket to send petitions without synchronizing it?

That's not a problem. I didn't explain all the things which are involved in the program. The Control Thread doesn't answer through the same s-pipe.
So if T1 and T2 ask for acces to mutex, they wait until they recieve a responsen in an another s-pipe only dedicated for each of them. The s-pipe I called data[0] and data[1] is used just for sending requests and nothing else.
So there are:
- A pair of sockets for sending petitions to Control Thread, they are used only in one way. In this case data[0] is the socket in the proxys threads (T1, T2, ....) and data[1] is the socket in the Control Thread.
- A pair of sockets for each proxy thread, for answering petitions of all the Proxy Threads. For example. If we have 3 proxy threads: T1, T2 and T3. There are then: RecieveAskT1[1] which is in T1; RecieveAskT1[0] which is in Control Thread.
RecieveAskT2[1] which is in T1; RecieveAskT2[0] which is in Control Thread.
RecieveAskT3[1] which is in T1; RecieveAskT3[0] which is in Control Thread.
So the problem is with the socket data[0] which is used for T1, T2,...
What is important for me is: if some of the methods which are involved in sending "things" through a socket are synchronized? Otherwise I cannot understand how my professer uses data[0] without protecting it against the scheduler.
Previous thanks for you future answer!

Similar Messages

  • How to make several threads do smth at the same time?

    Hello!
    Is there a way to make several threads guaranteedly do something at exactly the same time? The code below creates two identical Random objects with approximately 90% probability, in 10% cases Random's are different (Random constructor takes current system time as its argument). But how to ensure it 100%?
    import java.util.*;
    public class SyncThreads extends Thread {
         static Date DATE;
         static long CURRENT_TIME;
        public static void main(String args[]) {
              DATE = new Date();
              CURRENT_TIME = DATE.getTime();
                    new SyncThreads().start();
              new SyncThreads().start();
         public void run()
              try
                   Date thread_date = new Date();
                   long diff = thread_date.getTime() - CURRENT_TIME;
                            //sleeping untill it's 1 second since the 1st line of main() executed
                   sleep(1000 - diff);
                   int r_i = new Random().nextInt();
                   System.out.println(r_i);          }
              catch (InterruptedException e)
                   System.out.println("Find out what InterruptedException is!");
    }

    and even if the PC has multiple processors you are still not guaranteed that it will run the seperate threads at the same exact moment. The JVM for the platform would have to be optomized to make use of multiprocessors for starters, and even if it is it still doesn't guarantee it, the way the OS handles applications (i.e. the JVM) and the way the JVM handles multithreads all will have implications on whether or not two threads actually truly run simultaniously.

  • Sharing socket object between threads - Is any sync required?

    For example, a thread receives (listens) datagram packets at incoming socket and forwards them downstream through another datagram socket. Another thread listens packets at this second socket and forwards the data via the first socket. It is possible that two threads invoke methods of the same socket (one is receiveng while another is sending). This situation is not described in the DatagramSocket manual. I beleive there is underlying synchronization at the OS kernel level. Can I relay on it? Thanks.

    I expected some critics for using old plain sockets instead of nio :)You should use NIO if you have hundreds of connections. If you have a small number of connections and want a simple implementation use blocking IO as you have.
    If you can have different
    threads reading or writing at once eg two readingor
    two writing then you need to use synchronisation.Shouldn't this be stated by the API designers somewhere?It is probibly covered in a tutorial or example somewhere.
    You have a different input and output stream. There is a lock on each. This is your best hint.
    Theoretically, nothing prohibits sending UDP packets in race conditions.
    In fact, this is what my program
    does - it respondes to different clients using one
    (server) datagram socket. The responses are produced
    by multiple threads. I suppose that java does not
    involve any state variables; thus, beleive that the
    system can accomplish the synchronisation unnecesary
    at the application level.That depends on how you build and send your messages. If each part of the message can be sent in one hit and the order does not matter you are fine.
    If you have a single object shared between threads, then all members are shared, not just static variables.

  • Read a same file concurrently by several threads

    Can anyone show me how to open and read a same file "concurrently" by several threads?

    You'll have to be more specific. Which part do you not know how to do? Do you know how to open and read a file? Do you know how to create multiple threads? Also, are all threads reading the file from start to end, or is each thread going to read a different part of the file, to either be combined after all are done or processed independently?
    Please be more specific about what you're asking and post code showing what you've attempted so far.

  • Monitering Multiple Sockets in one thread

    Hi all
    i have a problem.....i want to create and moniter multiple sockets BUT Due to resourse problems i cannot create as many threads. I want to use multiple sockets in one thread.
    Cani do something where i can have array of sockets and poll for data in a single while loop for all the sockets...OR u may suggest another solution for my porblem of using multiple sockets in one thread.
    thanks in advance
    Sai Burra

    Here's a pseudocode solution I've used:
    Create a thread to listen to incoming connections. Upon incoming connection, add the socket and inputstream to the incoming messages listener thread.
    Create a threadpool for outgoing messages.
    Create a thread to process incoming messages.
    Incoming message listener thread:
    Create a hashtable of sockets. Socket is the key, inputstream is the value.
    Loop through each key, and assuming BufferedReader input you can poll by using (input.isReady()). If isReady() returns true, that means there is data on the stream coming in, so you'll need to process it.
    After processing the data, call a thread from your outgoing messages threadpool to send the response - in case there is blocking, you'll still be able to process the rest of your data.
    Sleep the incoming messages thread for 200 ms (300-400ms is quite adequate for real time processing I've found, at least the slowdown isn't noticable).
    This should work for what you're talking about.

  • How can I show thru jlabel a variable that is modfied by several threads?

    Dear all,
    This is my problem, I have a variable, lets say m_money, m_money is been constantly modified by severeal threads, and it is also showed on my user interface using a jlabel componet, lets say m_jlabel_money. You have to take into account that if you want modify the state of a swing object with different threads you have to use something like invokeLater or another Swing utilities.
    My question is how should I program my threads to modify m_money and m_jlabel_money at the same time without multithreading inconsistency?
    I did this, but I am not sure if this is correct:
    // function called by my threads
    public synchronized void SetSaldoUsuarioMoneda(double money) {
              m_money=money;
              SwingUtilities.invokeLater(new ModifyJlabelState ());
    //funtion which modify jlabel statte
         private class ModifyJlabelState implements Runnable {
                  public void run() {
                        m_label_saldo.setText(m_money);
         }Is this correct?
    Should I pass m_money to ModifyJlabelState thru its constructor or should I read m_money from ModifyJlabelState?
    Is synchronized required or is it no sense?
    Is this a better way or is going to fail bacause money variable may be different?
    // function called by my threads
    public synchronized void SetSaldoUsuarioMoneda(double money) {
              m_money=money;
              SwingUtilities.invokeLater(new ModifyJlabelState (money));
    //funtion which modify jlabel statte
         private class ModifyJlabelState implements Runnable {
                      private double m_money;
                    public ModifyJlabelState(double money) {m_money=money;}
                  public void run() {
                        m_label_saldo.setText(m_money); // this m_money is/was what we had when we created this instance, so is this correct?
         }Well, I would like to know your opinions about this, because I don't know how should I solve this.
    Thanks a lot.
    Edited by: Ricardo_Ruiz_Lopez on Jun 24, 2008 1:01 AM
    Edited by: Ricardo_Ruiz_Lopez on Jun 24, 2008 1:02 AM

    Your first approach is incorrect. You either need to synchronize the 'setText' call or make m_money volatile.
    Your second approach will work with the caveat that the value displayed on the label may be slightly out-of-date.

  • Using the distributed System.Transaction in several threads

    I'm using Oracle DB 11.2.0.1 and ODP.NET 2.112.2.0.
    I try to use the distributed transaction from System.Transaction namespace in several threads asynchronously. Here is the test console application. It creates new TransactionScope, creates 2 connections and opens them. At this time the transaction becomes distributed, because there are 2 opened connection in it. Then 2 INSERT commands are executed simultaneously in the same transaction (via Parallel.ForEach):
         class Program
              private static string ConnectionString = "MyConnectionString";
              private static void Main(string[] args)
                   cleanup();
                   for (int i = 0; i < 100; i++)
                        try
                             init();
                             run();
                             check();
                             Console.WriteLine("ITERATION {0} IS OK", i);
                        catch (Exception ex)
                             Console.WriteLine("ERROR AT ITERATION {0}: {1}", i, ex);
                             //break;
                        finally
                             cleanup();
              // main method
              private static void run()
                   using (var scope = new TransactionScope())
                        using (var connection1 = new OracleConnection(ConnectionString))
                        using (var connection2 = new OracleConnection(ConnectionString))
                             connection1.Open();
                             connection2.Open();
                             Transaction tx = Transaction.Current;
                             if (tx.TransactionInformation.DistributedIdentifier == Guid.Empty)
                                  throw new InvalidOperationException("tx.TransactionInformation.DistributedIdentifier == Guid.Empty");
                             var queries = new ConcurrentDictionary<OracleConnection, string>();
                             queries[connection1] = "INSERT INTO T1 VALUES ('AAA')";
                             queries[connection2] = "INSERT INTO T2 VALUES ('BBB')";
                             Parallel.ForEach(
                                  queries,
                                  pair =>
                                       using (var innerScope = new TransactionScope(tx))
                                            OracleCommand cmd = pair.Key.CreateCommand();
                                            cmd.CommandText = pair.Value;
                                            cmd.ExecuteNonQuery();
                                            innerScope.Complete();
                        scope.Complete();
              // check results
              private static void check()
                   using (var connection = new OracleConnection(ConnectionString))
                        connection.Open();
                        OracleCommand cmd = connection.CreateCommand();
                        cmd.CommandText = "SELECT COUNT(1) FROM T1";
                        int n1 = Convert.ToInt32(cmd.ExecuteScalar());
                        if (n1 != 1)
                             throw new Exception("COUNT(T1) != 1");
                        cmd.CommandText = "SELECT COUNT(1) FROM T2";
                        int n2 = Convert.ToInt32(cmd.ExecuteScalar());
                        if (n2 != 1)
                             throw new Exception("COUNT(T2) != 1");
              // initialization          
              private static void init()
                   executeCommands(
                        "CREATE TABLE T1 ( V1 VARCHAR2(100) )",
                        "CREATE TABLE T2 ( V2 VARCHAR2(100) )"
              // cleaning up
              private static void cleanup()
                   try
                        executeCommands(
                             "DROP TABLE T1",
                             "DROP TABLE T2"
                   catch
              // helper method executing some commands
              private static void executeCommands(params string[] queries)
                   using (var connection = new OracleConnection(ConnectionString))
                        connection.Open();
                        OracleCommand cmd = connection.CreateCommand();
                        foreach (string query in queries)
                             cmd.CommandText = query;
                             cmd.ExecuteNonQuery();
    It works fine mostly. But sometimes an exception is thrown at check method ("COUNT(T1) != 1" or "COUNT(T2) != 1"). So one of commands isn't enlisted into the transaction. And when I change commands to insert values into the same table (and also change check method), a deadlock sometimes occurs.
    Where am I doing wrong?
    P.S. There are no errors on Oracle 9.

    The same question after a year: are you going to fix the issue?

  • Server socket programming and thread

    hello,
    briefly this is what i have sought out to do : the server waits for the client indefinitely,upon a client requesting for service by writing a service id into the socket, a new thread of the type id as read by the server which is a sub-class of the Server (main-class) is exec'd.
    the problem as i understand is that the client can send as much of data as it desires to the server and the thread can read the data it receives by the clients.the server(thread) fails to write reply to the client;eventhough out.write() is successful;the client is blocked in read() indefinitely.is it because of the thread which runs at a different port the client does not know and the server is not redirecting response to the client.
    how do i read what the server(thread) writes onto the socket from the clients.

    thanks again,just check out this code please
    public class Server extends Thread //create a multi-threaded server
        /* *Creates a new instance of Server  */
        String                           serverAddr=CodeServer.ServerRun.SERVER_IP_ADDR;
        int                              serverPortNum=CodeServer.ServerRun.SERVER_TCP_PORT;   
        ServerSocket                     sSock;
        Socket                           cSocket;
        int                              requestID=0;
        boolean                          debug=true;  
        InputStream                      in =null;
        OutputStream                     out=null;
        Server ()
        Server (Socket csocket)
            this.cSocket = csocket;
        /* start the server at specifed portNum */
        private void initialise ()
            try
                //start the server at serverPortNum
                sSock=new ServerSocket (serverPortNum);
            catch (IOException ex)
                ex.printStackTrace ();
            /* console output of status of server */
            if(debug==true)
                System.out.println ("Server waiting @ "+sSock);
        private void acceptRequest ()
            try
                this.cSocket=sSock.accept ();
                if(debug==true)
                    System.out.println ("client socket @ "+cSocket);
            catch (IOException ex)
                ex.printStackTrace ();
        private void readRequestID ()
            /*step 1: determine the type of server before threading
             *a request the the type of server got by the request ID
             * number */       
            try
                in        =cSocket.getInputStream ();
                requestID =in.read ();
                out       =cSocket.getOutputStream ();
                if(debug==true)
                    System.out.println ("accross the client:"+requestID);
            catch (IOException ex)
                ex.printStackTrace ();
        private void execThreadForRequest ()
            /*step 2: after requestID is received on the socket.
             *its time to decide the server to thread the control into.
            switch(requestID)
                case 1: //invoke the RegisterServer thread
                    new CodeServer.Core.RegistrationServer (this.cSocket).start ();
                    break;
                case 2: //invoke the ListingServer thread
                    break;
                case 3: //invoke the MessageTransferServer thread
                    break;
                case 4: //invoke the CallSetupServer thread
                    break;
                case 5: //invoke the DisconnectServer thread
                    break;
                case 6: //invoke the ChangeUserStatusServer thread
                    break;
        public void run ()
            // client processing code here==super class does nothing here
        public static void main (String args[]) throws Exception
            Server s=new Server ();
            s.initialise ();
            /* start indefinitely the main server thread */
            while (true)
               /* accept() blocks the server for a request
                  a new request is put into a thread for processing */
                s.acceptRequest (); //initialise the clientSocket
                s.readRequestID (); //get the actual requested service
                s.execThreadForRequest (); //start the actual server thread
    /**registration service class;
    *registers the IP address of the caller,callee and the status
    *adds the caller to the hash table and updates status
    class RegistrationServer extends Server
        Socket              cSocket;
        InetAddress         clientAddr;
        DataInputStream     inStream;
        DataOutputStream    outStream;
        //Socket s=new Socket()
        RegistrationServer (Socket cSocket)
            this.cSocket=cSocket;
            clientAddr =cSocket.getInetAddress ();
            try
                //init for reading status and custom message.
                inStream     =new DataInputStream (cSocket.getInputStream ());
                outStream    =new DataOutputStream (cSocket.getOutputStream ());
            catch (IOException ex)
                ex.printStackTrace ();
            if(CodeServer.ROO.DEBUG==true)
                System.out.println ("register server:"+cSocket
                                   +" host ip:"+clientAddr.getHostName ());
        public void run ()
            int    status=0;
            String custMesg=null;
            try
                /* read the custom message */
                if(inStream.available ()>0)
                    try
                        custMesg=inStream.readUTF ();
                        System.out.println (""+custMesg);
                    catch(EOFException e)
                        e.printStackTrace ();
                else
                    custMesg="";
            catch (IOException ex)
                ex.printStackTrace ();
                try
                           here is the problem, i found if i try reading accross the client             
                           i only receive a junk value 81 always */
                       outStream.write(1); //write success
                        outStream.flush();
                catch (IOException ex)
                    ex.printStackTrace ();
    }and with the client GUI :thanks for the help.please do find time to help me out with a suggestion or two on this code.

  • Socket programming and thread

    Hi all,
    I am getting problem while reading or writing to a port.
    we have two different thread for reading and writing, are they synchronize by itself or we have to make it explictly.
    the error i get are either of these two:
    1) java.io.EOFException
    2) java.net.SocketException: Broken pipe

    i am writing server and client which is thread based, i have implemented the biderectional ,first client writes to server. server reads from client first and then writes to client ,after server write client is not able to read data written by server. i am using the ObjectRead and Object Write methods of socket .
    At client when i read it gives the following error java.io.StreamCorruptedException
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readObject(Unknown Source)
         at com.canarys.web.pushlet.newClient.run(Client.java:93)
    please help me out how to avoid this, the object i am using is serialised, if any of u need more clarification i will paste the code also
    any help regarding this will be appreciated

  • NIO issue - writing to sockets from 2 threads

    Hi All,
    I have some questions regarding a NIO-server i'm developing. I have read many posts relating this issue, but still...
    I have one thread that does the Selector.select() and read & write to the sockets
    I have another thread that uses the same Selector and changes the interestOp to OP_WRITE (then wakes up the Selector).
    I have a Connection (attachment) that i use to hold the inputBuffer and outputBuffer to handle remainings.
    I've read a post in which some ppl wrote the stages of using th OP_WRITE and it was suggested that only when i wanna write i'll add the OP_WRITE and exclude it in the isWritable() - so i'm doing that.
    My questions are:
    1. Is it safe to change the interestOp on a key from another thread?
    2. What happens if the select() is in process?
    3. Are there any other problems with my implementation?
    4. What other way i have to write to a socket from 2 different threads (withough putting a lock on the socket)?
    Thanks.

    Reset your thinking a bit. You should only register for OP_WRITE when you have just executed a 'short write', i.e. a return value > 0 but < the length you asked to write, and you should deregister OP_WRITE any time you execute a 'complete write', i.e. a write which writes everything you asked for. At all other times you should just write and handle your own syncrhonization. The reason is that socket channels are almost always writable except under the condition described so you will just be returning early from select() for nothing.

  • Sync 1 socket between 2 threads.

    consider this code:
    Socket sok = new Socket('192.158.9.42", 5432);
    ThreadClass_1 t1 = new ThreadClass_1(sok);
    t1.start();
    ThreadClass_2 t2 = new ThreadClass_2(sok);
    t2.start();
    t1 needs to download a large file. then t2 takes over
    for the remaining work. so... things are not going well.
    what is the best way to make sure t2 doesn't
    start using the Socket before t1 is finished
    with it?
    should this approach be working? i think it looks like a hack?
    i really don't know.
    private Object lock = new Object();
    Socket sok = new Socket('192.158.9.42", 5432);
    synchronized(lock) {
      ThreadClass_1 t1 = new ThreadClass_1(sok, lock);
      t1.start();
    synchronized(lock) {
      lock.wait();
      ThreadClass_2 t2 = new ThreadClass_2(sok);
      t2.start();
           private class ThreadClass_1 extends Thread {
               private Object lock;
               private Socket sok;
               ThreadClass_1(Socket sok, Object lock) {
                   this.sok = sok;
                   this.lock = lock;
                 public void run() {
                       // do my business with sok
                     this.lock.notify();
    should that work?
    is there anyway to do this without passing the lock down to
    ThreadClass_1 in the constructor?
    and, i'd rather not use some static boolean.
    thanks.

    i got it.
    this is what i need:
    Socket sok = new Socket('192.158.9.42", 5432);
    ThreadClass_1 t1 = new ThreadClass_1(sok);
    t1.start();
    t1.join()  // <----
    ThreadClass_2 t2 = new ThreadClass_2(sok);
    t2.start();and then seriously decrease the size of the write buffer.

  • Write to a file using several threads

    Hello,
    I'm trying to implement a writing procedure into a file using several different threads. The idea I have is to make each thread to write in a different place in the file. In your opinion is there a possibility of some incoherence.
    Regards,
    Anton
    Edited by: anton_tonev on Oct 22, 2007 3:28 AM

    At this moment I can't give a source code example because I'm writing it :) and I'm trying to see the cases where I may have some problems.
    But my idea is to access the file at any time and to write into it with differenent threads in a differenet places in this file. In fact the problem I'm trying to solve is :
    I have a objects called Data that are generated by some other thread independent of my application. This thread puts the Data objects in some kind if buffer. When the buffer is full, I'm creating a few threads fo every object Data in this buffer (the data objects in its own is some collection of floats and doubles) So I'm trying to position the new created threads over the file but in different places and then make them write the data collected in the object Data.
    To position de threads over the file, I'm accessing it by some synchronous method to undestand where it can write in this file and the writing is not synchronous because I'm trying to calculate the exact place and number of bytes of writing and in this way to avoid (may be) the eventuality of errors.
    Regards,
    Anton

  • Properly closing socket and killing thread in cmd window

    Hey all,
    I'm creating my socket connection in a program that i am running in a cmd window. For what i need in my prog, i have a loop that tries to connect every 5 seconds, and even after that, I need to continue looping to check and try to connect.
    The only way i can end the program is to cntrl-c or X out of the program. What i need to know is there a way to properly clean up the thread and close the socket w/o any memory leak issues?
    Thanks

    What i need to know is there a
    way to properly clean up the thread and close the
    socket w/o any memory leak issues?All modern popular use OSes clean sockets, threads and memory (normal) when the application exits for any reason at all.

  • Socket in pooled thread

    I am using a ExecutorService (fixed thread pool) for client threads. Every client thread creates a socket connection. I was wondering if there is a way to keep the socket connection open for every pooled thread or if I have to have an extra pool for those socket connections.

    Yes, just put it somewhere where the Runnables can find it.

  • How should I handle using this socket on multiple threads?

    Hey there, I'm writing a SocketServer app to handle client communications and I've run into something I'm not sure how to handle. Currently, I have the main method create the SocketServer and start new threads whenever a client connects and keeps a list of clients that need to be updated with some info ("Stuff") (Basically a list, which when changed, each update will be sent to all clients on the list to be informed of updates). Each thread that's created when a new client connects is passed the instance of the 'PoolHandler' class, which is supposed to handle updates to the "Stuff" and send updates to any clients that need them. Currently, when a client adds itself to the list of users that need to be informed of updates on "Stuff", the thread that handles that client adds the client name and socket to a hashmap in 'PoolHandler' and anytime the "Stuff" is updated, 'PoolHandler' accesses the passed socket and sends the updates. I realize I'm probably not doing a very good job of explaining this, so I'll show my code:
    public static void main(String[] args) throws IOException {
            final PoolHandler pool = new PoolHandler();
            ServerSocket serverSocket = null;
            while (listening) {
                new SNMultiServerThread(clients, pool, serverSocket.accept()).run();
    public class SNMultiServerThread implements Runnable {
        public void run() {
            try {
                out = new BufferedOutputStream(socket.getOutputStream());
                in = new BufferedInputStream(socket.getInputStream());
                try {
                    while ((read = in.read(input)) > 0) {
                        outputList = snp.process(input);
                        if (outputList != null && !outputList.isEmpty()) {
                            for (byte[] output : outputList) {
                                System.out.println("Transmitting: " +
                                        new String(output, 0, output.length));
                                synchronized(socket) {
                                    out.write(output);
                                    out.flush();
                } catch (SocketException e) {
    public class PoolHandler {
        public PoolHandler() {
        private void updateClients(String update, String game, String team) throws IOException {
            if (update.equalsIgnoreCase("add")) {
                if (game.equalsIgnoreCase(CS)) {
                    clientUpdateListDummy = clientUpdateListCs;
                } else {
                // The HashMap here is K=String, V=Socket
                for (Map.Entry<String, Socket> m : clientUpdateListDummy.entrySet()) {
                    try {
                        out = new BufferedOutputStream(m.getValue().getOutputStream());
                        out.write(snp.prepareOutput(ADD_TEAM_REPLY, team));
                        out.flush();
                    } catch (SocketException e) {
            } else if (update.equalsIgnoreCase("remove")) {
                if (game.equalsIgnoreCase(CS)) {
                    clientUpdateListDummy = clientUpdateListCs;
                } else {
                // The HashMap here is K=String, V=Socket
                for (Map.Entry<String, Socket> m : clientUpdateListDummy.entrySet()) {
                    try {
                        out = new BufferedOutputStream(m.getValue().getOutputStream());
                        synchronized(m.getValue()) { // synchronizing on the socket connected to the client, m.getValue()
                            out.write(snp.prepareOutput(REMOVE_TEAM_REPLY, team));
                            out.flush();
                    } catch (SocketException e) {
    }I attempted adding a synchronized block in the second for loop in PoolHandler, although i'm not sure if I need one at all or if that'll do the trick. The question, I guess, is should I be accessing the socket and then the outputstream for it from here (perhaps with the synchronized block that I added in the second for loop)? Or should I perhaps add a method in the Runnable class that transmits data via the socket and call that from PoolHandler? If I go with the second approach, can I simply pass the thread's name instead of the socket and use, say, m.getValue().transmitThisDataOverTheSocket(myData)? Thanks again, I hope this is clear. :)

    So I've got another question about my code: will the PoolHandler class be responsive if used in the main() thread or do I need to implement Runnable in it and create a new thread also? If so, how should I go about it since I don't want PoolHandler to do anything other than keep track of clients and a few other client-related variables? Here's my main():
    public static void main(String[] args) throws IOException {
            final PoolHandler pool = new PoolHandler();
            ServerSocket serverSocket = null;
            boolean listening = true;
            final int port = 4555;
            try {
                serverSocket = new ServerSocket(port);
            } catch (IOException e) {
                System.err.println("Couldn't listen on port: " + port);
                System.exit(-1);
            while (listening) {
                new SNThreadReader(pool, serverSocket.accept()).run();
            serverSocket.close();
        }

Maybe you are looking for

  • Safari 3.1. and Mac OSX 10.5.3

    Hi everybody, I accidentaly dumped Safari from my system because I never used it and I didn't like it very much. anyway, I am using Opera but to set Opera as my default Browser I need Safari to change this setting. So I downloaded the new 3.1. Safari

  • Acrobat 7.0.5 Preferences

    I am using a program called Systoc that requires Acrobat 7.0.5 standard (yes I know it is way out of date but Systoc does not work with any other version as of right now). One of the required Preference settings under the "Forms" catagory "Automatica

  • Saving rights and date format in Forms - Acrobat Professional 8

    Hi Forum, I have designed a form for a client in Acrobat 8 Professional, and enabled viewing rights in Reader. There will be a lot of people using this form that are not in any way computer literate, and the client has requested that I do the followi

  • Lava lamp external hard drive for the PB G4

    We loaded our itunes library on the lava lamp, but after separating the lava lamp from the laptop, when we reconnect the two, the laptop doesn't recognize or find the external hd. Any suggestions? thanks! PowerBook G4 Mac OS X (10.4.9)

  • Constantly getting Failed SMS messages inside my house.

    I have had issues with a couple cell phone making calls inside the walls of my house, but as of last week now I am getting failed SMS messages that say !Message Not Sent constantly, but yet a large chunk of them do go through. I have never had this i