%IO.ServerSocket, %IO.Socket, ..

207 views
Skip to first unread message

Alexander Neuber

unread,
Jul 18, 2007, 11:53:50 AM7/18/07
to intersystems...@info2.kinich.com
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 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

eeya...@gmail.com

unread,
Jul 19, 2007, 11:04:26 AM7/19/07
to intersystems.public.cache
Just made a simple one. I hope it´s usefull.

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...

Alexander Neuber

unread,
Jul 20, 2007, 5:56:41 AM7/20/07
to intersystems...@info2.kinich.com
Yes, it ist very usefull.

Thank you
Alexander

<eeya...@gmail.com> schrieb im Newsbeitrag
news:1184857466.7...@n2g2000hse.googlegroups.com...

udhay kumar

unread,
Feb 13, 2015, 3:38:52 AM2/13/15
to intersystems...@googlegroups.com
Hi ,
       
     Your code is very useful...
     
      But I have a doubt..? 

     How can i generate sample packet to send the server and receive the response.(Modbus protocol)
     
     I just specify the protocol specification and java code. can anyone help me to convert this one to our cache.

     request:    00 00  00  00  00  06  09  03  00  04  00  01

     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);

Reply all
Reply to author
Forward
0 new messages