Two threads, one socket

B

Basim Anwar

I'm trying to create a communications manager module for my
application which is responsible for the sending and receiving of
messages between nodes in the network using TCP and persistent sockets
(in-order confirmed delivery is a requirement). Each node opens are
ServerSocket immediately to receive messages. Sockets are created the
first time a message is sent to a certain destination and the
PrintWriter is stored in a HashMap to be retrieved the next time a
message is sent to the same node.

The problem occurring frequently is that while one node may send and
receive messages as it should, the other one may miss out on some
messages or not receive any at all (though it can still send messages
which are properly received by the first node). No exceptions are
shown to indicate any problems.

The code for sending messages is as follows (ignore that the loops or
sockets are not terminated):

Code:
public void SendMsg(String destadd, Message myMsg) {
        Socket sendsock = null;
        PrintWriter printtemp = null;

        printtemp = SendingConnectionList.get(destadd);
        if(printtemp == null) {
            sendsock = ConnectToOpenPort(destadd, sendsock);
            if(sendsock != null) {
                try {
                    printtemp = new PrintWriter
(sendsock.getOutputStream(), true);
                    SendingConnectionList.put(destadd, printtemp);
                } catch (IOException ex) {
                    System.out.println(destadd+" "+ex);
                }
            }
        } else {
            //System.out.println("Socket already exists: " + destadd);
        }

        String msgtosend = myMsg.myMsgType.toString() + "=" +
GlobalVars.myCommMgr.get_my_address() + "=" + myMsg.content;
        if(printtemp != null) {
            printtemp.println(msgtosend);
        }
    }

    private Socket ConnectToOpenPort(String destadd, Socket sendsock)
{
        int trycount = 0;
        while(sendsock == null && trycount<3) {
            try {
                sendsock = new Socket();
                sendsock.bind(null);
                sendsock.connect(new InetSocketAddress(destadd,
GlobalVars.port), 300);
            } catch (UnknownHostException ex) {
                System.out.println(ex);
                sendsock = null;
                trycount++;
            } catch (IOException ex) {
                System.out.println(ex);
                sendsock = null;
                trycount++;
            }
        }
        return sendsock;
    }

I've tried a simpler client/server case where the client sends
messages in two threads and the server receives them properly.
However, when I make this go both ways and add another thread, the
problem reemerges. If anyone would like to try the code, I've uploaded
both cases at this link:
http://www.megaupload.com/?d=K4R58YRQ
. Testmulti is the simple
one that works, while Multest is the advanced one that causes
problems.

Any help would be greatly appreciated. Thanks!
 
R

Roedy Green

The problem occurring frequently is that while one node may send and
receive messages as it should, the other one may miss out on some
messages or not receive any at all (though it can still send messages
which are properly received by the first node). No exceptions are
shown to indicate any problems.

That would be expected with UDP, but not with a TCP/IP socket unless
there were a program bug.

What is causing your drop-outs?
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan
 
B

Basim Anwar

That would be expected with UDP, but not with a TCP/IP socket unless
there were a program bug.

What is causing your drop-outs?
--
Roedy Green Canadian Mind Productshttp://mindprod.com

"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan

The program gives me no indication as to why the messages aren't
detected, if they're being dropped at all. I think it could be that
the receiver gets the packets and acknowledges their delivery, but
somehow messages are lost or ignored. Otherwise, TCP would have thrown
an exception to indicate a discrepancy, at least.

You mentioned that it could be a program bug. I think it could be
something to do with the receiver, but could you hint as to what kind
of program bugs can cause such problems so I know what to look for?
Thanks.
 
B

Basim Anwar

The program gives me no indication as to why the messages aren't
detected, if they're being dropped at all. I think it could be that
the receiver gets the packets and acknowledges their delivery, but
somehow messages are lost or ignored. Otherwise, TCP would have thrown
an exception to indicate a discrepancy, at least.

You mentioned that it could be a program bug. I think it could be
something to do with the receiver, but could you hint as to what kind
of program bugs can cause such problems so I know what to look for?
Thanks.

I've simplified the problem so that it consists of two nodes, running
two sending threads each and with a ServerSocket open for receiving
messages. This also seems to cause messages to go missing without any
exceptions. Both sides indicate that they have sent 1000 messages, but
do not receive the same. If only one node is the sender and one is the
receiver, there is no problem. Also, if only one thread is sending,
the application runs problem-free. It is only when both sides act as
senders and receivers with multiple threads using the same persistent
socket that the problem arises.

Could this be a problem in the way I've implemented sockets? Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top