Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Can Someone tell me what I am doing wrong?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Someone tell me what I am doing wrong??" Watch "Can Someone tell me what I am doing wrong??" New topic
Author

Can Someone tell me what I am doing wrong??

Michael DeChirico
Greenhorn

Joined: Jul 31, 2008
Posts: 16
Hi there,

I have been trying to write a client/server app to communicate between a desktop and a mainframe server.

I have my server app running on the mainframe and have attached the java code that I am trying to get to run on my desktop as follows:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;

import java.io.*;
import java.net.*;

/**
*
* @author MichaelXPS720
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {

Socket ClientSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
ClientSocket = new Socket("122.022.032.246", 11007);
out = new PrintWriter(ClientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Unable to connect to host.");
System.exit(1);
} catch (IOException e) {
System.err.println("Host refused Connection");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String ReadFromHost;
String SentFromClient;

System.out.println("Connection Established, Dialog Begins");

while ((ReadFromHost = in.readLine()) != null) {
System.out.println("Read From Host: " + ReadFromHost);
if (ReadFromHost.equals("Bye.")) {
break;
}

SentFromClient = stdIn.readLine();
if (SentFromClient != null) {
System.out.println("User Entered Data: " + SentFromClient);
out.println(SentFromClient);
}
}

out.close();
in.close();
stdIn.close();
ClientSocket.close();
}
}

The sequence of events is as follows:

I start the server on the mainframe, once its listening for connection requests, I start the above java app.

Once the java app startes it displays a simple execution starts msg, then falls into a read from host/write to host loop.

The host recognizes the connection request and tries to send a "hello" msg, the java client is then supposed to write the data from the host and then send a string entered from the desktop via keyboard to host,

This loop should continue until the host sends the termination string, "Bye."

then the client should stop and the host drop its connection.

Whats happening is the client starts, displays the execution begins msg, the host sends its "hello" msg and the client sits their like a vegetable.

The host is in a wait state waiting for a response to the hello msg.

Can someone help a novice in the java world to understand whats going on?

Thanks,

JD
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
Don't println to a Socket

It's not just a Mac OS problem - it applies to all Unix-like OSes as well.
[ August 26, 2008: Message edited by: Ulf Dittmer ]
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
I cannot give you an answer without inspecting (sniffing) the conversation between client and server ... but 99% of the time the problem is about buffering.

When you write on a socket you normally flush the stream on need, or the stream get flushed onto the socket when the buffer has reached capacity.

If you have a sniffer you can check what is going on, if not, this are the most frequent pitfalls of socket communication.

1) The outputstream is buffered, but is not flushed.
If you use buffered streams you must be certain to flush them after completing a 'message'.

2) The inputstream uses 'line-terminators' which are not sent.
Some stream expect some kind of line terminators, (for example CR LF) which are not sent, the 'reader' in this case will lock, waiting for those markers.

Both this error applies to server and/or client, so sometimes the client is not flushing or viceversa, or the client waits for 'line-termination' or is the server waiting.

Without a sniffer, it is diffucult to understand what is going on, but you can guess what's happening reading one byte at a time.

Be sure to flush buffered stream.

Best Luck
(with sockets you need it)
[ August 26, 2008: Message edited by: Mario Minutolo ]
Michael DeChirico
Greenhorn

Joined: Jul 31, 2008
Posts: 16
Mario Minutolo,

Ok I'll bite, how does one "flush the buffer"???

Michael
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by Michael DeChirico:
Mario Minutolo,

Ok I'll bite, how does one "flush the buffer"???

Michael




... still I would advise you against reading 'lines' , a missing terminator could get your whole application stuck.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
... a missing terminator could get your whole application stuck.

... which is of course precisely what the article I linked to explains in detail.
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by Ulf Dittmer:

... which is of course precisely what the article I linked to explains in detail.

I saw, and I concord with you, but, I still like to explain, even briefly in post, what is all about because too many time I found a post with a solution I needed and the link pointing to the solution was broken (so frustrating!!!).
[ August 26, 2008: Message edited by: Mario Minutolo ]
Michael DeChirico
Greenhorn

Joined: Jul 31, 2008
Posts: 16
To all who have tried to help.

In frustration, discarded my existing java client code and came up with another one:




There is a comment line: /* waits here */ in the above code, its waiting for a response from the host, the problem is the host says it sent the response.

Once again, any one have any ideas on how to figure out what the problem is?

Thanks

Michael

[edit]Add code tags. CR[/edit]
[ August 26, 2008: Message edited by: Campbell Ritchie ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
Did you read the article I linked to? The problem is still present in the code.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Michael,
Could it be due to the messageIn = incoming.readLine(); you are doing?

Me thinks that BufferedReader.readLine(); will wait for a line termination to come through.

Does your handshake message send through a line termination?

I am not sure what will constitute a line termination in this case. I think it depends on the platform that your client is running on.

Have you tried using one of the other read methods in the BufferedReader class which probably do not have to wait for line termination?
[ August 27, 2008: Message edited by: Gamini Sirisena ]
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
The problem seems to be something else, IMHO ...

Your code waits for two things to happen:
- The server to send data
- The user entering data through System.in

In a single thread, you cannot wait for two events at the same time. So you need one thread that waits for and reads input from System.in and sends it to the server, and one more thread that waits for and reads data coming back from the server and processes it in some way, eg by printing it to System.out.
Michael DeChirico
Greenhorn

Joined: Jul 31, 2008
Posts: 16
Actually, this is the old style half duplex communications methodology, i am either sending or waiting and never doing both at the same time.

The client sent a msg, the server received it and displayed it in a msg log, then turned around and sent a msg back to the client, the client never got it, the server then turned around and waited for something from the client and there they sat, fat dumb and happy.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Sorry, so I didn't get the second code example right ... my answer was referring to the code in the original post.
Michael DeChirico
Greenhorn

Joined: Jul 31, 2008
Posts: 16
My thanks to everyone who offered suggestions, it turns out it was a real dumb programming mistake on the server side.

All of the comments regarding println should have made me look closer at the server code but this was one of those can't see the forest for the trees things.

The client was running fine, the server (residing on an ibm main frame) uses ebcdic and not ascii as its code base, so when it had a response to send to the client it was doing a code translation from ebcdic to ascii, but was being too aggressive, it was translating non-printable characters, like x0A to x00 so the println never saw end of msg and sat there waiting for it.

Fixing the translate table resolved the problem. It works like a champ now, a little slow for a production environment but well enough to permit the prototype to go forward.

My Thanks again.

Michael
[ August 28, 2008: Message edited by: Michael DeChirico ]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Are you talking about this println?
System.out.println(messageIn);

As I understand the println will print the platform dependent linetermination in contrast to a System.out.print() which will not. So wasn't the problem actually in the incoming.readLine() where it was expecting to a line termination to return?
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Sorry that I can't help you out with this. I tried your code and I am getting the same results.

Have you figured out what's happening?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Originally posted by Gamini Sirisena:
Sorry that I can't help you out with this. I tried your code and I am getting the same results.

Have you figured out what's happening?


Did you read Michael's last message ?


Joanne
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
I apologise for this,
That response was meant for this thread

I seem to have mistakenly responded to this question instead. Since my reply didn't appear in the relevant thread I posted again and this can be verified by the time stamps of the posts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Someone tell me what I am doing wrong??