File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes sockets: How to receive server response in client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "sockets: How to receive server response in client" Watch "sockets: How to receive server response in client" New topic
Author

sockets: How to receive server response in client

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Hello,

Just as a warning... I'm new to socket programming. I have a book on the way... but I'm looking for a solution in the meantime.

I am sending a message to a server via socket 18011. That server is on a windows platform. Their VB application is acting as the socket server. It is supposed to be sending me a response. Their program is sending a response to my IP address over port 18015 ? I have tried a number of approaches. Their server supposedly tries to send a response every 10 seconds until I receive it. The code below executes but I never get the response. Is there a surefire way for me to keep that port listening on my machine for a response?



I have tried the following... but it does not seem to work:


DataInputStream input;
try {
input = new DataInputStream(MyClient.getInputStream());
}
catch (IOException e) {
System.out.println(e);
}


Any help would be much appreciated.



- Mike
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
I tried to test a server socket response based on the following code. I've added the client and server apps. The server app is noted where I am trying to send back a response to the client. Any suggestions on what I need to add to send back the response?

Thanks,

Mike



________________________________________________



J Kneeland
Ranch Hand

Joined: Aug 15, 2004
Posts: 31
Wait a second, so you are saying that you contact the VB server on it's port 18011 and then it some how (disconect first?) connects back on your port 18015 ?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Mike Cunningham:
Any suggestions on what I need to add to send back the response?


You are sending a response from the server. You are not, however, reading that response on the client.
This question has more to do with
Sockets and Internet Protocols so I'm going to move it to that forum.


[How To Ask Questions On JavaRanch]
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Originally posted by J Kneeland:
Wait a second, so you are saying that you contact the VB server on it's port 18011 and then it some how (disconect first?) connects back on your port 18015 ?


The VB server is from another department which is using a product from Dart.com called PowerTCP Winsock Tool. I'm connecting via Java application to a new socket using port "A" on his server. He is then supposed to be sending me a response back on port "B" to my machine. Do I need to have a socket server running on my machine to receive the response from his server or can I add code in my client to listen for the response?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Mike Cunningham:

I'm connecting via Java application to a new socket using port "A" on his server. He is then supposed to be sending me a response back on port "B" to my machine.


Just like J Kneeland, I am amazed that your system works that way.
You will have to set up a ServerSocket to listen to port "B" on your client.
If you want to learn more about networking, check out the Networking chapter of the Java Tutorial.
[ August 25, 2004: Message edited by: Joe Ess ]
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Are there two connection involved, or just two ports?

A TCP connection consists of two machine/port pairs. When your machine connects to the server, it uses an ephemeral port number on your machine but connects to a well known port number (18011 in this case) on the server. The ephemeral port number on your machine is machine assigned and may vary from connection to connection; you shouldn't have to worry about it, because the connection setup takes care of informing the server what it is. The server can then send responses on the same TCP connection, which is two way. If this is the way your system works, than your bug is elsewhere than the port numbers.

On the other hand, it's possible that the server application connects back using a second TCP connection. In this case, it will use an ephemeral port on the server machine and connect to a well known port on your machine (perhaps 18015 in this case?) If this is the way your system works, then yes, you need to set up a ServerSocket to listen on port 18015 on your machine.
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
You probably have to consider whether there are firewalls inbetween as well......


Meng Yi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sockets: How to receive server response in client