What do you mean by reference?
Main Topics
Browse All TopicsGreetings,
I am in dire need of help to get this code to work in a JDK 1.1, 1.2 platform... The following code connects to a fedex server and uploads a string of data and then returns the servers response. This definately works right now on 1.4.2 on my local machine, however I need to use this in Oracle 8i which has an older version of Java (1.2 or 1.1)...
The socket.shutdownOutput() method does not exist in this version. Without explicitly shutting down the output, the server keeps waiting for more data and never sends a resopnse.
I have looked all over google and ee for suggetions and answersa and nothing has worked. I have no ability to modify the serverside of this!
I have also tried to do this in PL/SQL via UTL_TCP with the same issue. However, there is no equivalent to the socket.shutdownOutput(); method so it just sits there like a lazy dog boy.
I need a way to get this functioning!!!
HELP!
import java.net.*;
import java.io.*;
public class xxxTTT {
public static void main (String[] args) throws Exception {
try {
char dq = (char)34;
String data = "example"
String hostname = "xx.xx.xx.xx";
int port = 2000;
InetAddress addr = InetAddress.getByName(host
Socket socket = new Socket(addr, port);
// Specify the timeout.
socket.setSoTimeout(5000);
// Send data to socket
PrintWriter wr = new PrintWriter(socket.getOutp
wr.println(data);
wr.flush();
socket.shutdownOutput();
// Get response from the fed ex server!
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.g
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
wr.close();
rd.close();
socket.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok,
I tried both suggestions and neither work... when closing the reference (out.close), it closes the socket and I have nothing to read. I am attaching the modified source... It will work by commenting the out.close and using the shutdownOutput() though...
import java.net.*;
import java.io.*;
import java.lang.*;
public class XXXX {
public static void main (String[] args) throws Exception {
try {
char dq = (char)34;
String data = "example";
String hostname = "XX.XX.xx.xx";
int port = 2000;
InetAddress addr = InetAddress.getByName(host
Socket socket = new Socket(addr, port);
// Specify the timeout.
socket.setSoTimeout(1000);
// Send data to socket
// PrintWriter wr = new PrintWriter(socket.getOutp
OutputStream out = socket.getOutputStream();
PrintWriter wr = new PrintWriter(out, true);
//wr.println(data);
wr.println(data);
wr.flush();
out.close();
// socket.shutdownOutput();
// Get response from the fed ex server!
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.g
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
wr.close();
rd.close();
socket.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
ack.. I checked out the java source for the socket and of course it is a native method for shutdownOutput(). Did you write/control the socket server? If so just code a special character for the "stop listening and give me some output" event. If you don't control it (I think you said it is FedEx), have you asked them "how do I get it to stop listening?"
I am waiting for a response to see if there is some sort of termination sequence that it is expecting... However, I didn't see that evidenced in the documentation they provide online... Here is the "sample" source code from a C version of the program they include in the Appendix...
using TCP/IP protocol.
/*
* (c) Copyright FedEx 1998-2002
*
*
* Ship Manager Business Architecture Development
* Custom Integrated Solutions
* September 1998
*
*
*
* DESCRIPTION: Sample socket client to connect to the Ship Manager Plus Server.
* Inputs: <input file> <output file>
* Outputs: <output file>
*
* COMMENTS: change the #define PSPSERVER_HOSTNAME to match the PSPServer
* hostname as defined in your /etc/hosts file.
*
* COMPILE:
* SOLARIS 2.6 cc fxrssamp.c -o fxrssamp -lnsl -lsocket
* REDHAT LINUX 7.2 cc fxrssamp.c -o fxrssamp
* WINDOWS VISUAL STUDIO 6 Be sure to include WSOCK32.lib in link
* IBM AIX 3.4 /bin/xlc_r fxrssamp.c -o fxrssamp
* HPUX 11.0 /opt/aCC/bin/aCC +W829,749 +DA1.1 fxrssamp.c -o fxrssamp
*
*/
#ifdef WIN32
#include <winsock.h>
#include <io.h>
#define CLOSE_SOCKET closesocket
#else
#include <unistd.h> /* open(), close(), write(), read() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#define CLOSE_SOCKET close
#endif
#include <stdio.h>
#include <string.h>
/***** SET YOUR TEST HOST HERE *****/
#define PSPSERVER_HOSTNAME "yourFedExServerName.yourc
#define PSPSERVER_PORT 2000
int ConnectPSPServer(int *sd, char *hostNm, short portNbr);
void reportError (char *str);
int main(int argc, char **argv)
{
int sd, i=0;
FILE *inFile, *outFile;
char buffer[4096];
if(argc != 3)
{
printf("USAGE: %s <trans in file> <trans out file>\n", argv[0]);
return 1;
}
printf("Test Client of FedEx Ship Manager Plus Server started.\n");
printf("Attempting to connect to: %s:%d\n", PSPSERVER_HOSTNAME, PSPSERVER_PORT);
if(!ConnectPSPServer(&sd, PSPSERVER_HOSTNAME, PSPSERVER_PORT))
{
printf("Error: Cannot connect to %s\n", PSPSERVER_HOSTNAME);
return 2;
}
printf("Connected to %s:%d\n", PSPSERVER_HOSTNAME, PSPSERVER_PORT);
inFile = fopen(argv[1], "r");
/* error checking ignored for simplicity */
outFile = fopen(argv[2], "wa");
/* error checking ignored for simplicity */
printf("Reading request transactions from: %s\n", argv[1]);
printf("Writing reply transactions to: %s \n", argv[2]);
fgets(buffer, sizeof(buffer), inFile); /* read transaction request */
while(!feof(inFile))
{
int rc;
rc=send(sd, buffer, strlen(buffer),0); /* write request to server */
if (rc < 0)
reportError("Write to server failed");
memset (buffer, 0, sizeof buffer);
rc=recv(sd, buffer, sizeof buffer,0); /* read transaction reply */
if (rc <= 0)
{
if (rc == 0)
printf ("socket was closed by server\n");
else
reportError("Reading response failed");
}
fputs(buffer, outFile); /* write reply to file */
fputc('\n', outFile);
i++;/* increase reply count */
if(i % 10 == 0)
printf("%d", i);
else
printf(".");
fflush(stdout);/* stdout normally flushes with each \n character */
fgets(buffer, sizeof(buffer), inFile); /* read transaction request */
}
fclose(outFile);
fclose(inFile);
if(CLOSE_SOCKET(sd) == 0) /* Close connection */
printf("\nConnection to %s terminated.\n", PSPSERVER_HOSTNAME);
printf("%d transactions processed.\n", i);
return 0;
}
int ConnectPSPServer(int *sd, char *hostNm, short portNbr)
{
struct hostent *hp;
struct sockaddr_in sin;
#ifdef WIN32
WORD winsock_ver;
WSADATA winsock_data;
int rc;
winsock_ver = 0x0101;
rc = WSAStartup(winsock_ver, &winsock_data);
if (rc != 0)
return 0;
#endif
/* Set up the socket descriptor and the address */
memset(&sin, 0, sizeof(struct sockaddr_in));
*sd = socket(AF_INET, SOCK_STREAM, 0);
if (*sd < 0)
{
reportError("socket call failed");
return 0;
}
hp = gethostbyname(hostNm);
if (hp == NULL)
{
reportError("gethostbyname
return 0;
}
memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
sin.sin_port = htons(portNbr);
sin.sin_family = hp->h_addrtype;
/* Make the connection */
if(connect(*sd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == -1)
{
reportError("connect to host failed");
return 0;/*FALSE*/
}
return 1;/*TRUE*/
}
void reportError (char *str)
{
int errValue;
#ifdef WIN32
errValue = WSAGetLastError();
#else
errValue = errno;
#endif
printf ("ERROR: %s [err=%d]\n", str, errValue);
return;
I will try your suggest CEHJ, I also have another piece of info that may help you guys help me :) (THANKS ALOT NO MATTER WHAT!)
Anyway, when I was cutting and pasting the C code, I noticed that they allocate 4096 bytes to the write... I decided to try to fill up the buffer... and tested > 4096 and <4096
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
// total buffer size 4410 //
System.out.println("*** 1 ***");
The end result was that if I run this code "no close or setoutputoff... It works, albeit strangely since I am sending mutliple requests... Anyway, I think I may need to just fill up the buffer with 4096 - data.length() empty spaces or garbage character...
If one of you could help me with that code ( I AM TOTALLY NOT A JAVA PROGRAMMER) it would be awesome!
import java.net.*;
import java.io.*;
import java.lang.*;
public class XXXedex {
public static void main (String[] args) throws Exception {
try {
char dq = (char)34;
String data = "test";
String hostname = "XXXXX5";
int port = 2000;
InetAddress addr = InetAddress.getByName(host
Socket socket = new Socket(addr, port);
// Specify the timeout.
socket.setSoTimeout(1000);
// Send data to socket
// PrintWriter wr = new PrintWriter(socket.getOutp
OutputStream out = socket.getOutputStream();
PrintWriter wr = new PrintWriter(out, true);
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
wr.println(data); //441
// total buffer size 4410 //
System.out.println("*** 1 ***");
wr.flush();
// out.close();
//socket.shutdownOutput();
// Get response from the fed ex server!
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.g
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
wr.close();
rd.close();
socket.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
Ok, got it... Bad news is that this also works if you use the shutdownOutput statement. I tried it sans the out.write(0) as well... Any other suggestions???
// Send data to socket
OutputStream out = socket.getOutputStream();
int buf = -1;
StringReader in = new StringReader(data);
while ((buf = in.read()) > -1) {
out.write(buf);
}
out.write(0);
//out.close();
//socket.shutdownOutput();
I just finished with fed-ex support... I don't know if support is really the term that applies :)! Anyway, they had no suggestion to help... The 99,"" is a termination sequence that is expected by the server... Using the shutdownOutput command, I receive a response and that can only happen if the transaction is complete which demonstrates that my test transaction is syntactically correct.
I am really at a loss...
Wait if you send the characters 99,"" fedex calls the transaction complete? The "file" you are sending to them has a published spec right? That file must have some type of "end" record.. which means that if you are sending the file in the right format the socket shouldn't have to be shutdown to start receiving a response. Is there anyway you can ask them to give you a debugish environment. Can you confirm with them what they are actually receiving? Maybe there is some stupid encoding problem?
The transaction I send has a specific format. The sequence 99,"" is a terminator for a transaction. The fact that I am getting a valid response indicates that my transaction contains is valid as defined by fed-ex.
The support person i spoke to says that 99,"" is the terminator the server is looking for... I can conclusively show that my transaction contains the terminator and the failure to respond is not due to a malformed transaction...
This just takes us back to the fact that without the shutdownOutput method, I can't get the reply...
Okay so are you certain that fedex is receiving the 99,"" terminator? If not, the shutdownOutput method could merely be doing something like a buffer flush (I know you have the flush in your code) at a lower level. I would make sure that they are receiving what you think they are. You could snoop the tcp packets and confirm you are sending what you think you are, if they can't help you.
I am changing the printwriter code when I get the chance... It works but it leaves a strange termination character I don't like. Anyway, I split the points again because I found everyones help very usefull. The solution was actually very simple and I am happy it is behind me.
The error turned out to be on the read of the response. Using what I learned from CEHJ
int buf = -1;
StringReader in = new StringReader(data);
while ((buf = in.read()) > -1) {
out.write(buf);
}
out.write(0);
I modified this to see the effect of the multiple sends I did when I saw a resposne. I realized by seeing the ascii stream coming back that the termination was not what I expected. I then corrected it and it has been working great ever since!!!
You all rock!
Business Accounts
Answer for Membership
by: CEHJPosted on 2005-10-03 at 14:04:42ID: 15009893
Try just closing the output stream. You need a reference to it first though