smitht
12-21-2005, 07:15 PM
I have a file with one record. I've been using the fget statement with the (WHILE) command. When my C program finish process, it show 0 records process. I need a command in C that only reads one record.
/*
* (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 -0 fxrssamp -Insl -I socket
* REDHAT LlNUX 7.2 cc fxrssamp.c -0 fxrssamp
* WINDOWS VISUAL STUDIO 6 Be sure to include WSOCK32.lib in link
* IBM AIX 3.4 /bin/xlc_r fxrssamp.c -0 fxrssamp
* HPUX 11.0 /opt/aCC/bin/aCC +W829,749 +DA1.1 fxrssamp.c -0 fxrssamp
*
*/
#ifdef WIN32
#include <winsock.h>
#include <io.h>
#define CLOSE_SOCKET closesocket
#else
#include <unistd.h> /* openO, closeO, writeO, readO */
#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 "199.82.243.115"
#define PSPSERVER_PORT 2000
/*
int ConnectPSPServer(int *sd, char *hostNm, short portNbr);
void reportError(char *str);
*/
int ConnectPSPServer();
void reportError();
main(argc,argv)
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)
*/
int ConnectPSPServer(sd, hostNm, portNbr)
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 = Ox0101;
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 lookup failed");
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)
*/
void reportError (str)
char *str;
{
int errValue;
#ifdef WIN32
errValue = WSAGetLastError();
#else
errValue = errno;
#endif
printf ("ERROR: %s [err=%d]\n", str, errValue);
return;
}
/*
* (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 -0 fxrssamp -Insl -I socket
* REDHAT LlNUX 7.2 cc fxrssamp.c -0 fxrssamp
* WINDOWS VISUAL STUDIO 6 Be sure to include WSOCK32.lib in link
* IBM AIX 3.4 /bin/xlc_r fxrssamp.c -0 fxrssamp
* HPUX 11.0 /opt/aCC/bin/aCC +W829,749 +DA1.1 fxrssamp.c -0 fxrssamp
*
*/
#ifdef WIN32
#include <winsock.h>
#include <io.h>
#define CLOSE_SOCKET closesocket
#else
#include <unistd.h> /* openO, closeO, writeO, readO */
#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 "199.82.243.115"
#define PSPSERVER_PORT 2000
/*
int ConnectPSPServer(int *sd, char *hostNm, short portNbr);
void reportError(char *str);
*/
int ConnectPSPServer();
void reportError();
main(argc,argv)
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)
*/
int ConnectPSPServer(sd, hostNm, portNbr)
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 = Ox0101;
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 lookup failed");
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)
*/
void reportError (str)
char *str;
{
int errValue;
#ifdef WIN32
errValue = WSAGetLastError();
#else
errValue = errno;
#endif
printf ("ERROR: %s [err=%d]\n", str, errValue);
return;
}