Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Working With Sockets

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Working With Sockets

    Hi All,

    I have been tasked with creating an interface to a PBX. The PBX (client) will connect to my application (Server). I have created a listener class that waits for
    messages from the client.

    this.serverSocket = new ServerSocket(this.serverPort);
    clientSocket = this.serverSocket.accept();

    Upon receipt of the message I spawn a thread that will handle that message.
    new Thread(
    new WorkerRunnable(
    clientSocket, "MS: "+i)
    ).start();

    From time to time I need to send messages to the client. What would be the best way to do this?
    The client needs to ACK each message I send it.
    I was thinking of creating a separate thread sendMsg(clientSocket) to handle this. So after I send my first message it (PBX) will ACK. Does this ACK stay in this thread or does it go to my listener class? If it goes to my listener class how do I know that the message I sent was ACKEed or some other message?

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Working With Sockets

    Threads are one thing, socket connections are another.
    I think You can read and write through a socket from different threads.
    If you send a message through a socket and the program sends a response through the same socket, I don't think which thread did it will matter.

    I don't know what happens if more than one thread tries to read from a socket at the same time.

    Write a small test program and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 09:45 AM
  2. [SOLVED] Problems with Sockets
    By Samthere in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2011, 06:02 AM
  3. Help Needed - Strings from sockets?
    By nate0927 in forum Java Networking
    Replies: 1
    Last Post: January 24th, 2011, 09:06 AM
  4. Java sockets help
    By ma05k1 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 05:52 AM