8 Replies Latest reply: Aug 7, 2012 2:11 AM by 950703 RSS

    C# developed client,to connection osb socket proxy service

    950703
      hi friends , i have a osb ,which have a socket proxy service to connection to outsize socket service, then i develop a Client with C#,which to connection this proxy,my ip and port is right,but it never worked.i got the log in server,show me connection is right ,but server read out time. i test the proxy debug,all right.

      what's importance is that,i have a java client ,which is in the osb socket transport provider sample(http://docs.oracle.com/cd/E23943_01/dev.1111/e15866/example.htm#BABEEHGF) and works all right, i have not any idea,pls help me.
        • 1. Re: C# developed client,to connection osb socket proxy service
          vladodias
          Hi,

          I understand you're trying to port the java client to C#, should be ok, I believe the problem may be some minor issue when you trying to mimic the Java behavior in C#, but is very difficult to figure out without seeing the code...

          Cheers,
          Vlad
          • 2. Re: C# developed client,to connection osb socket proxy service
            950703
            java client:

            package com.bea.alsb.transports.sample.test;

            import java.io.IOException;
            import java.io.InputStream;
            import java.io.InputStreamReader;
            import java.io.OutputStream;
            import java.net.Socket;
            import java.util.Date;

            /**
            * This class can be used to send request messages to the ALSB socket
            * proxyservice and receive the response.
            */
            public class TestClient {

            private static final String MSG = "I am a Rimbaud with a leather jacket, " +
            "If my poetry aims to achieve anything, it's to deliver people from the limited ways in which they see and feel" +
            "-- the Lizard King -----JM";
            private String message = MSG;
            private String fileEnc;
            private String host;
            private int port;
            private int threadCt;

            public TestClient(String msg, String host, int port, int threadCt,
            String fileEnc) {
            this.host = host;
            this.port = port;
            this.threadCt = threadCt;
            this.fileEnc = fileEnc;
            if(msg != null) {
            this.message = msg;
            }
            }

            private void sendToSocketTransport() {
            try {
            SendReceiveTask target = new SendReceiveTask(host, port);
            Thread[] threads = new Thread[threadCt];

            for (int i = 0; i < threadCt; i++) {
            threads = new Thread(target);
            }

            for (int i = 0; i < threadCt; i++) {
            threads.start();
            }

            } catch (Exception e) {
            e.printStackTrace();
            }
            }

            private static void debugSocket(Socket sock) {
            log("----> sock.getPort() = " + sock.getPort());
            log(
            "----> sock.getRemoteSocketAddress() = " + sock.getRemoteSocketAddress());
            log("----> sock.getLocalSocketAddress() = " + sock.getLocalSocketAddress());
            log("----> sock.getInetAddress() = " +
            sock.getInetAddress().getCanonicalHostName());
            log("----> sock.getLocalPort() = " + sock.getLocalPort());
            }

            private static void log(String msg) {
            System.out.println("<"> " + msg);
            }

            public static void main(String[] args) {

            if (args.length < 3) {
            log("Wrong Usage.");
            log(
            "Usage is : java -Dfile-encoding=<file-encoding> " +
            "-Dresponse-encoding=<response-encoding> " +
            "com.bea.alsb.transports.sample.test.TestClient " +
            "<host-name> <port> <thread-ct> <message-file-location> \n" +
            "where host-name is host name of the ALSB\n" +
            "port is the port number at which proxy service is listening to,\n" +
            "therad-ct is the number of threads/clients \n" +
            "message-file-location (optional argument)is the location of " +
            "the message-file, " +
            "which will be sent as a response to the business service.\n" +
            "file-encoding is is the encoding of the file. " +
            "response-encoding is a system property which is the encoding of the" +
            "response received from the socket proxy service. " +
            "Both these properties have a default value of utf-8\n\n");
            System.exit(1);
            }

            String host = args[0];
            int port = Integer.parseInt(args[1]);
            int threadCt = Integer.parseInt(args[2]);
            log("----> host = " + host);
            log("----> port = " + port);
            log("----> threadCt = " + threadCt);
            String msg = null;
            String fileEnc = System.getProperty("file-encoding", "utf-8");
            log("----> file-encoding = "+fileEnc);
            if (args.length > 3) {
            msg = TestUtil.getMessage(args[3], fileEnc);
            }
            TestClient testClient = new TestClient(msg, host, port, threadCt, fileEnc);
            testClient.sendToSocketTransport();
            }

            class SendReceiveTask implements Runnable {
            private String host;
            private int port;

            public SendReceiveTask(String host, int port) {
            this.host = host;
            this.port = port;
            }

            public void run() {
            try {
            Socket sock = new Socket(host, port);
            debugSocket(sock);
            OutputStream outputStream = sock.getOutputStream();
            InputStream inputStream = sock.getInputStream();
            InputStreamReader inputStreamReader;
            StringBuilder sb;
            int index;
            try {
            outputStream.write(message.getBytes(fileEnc));
            outputStream.write(TestUtil.D_CRLF.getBytes(fileEnc));
            outputStream.flush();
            log("Sent a message to the server on thread: " +
            Thread.currentThread().getName());
            String resEnc = System.getProperty("response-encoding", "utf-8");
            inputStreamReader = new InputStreamReader(inputStream, resEnc);
            sb = new StringBuilder();
            char[] buff = new char[512];
            String doubleCRLF = new String(TestUtil.D_CRLF.getBytes(resEnc));
            while (true) {
            index = inputStreamReader.read(buff);
            if (index == -1) {
            break;
            }
            sb.append(buff, 0, index);
            if ((index = sb.lastIndexOf(doubleCRLF)) != -1) {
            break;
            }
            }
            } finally {
            sock.close();
            }

            if (index == -1) {
            log("Illegal response message format or it might be one-way.");
            return;
            }
            String response = sb.toString().substring(0, index);
            log("----> response for thread: " + Thread.currentThread().getName() +
            "= " + response);
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }
            }
            *java util:*

            package com.bea.alsb.transports.sample.test;

            import java.io.File;
            import java.io.FileInputStream;
            import java.io.IOException;
            import java.io.InputStreamReader;

            /**
            * This is an Util class which is used by TestServer and TestClient.
            */
            public class TestUtil {
            public static final String D_CRLF = "\r\n\r\n";

            /**
            * Returns a String of the contents in the given file location. It replaces
            * multiple \r\n to asignle \r\n.
            *
            * @param fileLocation
            * @return String of the content of the given file.
            */
            public static String getMessage(String fileLocation, String enc) {
            File file = new File(fileLocation);
            if (!file.exists()) {
            throw new IllegalArgumentException(
            "message-file-fileLocation :" + fileLocation + " does not exist.");
            }

            StringBuilder msg = new StringBuilder();
            InputStreamReader inputStreamReader = null;
            try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file), enc);
            int ct = 0;
            char[] charBuffer = new char[512];
            while ((ct = inputStreamReader.read(charBuffer)) != -1) {
            msg.append(charBuffer, 0, ct);
            }
            } catch (IOException e) {
            e.printStackTrace();
            } finally {
            try {
            if(inputStreamReader != null) {
            inputStreamReader.close();
            }
            } catch (IOException ignore) {
            }
            }
            String str = msg.toString();
            /** replacing multiple \r\n to a signle \r\n */
            str=str.replaceAll("(\r\n)+", "\r\n");
            return str;
            }

            }





            *my C# client code,i just need to test:*


            StringBuilder str = new StringBuilder();
            str.Append("<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>");
            str.Append("<ssssssss1111/>");
            str.Append("</soapenv:Body>");
            TcpClient client = new TcpClient();
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.196"), 7778);
            client.Connect(serverEndPoint);

            ASCIIEncoding encoder = new ASCIIEncoding();
            NetworkStream clientStream = client.GetStream();

            byte[] buffer = encoder.GetBytes(str.ToString());
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();

            i just try to use TCP and stream to send my data , just like java code.

            Are there any encoding error or socket error in C# code?

            帖子经 947700编辑过
            • 3. Re: C# developed client,to connection osb socket proxy service
              vladodias
              Hi,

              I'm not as "fluent" in C# as I'm in Java, but it seems that you are just writing and not reading anything back, the code in java writes, reads and then closes the socket. The server may be waiting for the client to do all this...

              Cheers,
              Vlad

              It is considered good etiquette to reward answerers with points (as "helpful" - 5 pts - or "correct" - 10pts)
              https://forums.oracle.com/forums/ann.jspa?annID=893
              • 4. Re: C# developed client,to connection osb socket proxy service
                950703
                hi vladodias , i know what you say,because i just need the server receive the request, ,but is did not work ...the java client code just the demo,not very fit our scene,our sever has not any response back.

                i debug the server code, it didn't get any request. and the osb log say: read out time...

                what wrong...with it..?
                • 5. Re: C# developed client,to connection osb socket proxy service
                  950703
                  i found the java code use the 'char[]' as buffer , is it the point?
                  • 6. Re: C# developed client,to connection osb socket proxy service
                    950703
                    it need someone here..
                    • 7. Re: C# developed client,to connection osb socket proxy service
                      vladodias
                      I think you should be using Sockets and not TcpClient... Something like this...
                      http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/3/
                      our sever has not any response back
                      Have you modified the server? Or you are using the sample?
                      java client ,which is in the osb socket transport provider sample ... works all right
                      Using what code in the server, the sample or the modified version?

                      If you modified the server and the client, you don't know where the problem is... I suggest you create a C# client that does exactly what the sample Java Client does and try and make it work first... Then you can go from there... Otherwise there will be so many variables that you will struggle to figure out where the problem is...

                      Cheers,
                      Vlad

                      It is considered good etiquette to reward answerers with points (as "helpful" - 5 pts - or "correct" - 10pts)
                      https://forums.oracle.com/forums/ann.jspa?annID=893
                      • 8. Re: C# developed client,to connection osb socket proxy service
                        950703
                        i used the sample server code
                        谢谢(thanks), i will check it once more...