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: Problems with connecting two sockets on the same application.

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

    Default Problems with connecting two sockets on the same application.

    I have two applications running on the same computer. They are to converse with each other via TCP/IP sockets.

    Application 1 is a second party application and I have no control over how it handles IO. It has two connections 1 is a write port and the other a listen port and when it is connecting to applications on the same computer then the ports cannot be the same.

    Application 2 is my Java application that is to receive data from and pass data to Application 1. It accepts the connection of Application 1 by using a ServerSocket.accept() method. However there is a problem when I attempt to create a Socket that connects to the listen port of Application 1. When I attempt to create the second socket the socket that is created from the ServerSocket.accept() closes or stops the InputStream.

    I need to have way for Application 2 to listen to the port that Application 1 writes to and write to the port that Application 1 listens to and they must be able to handle this at the same time.

    Does anyone know what I could be doing wrong? Here's an example of what I'm seeing:
    Code:

    ServerSocket receiver = null;
    Socket socket1 = null;
    Socket socket2 = null;

    try
    {
    receiver = new ServerSocket(6001);
    socket1 = receiver.accept();
    // here I would start handling socket1.getInputStream();
    socket2 = new Socket("172.10.10.21", 6002);
    // the line above breaks the InputStream from socket1
    }
    catch(Exception e)
    {

    }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problems with connecting two sockets on the same application.

    Why use two different ports for reading and writing?

    Why not just open one socket and read/write on it?

    // Json

Similar Threads

  1. Replies: 1
    Last Post: October 20th, 2009, 07:31 AM
  2. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 08:22 AM
  3. Help about connecting DB to java!
    By java_cs in forum JDBC & Databases
    Replies: 1
    Last Post: September 11th, 2009, 01:39 PM
  4. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 04:15 PM
  5. Replies: 1
    Last Post: November 12th, 2008, 05:16 PM

Tags for this Thread