简体   繁体   中英

How can I use a SocketServer to start a communication with a Client in Java?

First, I am quite new with the use of Socket communication, so there still some things that I don't know. I understand the basics of Socket communication in Java, so I seeking for guide here.

Now, about my problem, I will need to build a server socket communication with a remote application. In my application I will run the serverSocket to which the Client on the other application will communicate with me. Part of the protocol implemented for this communication is that this socket has to be opened all the time, this provide us of a one perpetual channel of communication between both applications.

Nothing very estrange so far. The problem is that I am going to need to use mi serverSocket to start the conversation with the Client. This means that my serverSocket will need to send some information to the client without the client having requested for it.

For definition I understand that serverSockets are threads that are waiting for a Client to open a connection with them, once established this connection the conversation between the applications normally start with some request (ie an ENQ character) from the Client to start the information exchange; this is for the cases that there is some protocol implemented to validate, send and receive data. Otherwise, the information is send by the client without great validation and control.

In a normal case my problem would be handled in a more natural way if mi application could use a Client to communicate to a serverSocket on the remote application. But this is not the case. The remote application cannot have a serverSocket, it can only have a Client (Don't ask me why, this is the application of a business partner, not my own, and for the moment they are not willing to change their communication scheme).

With this in mind, my question is: There is a way that I could use my running and waiting serverSocket to send some information from my application to the remote Client whenever a process in my application need to? how I call from a third bean in my application the open socket in the serverSocket to write on it?

Sounds like what you're looking for is a UDP socket.

Give the DatagramSocket class a look.

The problem is that I am going to need to use mi serverSocket to start the conversation with the Client. This means that my serverSocket will need to send some information to the client without the client having requested for it.

That doesn't even begin to make sense. The whole idea of a ServerSocket is that the client initiates the connection. And ServerSockets can't send data.

You need to sort out your requirement: it doesn't make sense as stated.

ServerSocket will wait for a TCP connection from something else. Once the connection is made, a call to ServerSocket.accept() will return a new Socket that is connected to the remote system.

Once the connection is established, the distinction between server and client is arbitrary. There is no requirement on who must transmit first. It all depends on the protocol you are implementing.

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