Home arrow static arrow Java Programming [Archive] - Socket Programming from Windows to AS400
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Socket Programming from Windows to AS400
This topic has 9 replies on 1 page.

Posts:5
Registered: 8/2/04
Socket Programming from Windows to AS400  
Aug 2, 2004 11:59 PM



 
Hello all,
I'm trying to pull data from a AS400 System, the problem is that i do not get proper responses for the first time, i get some irrelevant responces in the begining and after few attempts i get proper response. I tried to follow few tips given in the forum, could not suceed in tracing the problem. Could any one help me out. I have attached the code

strUrl = System.getProperty("AS400connecturl");
if(strUrl != null)
{
url = new URL(strUrl);
}
conn = url.openConnection();
if(conn != null)
{
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);

wr = new OutputStreamWriter(conn.getOutputStream());
}
if(wr != null)
{
wr.write(data);
wr.flush();
}

// Get the response
if(conn != null)
{
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
if(rd != null)
{
while ((line = rd.readLine()) != null)
{
if(line!=null && line.length() >0)
responseLine=line;
}

}
wr.close();
rd.close();
 

Posts:31,095
Registered: 4/30/99
Re: Socket Programming from Windows to AS400  
Aug 3, 2004 8:43 AM (reply 1 of 9)



 
Sure, just send your AS/400 over to my place. Make sure you let me know how to start your server as well. Then I will be able to debug your code.

Seriously, though, you're on your own. That looks like reasonable code to read from a URL, except for all the unnecessary "if (something != null)" tests which don't do any harm. But you're writing data to the URL as well; maybe you're writing the wrong data. Or maybe the server is returning the wrong data. Could be any number of things. It's time to stop programming and start debugging.
 

Posts:5
Registered: 8/2/04
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 10:20 AM (reply 2 of 9)



 
Hy,
I checked it out by putting logs, the problem is with the response and not with the data i am sending.Could any on elet me know how to ensure that i get proper response for my request.
Thanks
 

Posts:31,095
Registered: 4/30/99
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 11:26 AM (reply 3 of 9)



 
Could any on elet me know how to ensure that i get proper response for my request.

What the server sends you IS the proper response for your request -- unless it's wrong, in which case somebody will need to fix the code that runs the server. Or unless you aren't getting any response due to some technical network problem. Could be just about anything based on the details you have posted.
 

Posts:5
Registered: 8/2/04
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 12:23 PM (reply 4 of 9)



 
hy thanks,
What i ment was that the application is not getting a new connection for each of my new requests and that is the reason i am geting unwanted responses in the begining, Is there any way i can ensure that the conn object is new for each and every request i send, And how to clase the connection once the response is obtained.
Thanks
Prinse
 

Posts:31,095
Registered: 4/30/99
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 1:50 PM (reply 5 of 9)



 
Oh, you think there might be something wrong with your code. That's very likely. Could you post it so it's formatted properly? The way you did it, with no indentation or anything, makes it unreadable.
 

Posts:5
Registered: 8/2/04
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 2:18 PM (reply 6 of 9)



 
strUrl = System.getProperty("AS400connecturl");if(strUrl != null){	url = new URL(strUrl);} conn = url.openConnection(); if(conn != null){	conn.setUseCaches(false);	conn.setDefaultUseCaches(false);	conn.setDoInput(true);	conn.setDoOutput(true); 	wr = new OutputStreamWriter(conn.getOutputStream());} if(wr != null){	wr.write(data);	wr.flush();} // Get the responseif(conn != null){	rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));} if(rd != null){	while ((line = rd.readLine()) != null) 	{		if(line!=null && line.length() >0)		responseLine=line;	} } wr.close();rd.close(); 
 

Posts:31,095
Registered: 4/30/99
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 2:31 PM (reply 7 of 9)



 
That's some weird if-logic there. You assign a value to a variable inside an if-statement (so the assignment might not take place), then you proceed to use that variable as if the assignment had taken place. I would rewrite it like this:
strUrl = System.getProperty("AS400connecturl");if(strUrl != null){  url = new URL(strUrl);  conn = url.openConnection();  // the documentation doesn't suggest that openConnection() ever returns null  // but this code can't hurt  if(conn != null)  {    conn.setUseCaches(false);    conn.setDefaultUseCaches(false);    conn.setDoInput(true);    conn.setDoOutput(true);    wr = new OutputStreamWriter(conn.getOutputStream());    // note: wr cannot be null here    wr.write(data);    wr.flush();    // Get the response    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));    // likewise rd cannot be null here    while ((line = rd.readLine()) != null)     {      // note: line cannot be null here      if(line.length() >0)        responseLine=line;    }    // note: you only extract the last line of the response, is that what you wanted?    wd.close();    rd.close();  }}
 

Posts:5
Registered: 8/2/04
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 2:36 PM (reply 8 of 9)



 
Hi ,
Thanks A Ton for the reply, You were right i need the last line in the response and the code you have mentioned looks the optimum way to write the code, will follow it. Can you plz tell me how to get a new connection each time i send the request. And how to close the connection once the response is obtained.
Thanks
Prinse
 

Posts:31,095
Registered: 4/30/99
Re: Socket Programming from Windows to AS400  
Aug 4, 2004 3:13 PM (reply 9 of 9)



 
Can you plz tell me how to get a new
connection each time i send the request.

The code already does that.

And how to close the connection once the response is obtained.

You don't have to. Closing the input and output streams is sufficient.
 
This topic has 9 replies on 1 page.