does anyone have a example how to use %IO.ServerSocket, %IO.Socket, ... .
It is not so funny to analyse the code ...
like the example for COS in
http://docs.intersystems.com/cache20071/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_cjob#RCOS_B29124
$ZV=Cache for Windows (Intel) 2007.1 (Build 369) Fri Jun 15 2007 15:25:42
EDT
Thanks in advance for your help.
Alexander
Class teste.ClientSocket Extends %RegisteredObject
{
ClassMethod connect()
{
set sock = ##class(%IO.Socket).%New()
set st = $$$OK
w "Starting", !
// connecting, param:
// address, port, timeout, status
w sock.Open("127.0.0.1","7777",-1, st), !
w st, !
// reading from socket, param:
// maxStringSize, timeout, status, lineTerminator
w sock.ReadLine(50, 30, st, $c(13)), !
// writing to socket, param:
// stringToBeWritten, automaticFlush, status
d sock.Write("We are the champions", 1, st)
quit
}
ClassMethod server()
{
set sock = ##class(%IO.ServerSocket).%New()
set st = $$$OK
w "Starting server...", !
// open serverSocket, param:
// port, timeOut, status
w sock.Open(7777, -1, st), !
w st, !
// server to listen incoming connections, param:
// timeOut, status
w sock.Listen(-1, st), !
w st, !
// writing to socket, param:
// stringToBeWritten, automaticFlush, status
d sock.Write("I am the champion", 1, st)
// reading from socket, param:
// maxStringSize, timeout, status, lineTerminator
w sock.ReadLine(50, 30, st, $c(13)), !
w sock.Close(), !
w "Closing server...", !
}
}
On Jul 18, 12:53 pm, "Alexander Neuber" <alexander.neu...@leichum.com>
wrote:
> Hi,
>
> does anyone have a example how to use %IO.ServerSocket, %IO.Socket, ... .
>
> It is not so funny to analyse the code ...
>
> like the example for COS inhttp://docs.intersystems.com/cache20071/csp/docbook/DocBook.UI.Page.c...
Thank you
Alexander
<eeya...@gmail.com> schrieb im Newsbeitrag
news:1184857466.7...@n2g2000hse.googlegroups.com...
response: 00 00 00 00 00 05 09 03 02 00 05
The following javacode is working fine...But i dont know how to convert this one to our cache...using %IO.Socket...
javacode:
// test3.java 7/23/97 - JAVA program to read registers via gateway
// compile as
// javac test3.java
// run as
// java test3 aswales1.modicon.com 1 2 3
import java.io.* ;
import java.net.* ;
import java.util.*;
class Modbus {
public static void main(String argv[]) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("IP-Address: ");
String ip_adrs = br.readLine();
System.out.print("Slave ref: ");
int unit = Integer.parseInt(br.readLine());
System.out.print("Function code: ");
int fun_code=Integer.parseInt(br.readLine());
System.out.print("Register ref: ");
int reg_no = Integer.parseInt(br.readLine());
System.out.print("Length : ");
int num_regs = Integer.parseInt(br.readLine());
System.out.println("ip_adrs = "+ip_adrs+" unit = "+unit+" fun_code = "+fun_code+" reg_no = "+
reg_no+" num_regs = "+num_regs);
// set up socket
Socket es = new Socket(ip_adrs,502);
OutputStream os= es.getOutputStream();
FilterInputStream is = new BufferedInputStream(es.getInputStream());
byte obuf[] = new byte[261];
byte ibuf[] = new byte[261];
int c = 0;
int i;
// build request of form 0 0 0 0 0 6 ui 3 rr rr nn nn
for (i=0;i<5;i++) obuf[i] = 0;
obuf[5] = 6;
obuf[6] = (byte)unit;
obuf[7] = (byte)fun_code;
obuf[8] = (byte)(reg_no >> 8);
obuf[9] = (byte)(reg_no & 0xff);
obuf[10] = (byte)(num_regs >> 8);
obuf[11] = (byte)(num_regs & 0xff);
// send request
os.write(obuf, 0, 12);
// read response
i = is.read(ibuf, 0, 261);
if (i<9) {
if (i==0) {
System.out.println("unexpected close of connection at remote end");
} else {
System.out.println("response was too short - "+i+" chars");
}
} else if (0 != (ibuf[7] & 0x80)) {
System.out.println("MODBUS exception response - type "+ibuf[8]);
} else if (i != (9+2*num_regs)) {
System.out.println("incorrect response size is "+i+
" expected"+(9+2*num_regs));
} else {
for (i=0;i<num_regs;i++) {
int w = (ibuf[9+i+i]<<8) + ibuf[10+i+i];
System.out.println("word "+i+" = "+w);
}
}
System.out.println();
System.out.print("Request : ");
for(i=0;i<12;i++)
{
System.out.print(obuf[i]+" ");
}
System.out.println();
System.out.print("Response : ");
for(i=0;i<10;i++)
{
System.out.print(ibuf[i]+" ");
}
// close down
es.close();
} catch (Exception e) {
System.out.println("exception :"+e);