0

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 (i.e. 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?

ArsenioM
  • 21
  • 4
  • 1
    since it's a persistent channel, just open the connection from the client to the server and put the client in a read state - when the server writes, the client will get it. – KevinDTimm Nov 16 '11 at 21:55
  • Exact, that is what I understand that will happen. I have done some tests sending some data to the client when it open the connection with me (just right after the ServerSocket.accept() return the Socket connected to the client). But how can I send this information any time I want, if this socket connections is already opened? How can I write on this socket that is already waiting for some transmission to happen? – ArsenioM Nov 17 '11 at 14:28
  • `write` at the server side and `read` at the client side – KevinDTimm Nov 17 '11 at 14:32
  • Exactly. I know that is what I have to do. but, how I do it in code?. how I call from a third bean in my application the open socket in the serverSocket to write on it? – ArsenioM Nov 17 '11 at 14:47

3 Answers3

1

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

Give the DatagramSocket class a look.

ICas
  • 197
  • 4
  • 2
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • ServerSockets can send data. For example: when there is a protocol in which the client need confirmation from the serverSocket when the received data has been validated correctly or not. – ArsenioM Nov 17 '11 at 14:01
  • @ArsenioM Really. By which API? Which OutputStream? ServerSockets cannot send data. – user207421 Nov 17 '11 at 22:38
  • When your SocketServer.accept() return a new Socket, you can use the socket.getOutPutStream() to handle an OutputStream. on which you can call the write(). Anything that you pass to the write() will be received on the client. – ArsenioM Nov 18 '11 at 14:10
  • @ArsenioM Exactly my point. The *accepted Socket* can send data. The ServerSocket cannot. – user207421 Nov 18 '11 at 23:20
  • That is correct, I mean to use the socket accepted in the ServerSocket. Excuse me for the misunderstood. – ArsenioM Nov 23 '11 at 13:37
0

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.

John Haager
  • 2,107
  • 1
  • 17
  • 22
  • Correct... Once the connection is stablished no matter who start the data transmission. In my case the connection is always open, so any side can start the communication and data transmission. My problem is that I don't know how to invoke and use the serverSocket on my application to be the one who send data first to the client any time I need. The client do not request this data. I have to send this data to the client when some process occur in my application. – ArsenioM Nov 17 '11 at 14:11
  • I have done some tests sending some data to the client when it open the connection with me (just right after the ServerSocket.accept() return the Socket connected to the client). But how can I send this information any time I want, if this connections is already opened? – ArsenioM Nov 17 '11 at 14:22
  • In order to send data to the client, you will have to take the Socket that is accepted by the server and store it somewhere for later use. Normally, I put them into a Map that maps a client identifier to the Socket instance. Then I can look it up whenever I need it and send data. – John Haager Nov 19 '11 at 00:04
  • I'll try to do that, thanks for your advise. If it works for me I will post the results. In the meanwhile, I'll appreciate if you could illustrate your solution giving me some examples of your code, that way I could verify if I clearly understand your answer. Thanks. – ArsenioM Nov 23 '11 at 13:43