장기 실행 스크립트 또는 원격 컴퓨터에서 실행되는 스크립트에서 중앙 위치의 메시지 또는 데이터를 수집하는 것이 때때로 유용합니다.이를 설명하는 네 개의 파일이 첨부되어 있습니다.
- 다른 기계나 프로세스에서 메시지를 수집하여 데이터 테이블에 저장하는 '서버'. 다른 스크립트나 테이블과 별도의 JMP 인스턴스에서 실행합니다.
- 서버에 메시지를 보내는 '클라이언트' JSL 스크립트, 이를 스크립트에 포함합니다.
- 서버에 메시지를 보내는 '클라이언트' R 스크립트
- 서버에 메시지를 보내는 '클라이언트' 파이썬 스크립트
다음은 이것이 어떻게 보이는지에 대한 예입니다. 오른쪽의 클라이언트는 다른 컴퓨터에 있을 수도 있고 동일한 컴퓨터에 있는 별도의 JMP 복사본일 수도 있습니다.

중요한 코드는 다음과 같습니다.
섬기는 사람
//Create a socket
con = Socket();
//Use a specific address and port on this computer
con << Bind( "10.0.0.2", "12345" );
//Tell this socket to be ready for other sockets to connect to it
con << Listen();
//Verify that the connection is active
con << getsockname;
//When something connects to this socket, call a function to handle
//the connection. Accept connections for the next 5 minutes.
con << Accept(callbackfn, 300);
//Now messages can be sent to this connection.
//When all done, close the connection
con << close();
콜백 함수는 다음과 같습니다(실제 함수는 더 복잡합니다. 아래 참조).
//Simplified function to handle communication with the client, do not use as is
callbackfn = function( {x},
If( x[2] == "ok",
//Get data
r = x[4] << recv( 1000 );
//While data was received
While( r[2] == "ok",
//Write it to the log
write( Blob to Char( r[3] ) );
//try to get more data
r = x[4] << recv( 1000 );
);
);
//close the connection to the client
x[4] << Close;
);
다음은 차단(공통)을 처리하는 약간 더 복잡한 콜백 함수입니다.
더보기...
다음은 완전한 서버 코드의 예입니다.
더보기...
Names default to here( 1 );
//Table to hold received data
dtLog = New Table( "Log",
New Column( "Time", Numeric, "Continuous", Format( "m/d/y h:m:s", 23, 0 ),
Input Format( "m/d/y h:m:s", 0 ) ),
New Column( "Client", Character, "Nominal" ),
New Column( "Message", Character, "Nominal" )
);
//Wait for and get data from remote machine
receivedata = Function( {c, waittime = 1, waitcount = 10},
//Varaible to store data received from remote machine
blobdata = Char To Blob( "" );
//Make socket is connected
r = c << GetPeerName();
//While receiving data or waiting to receive data
i = 0;
While(
(
(r[2] == "ok") |
((blobdata == Char To Blob( "" )) & (Left(r[2], 11) == "WOULDBLOCK:" ))
) &
(i < waitcount),
i++;
//Get data
r = c << recv( 100000 );
//If no data is received yet, wait
If(Left(r[2], 11) == "WOULDBLOCK:", Wait( waittime ) );
//Write whatever is received to the log
Write( Blob to Char( r[3] ) );
//join data to whatever is already received
blobdata = blobdata || r[3];
);
return( blobdata );
);
//Get log info from remote machine, store it in a data table
processlogconnection = function( {c},
//New row in log table
dtLog << Add Rows( 1 );
rowout = N Rows( dtLog );
Column( dtLog, "Time" )[rowout] = Today();
//Get computer name of remote machine
r = c << GetPeerName();
Column( dtLog, "Client" )[rowout] = r[3];
//Get message from the remote machine
Column( dtLog, "Message" )[rowout] = Blob to Char( receivedata( c ) );
//Close the connection
c << Close;
);
//Function to accept conections from remote machine, calls another
//function to handle communication with that machine
callbackfn = function( {x},
//Call another function with the connection returned from the Accept message
If( x[2] == "ok", processlogconnection( x[4] ) );
);
//Create a socket
con = Socket();
//Use a specific port on this computer
con << Bind( "10.0.0.2", "12345" );
//Tell this socket to be ready for other sockets to connect to it
con << Listen();
//Verify that the connection is active
con << getsockname;
//When something connects to this socket, call a function to handle
//the connection. Accept connections for the next 5 minutes.
con << Accept(callbackfn, 300);
//Now messages can be sent to this connection.
//When all done, close the connection
con << close();
이름의 기본값은 here( 1 )입니다. //수신된 데이터를 보관할 테이블 dtLog = New Table( "Log", New Column( "Time", Numeric, "Continuous", Format( "m/d/yh:m:s", 23, 0 ), Input Format ( "m/d/yh:m:s", 0 ) ), 새 열( "클라이언트", 문자, "명목" ), 새 열( "메시지", 문자, "명목" ) ); //원격 컴퓨터에서 데이터를 대기하고 가져옵니다. receivedata = Function( {c, waittime = 1, waitcount = 10}, //원격 컴퓨터에서 받은 데이터를 저장할 변수 blobdata = Char To Blob( "" ); //소켓 만들기 연결됨 r = c << GetPeerName(); //데이터 수신 중 또는 데이터 수신 대기 중 i = 0; While( ( ( (r[2] == "ok") | ((blobdata == Char To Blob( " " )) & (Left(r[2], 11) == "WOULDBLOCK:" )) ) & (i < waitcount), i++; //데이터 가져오기 r = c << recv( 100000 ); //없으면 데이터가 아직 수신되지 않은 경우 대기 If(Left(r[2], 11) == "WOULDBLOCK:", Wait( waittime ) ); //수신된 내용을 로그에 기록 Write( Blob to Char( r[3] ) ); //이미 수신된 데이터에 결합 blobdata = blobdata || r[3]; ); return( blobdata ); ); //원격 시스템에서 로그 정보를 가져와 데이터 테이블에 저장 processlogconnection = function( {c}, //로그 테이블의 새 행 dtLog << Add Rows( 1 ); rowout = N Rows( dtLog ); Column( dtLog , "Time" )[rowout] = Today(); //원격 시스템의 컴퓨터 이름 가져오기 r = c << GetPeerName(); Column( dtLog, "Client" )[rowout] = r[3]; //Get 원격 시스템의 메시지 Column( dtLog, "Message" )[rowout] = Blob to Char( receivedata( c ) ); //연결을 닫습니다 c << Close; ); //원격 기계로부터 연결을 수락하는 함수, 해당 기계와의 통신을 처리하기 위해 //다른 함수 호출 callbackfn = function( {x}, //수락 메시지에서 반환된 연결로 다른 함수 호출 If( x[2] == "ok", processlogconnection( x[4] ) ); ); //소켓 생성 con = Socket(); //이 컴퓨터의 특정 포트 사용 con << Bind( "10.0.0.2", "12345" ); //다른 소켓이 연결할 준비가 되도록 이 소켓에 알립니다. con << Listen(); //연결이 활성 상태인지 확인 con << getsockname; //무언가가 이 소켓에 연결되면 //연결을 처리하는 함수를 호출합니다. 다음 5분 동안 연결을 수락합니다. con << 수락(callbackfn, 300); //이제 이 연결로 메시지를 보낼 수 있습니다. //모두 완료되면 연결을 닫습니다. con << close();
JSL 클라이언트
Names default to here( 1 );
//function to send a message to a waiting connection
SendMessage = Function( {message},
//Create a socket
con = Socket();
//Connect to a socket that is already bound to, listening on, and accepting
//connections on a port. This is the server IP address, not the client.
con << Connect( "10.0.0.2", "12345" );
//Send data
r = con << Send( Char To Blob( message ) );
///check that the data was sent
if(r[2] == "ok", result = "sent", result = "fail" );
//close the connection
con << Close;
return( result );
);
//send messages
Write( SendMessage( "Hi" ) );
R 클라이언트
sendmessage = function( message ) {
con <- socketConnection(host="10.0.0.2", port = 12345, blocking=FALSE,
server=FALSE, open="r+")
write_resp <- write(message, con)
close(con)
}
sendmessage("Hi from R")
파이썬 클라이언트
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("10.0.0.2", 12345))
client_socket.send("Hello from Python")
client_socket.close()
break;
파워쉘 클라이언트
$socket = new-object System.Net.Sockets.TcpClient( "10.0.0.2", 12345)
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
$writer.WriteLine( "Hello from Powershell`n" )
$writer.Flush()
$writer.Close()
이것은 기능적인 로깅 메커니즘보다 예제 코드를 찾는 사람을 위한 소켓 통신에 대한 소개에 가깝지만 귀하의 의견과 제안을 환영합니다!