简体   繁体   中英

How to use source port to send and receive data by TCP/IP in Java

I have started a client in my system. It is running on port no 7913. I am sending a request data via TCP/IP from Java to server socket running on 7913.

log is Message sent to Socket [addr=/190.161.153.109,port=7913,localport=54717]

I have also received the response from server for that particular data. Now the server is also trying to send a request to my localport 54717, not to port where my application is listening [ie 7913].

How to handle the request? When I try to connect with telnet to my localport, connection is refused.

The code:

public static String ickTransport(String ickHeader,String ickdata, Socket connection) throws UnknownHostException, IOException

    try
    {
        connection.setSoTimeout(Integer.parseInt(ickTimeOut));
        log.debug("ick Message for "+connection.toString()+" is " + ickMessage);            
        BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
        DataOutputStream osw = new DataOutputStream(bos);
        osw.writeShort(Integer.parseInt(ickHeader));
        osw.writeBytes(ickMessage);
        osw.flush();

        DataInputStream stream = new DataInputStream(connection.getInputStream());
        int numberRecords   = stream.readShort();   
        if (numberRecords > 0) {
            int nSizeRead = 0;
            byte[] bRequest = new byte[numberRecords];

            int nSizeBuffer;
            for (; numberRecords > 0; numberRecords -= nSizeBuffer) {
              byte[] bBuffer = new byte[numberRecords];
              nSizeBuffer = stream.read(bBuffer);
              System.arraycopy(bBuffer, 0, bRequest, nSizeRead, nSizeBuffer);
              nSizeRead += nSizeBuffer;
            }
            ickResponse = new String(bRequest);
            log.debug("Response from ick is " + ickResponse);
        }               
    }
    catch (SocketTimeoutException e) 
    {
        log.error(e.getMessage());       
    }

    return ickResponse;     

To understand what is going on you should understand what is listen socket and how it differs from connection socket .

When your application listens it (this ServerSocket does):

  • Attaches to the port that you specify in bind request or in constructor
  • Ask JVM to receive new connection on that port
  • When connection is received listen socket changes it state and provide you new socket for new connection with accept method.

When your application establishes NEW connection it use connect method. Unless you use bind request on socket it:

  • Allocates new dynamic port (54717 in your example)
  • Sends connect request to the server
  • After connection established you can use it for sending/receiving requests to/from server

Because nobody listens this dynamic port telnet requests are refused on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM