Consultor Eletrônico



Kbase P6384: How to use 4GL sockets to send a file from one machine to another
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   15/06/2010
Status: Verified

GOAL:

How to use 4GL sockets to send a file from one machine to another

GOAL:

Sample code for using 4GL sockets

FACT(s) (Environment):

All Supported Operating Systems
Progress 9.1x
OpenEdge 10.x

FIX:


/* The following socket client sends a file to a socket server */
/* To test this procedure, simply change ServerName and FileName */
/* string occurrences to valid server and file name values. */

DEFINE VARIABLE hSocket AS HANDLE NO-UNDO.
DEFINE VARIABLE lRC AS LOGICAL NO-UNDO.
DEFINE VARIABLE mData AS MEMPTR NO-UNDO.

PAUSE 0 BEFORE-HIDE.

CREATE SOCKET hSocket.

hSocket:CONNECT('-H ServerName -S 3333') NO-ERROR.

RUN SendFileToServer.

/*--------------------------------------------------------*/
/* Main loop: Wait for data to be sent from server */
/*--------------------------------------------------------*/

REPEAT ON STOP UNDO, LEAVE ON QUIT UNDO, LEAVE:
WAIT-FOR READ-RESPONSE OF hSocket.
END.

hSocket:DISCONNECT() NO-ERROR.

DELETE OBJECT hSocket.

SET-SIZE(mData) = 0.

QUIT.

PROCEDURE SendFileToServer:
/* Import data from file to the file's memory pointer */

DEFINE VARIABLE mFileData AS MEMPTR NO-UNDO.

FILE-INFO:FILE-NAME = "FileName".
SET-SIZE(mFileData) = FILE-INFO:FILE-SIZE.

INPUT FROM "FileName" BINARY NO-MAP NO-CONVERT.
IMPORT UNFORMATTED mFileData.
INPUT CLOSE.

/* Set size of memory pointer to write server socket */

SET-SIZE(mData) = 0.
SET-SIZE(mData) = FILE-INFO:FILE-SIZE + 4.
SET-BYTE-ORDER(mData) = BIG-ENDIAN.

/* Marshal data & header into the memory pointer */

PUT-LONG(mData,1) = FILE-INFO:FILE-SIZE.
PUT-BYTES(mData,5) = mFileData.

/* Write the whole thing to the server socket */

lRC = hSocket:WRITE(mData,1,FILE-INFO:FILE-SIZE + 4) NO-ERROR.

IF lRC = FALSE OR ERROR-STATUS:GET-MESSAGE(1) <> '' THEN
DO:
DISPLAY 'Unable To Write Detail Bytes'.
RETURN.
END.
END PROCEDURE.