0

I'm trying to develop a class that connect to iSeries (as400 - PGM Program) by sockets. The connection works fine but when I try to send data and receive it's get an error.

This is the code:

class Program
{
    public static void StartClient()
    {
        byte[] bytes = new byte[1024];
        string desaip = "10.112.2.11";

        // Connect to a remote device.
        try
        {

            IPAddress[] ipAddress = Dns.GetHostAddresses(desaip);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress[0], 42125);

            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Connect the socket to the remote endpoint. Catch any errors.
            try
            {
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected to {0}",
                    sender.RemoteEndPoint.ToString());


                #region forma A
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("PRUEBAIB");


                // Send the data through the socket.
                int bytesSent = sender.Send(msg);

                // Receive the response from the remote device.
                sender.ReceiveTimeout = 10000;
                int bytesRec = sender.Receive(bytes);

                Console.WriteLine("Echoed test = {0}",
                    Encoding.ASCII.GetString(bytes, 0, bytesRec));

                // Release the socket.
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
                #endregion forma A


            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}

What could be wrong?

Harald K
  • 26,314
  • 7
  • 65
  • 111
Kenjfg
  • 1
  • 6
  • 1
    Are you sure you want to implement this on the socket level? Surely there's a library available that abstracts this? If you want help with this, you should show the actual errors you receive. – CodeCaster Jul 28 '14 at 21:54
  • Actually the program gets stuck waiting for a response. – Kenjfg Jul 28 '14 at 22:11
  • What software is listening to that port on the iSeries (or IBM i on Power) server? Did you check that the program is active and listening? Did you check with the security administrator that there is no firewall blocking you? – WarrenT Jul 29 '14 at 01:50
  • To add to @CodeCaster answer, depending on what you're trying to accomplish, you might even find it easier to use something as simple as FTP for infrequent or low volume transactions -- you can even call remote programs on the AS400 if you're sending data to be processed. Sounds pretty archaic and there may be more elegant solutions out there, but it can work pretty reliably ... again, depending on the problem you're trying to solve and the time you have available to spend. – Scott Prokopetz Jul 29 '14 at 03:54
  • 1
    `...when I try to send data and receive it's get an error.` What gets an error? What error? What statement? It's extremely difficult to address an "error" without even knowing what it is. What happens on the other end of the socket? Is anything received? Is it correct? Is anything sent back out? Does it arrive as expected? – user2338816 Jul 29 '14 at 06:53

1 Answers1

0

Check the software that is supposed to be listening to that port on the iSeries. Make sure that it is running, that it is listening to that port. Work with that job, and look at the job log to see what messages were sent. Check the program stack to see what statement it is on, which may help determine what has happened. The open files & I/O stats might also help.

WarrenT
  • 4,502
  • 19
  • 27