[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

AS400 To Windows Socket hanging up

Asked by CantonMarko in Programming for iSeries / AS400

Tags: as400

I have a major problem trying to communicate to an AS400 through a windows socket.  

The AS400 is programmed to send a message to a port on a Windows 2003 server.  My program picks up that message and depending on what the message is, we process some stroed procedures on our SQL server and then send back a message to the AS400.  Depending on what the AS400 sends us back we do more processing until the AS400 finally sends us a message of "QUIT".  Once this "QUIT" message is received, we want to go back to listening on the port and waiting for another message.

The problem comes in that when we receive the "QUIT" from the AS400 it looks like we go back to listening, however, we never receive the next message from the AS400.  The AS400 program just hangs there like it is waiting for our response.  Once I shut down the listening app on the Windows 2003 server, the AS400 job completes, with errors of course, because we didn't send it any data.

I have worked on this for 2 weeks and cannot get any answers or resolution.  The AS400 programmer says it cannot be on his side, that it has to be in Windows, but I have no idea.  I am new to working with sockets and I inherited this code from a former programmer.  Any help anyone can offer would be appreciated.

Here is my code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;

namespace SocketServer
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            public AsyncCallback pfnWorkerCallBack ;
            public  Socket m_socListener;
            public  Socket m_socWorker;
            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox txtPortNo;
            private System.Windows.Forms.Button cmdListen;
            private System.Windows.Forms.TextBox txtDataRx;
            private System.Windows.Forms.GroupBox groupBox2;
            private System.Windows.Forms.GroupBox groupBox3;
            private System.Windows.Forms.TextBox txtDataTx;
            private System.Windows.Forms.Button button1;
            public string rxText;
            private System.Windows.Forms.TextBox textBox1;
                        
            
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.groupBox1 = new System.Windows.Forms.GroupBox();
                  this.cmdListen = new System.Windows.Forms.Button();
                  this.txtPortNo = new System.Windows.Forms.TextBox();
                  this.label1 = new System.Windows.Forms.Label();
                  this.txtDataRx = new System.Windows.Forms.TextBox();
                  this.groupBox2 = new System.Windows.Forms.GroupBox();
                  this.button1 = new System.Windows.Forms.Button();
                  this.txtDataTx = new System.Windows.Forms.TextBox();
                  this.groupBox3 = new System.Windows.Forms.GroupBox();
                  this.textBox1 = new System.Windows.Forms.TextBox();
                  this.groupBox1.SuspendLayout();
                  this.groupBox2.SuspendLayout();
                  this.SuspendLayout();
                  //
                  // groupBox1
                  //
                  this.groupBox1.Controls.Add(this.cmdListen);
                  this.groupBox1.Controls.Add(this.txtPortNo);
                  this.groupBox1.Controls.Add(this.label1);
                  this.groupBox1.Location = new System.Drawing.Point(8, 16);
                  this.groupBox1.Name = "groupBox1";
                  this.groupBox1.Size = new System.Drawing.Size(264, 48);
                  this.groupBox1.TabIndex = 0;
                  this.groupBox1.TabStop = false;
                  this.groupBox1.Text = "Settings";
                  //
                  // cmdListen
                  //
                  this.cmdListen.Location = new System.Drawing.Point(144, 16);
                  this.cmdListen.Name = "cmdListen";
                  this.cmdListen.Size = new System.Drawing.Size(104, 24);
                  this.cmdListen.TabIndex = 2;
                  this.cmdListen.Text = "Start Listening";
                  this.cmdListen.Click += new System.EventHandler(this.cmdListen_Click);
                  //
                  // txtPortNo
                  //
                  this.txtPortNo.Location = new System.Drawing.Point(96, 16);
                  this.txtPortNo.Name = "txtPortNo";
                  this.txtPortNo.Size = new System.Drawing.Size(40, 20);
                  this.txtPortNo.TabIndex = 1;
                  this.txtPortNo.Text = "11000";
                  //
                  // label1
                  //
                  this.label1.Location = new System.Drawing.Point(16, 16);
                  this.label1.Name = "label1";
                  this.label1.Size = new System.Drawing.Size(72, 16);
                  this.label1.TabIndex = 0;
                  this.label1.Text = "Port Number:";
                  //
                  // txtDataRx
                  //
                  this.txtDataRx.Location = new System.Drawing.Point(8, 264);
                  this.txtDataRx.Multiline = true;
                  this.txtDataRx.Name = "txtDataRx";
                  this.txtDataRx.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                  this.txtDataRx.Size = new System.Drawing.Size(272, 80);
                  this.txtDataRx.TabIndex = 1;
                  this.txtDataRx.Text = "";
                  //
                  // groupBox2
                  //
                  this.groupBox2.Controls.Add(this.button1);
                  this.groupBox2.Controls.Add(this.txtDataTx);
                  this.groupBox2.Location = new System.Drawing.Point(8, 72);
                  this.groupBox2.Name = "groupBox2";
                  this.groupBox2.Size = new System.Drawing.Size(272, 152);
                  this.groupBox2.TabIndex = 2;
                  this.groupBox2.TabStop = false;
                  this.groupBox2.Text = "Send Data";
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(16, 120);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(232, 24);
                  this.button1.TabIndex = 1;
                  this.button1.Text = "Send";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // txtDataTx
                  //
                  this.txtDataTx.Cursor = System.Windows.Forms.Cursors.IBeam;
                  this.txtDataTx.Location = new System.Drawing.Point(8, 16);
                  this.txtDataTx.Multiline = true;
                  this.txtDataTx.Name = "txtDataTx";
                  this.txtDataTx.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                  this.txtDataTx.Size = new System.Drawing.Size(240, 96);
                  this.txtDataTx.TabIndex = 0;
                  this.txtDataTx.Text = "";
                  //
                  // groupBox3
                  //
                  this.groupBox3.Location = new System.Drawing.Point(0, 240);
                  this.groupBox3.Name = "groupBox3";
                  this.groupBox3.Size = new System.Drawing.Size(288, 112);
                  this.groupBox3.TabIndex = 3;
                  this.groupBox3.TabStop = false;
                  this.groupBox3.Text = "Data Received";
                  //
                  // textBox1
                  //
                  this.textBox1.Location = new System.Drawing.Point(144, 224);
                  this.textBox1.Name = "textBox1";
                  this.textBox1.Size = new System.Drawing.Size(136, 20);
                  this.textBox1.TabIndex = 4;
                  this.textBox1.Text = "textBox1";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(296, 349);
                  this.Controls.Add(this.textBox1);
                  this.Controls.Add(this.groupBox2);
                  this.Controls.Add(this.txtDataRx);
                  this.Controls.Add(this.groupBox1);
                  this.Controls.Add(this.groupBox3);
                  this.Name = "Form1";
                  this.Text = "Socket Server in C#";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.groupBox1.ResumeLayout(false);
                  this.groupBox2.ResumeLayout(false);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void cmdListen_Click(object sender, System.EventArgs e)
            {
                  Listen();
            }

            public void Listen()
            {
                  try
                  {
                        
                        //create the listening socket...
                        m_socListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            
                        IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,11000);
                        //bind to local IP Address...
                        m_socListener.Bind( ipLocal );
                        //start listening...
                        m_socListener.Listen (6);
                        // create the call back for any client connections...
                        m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
                        cmdListen.Enabled = false;

                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show ( se.Message + " IN lISTEN" );
                  }
            }

            public void OnClientConnect(IAsyncResult asyn)
            {
                  try
                  {
                        m_socWorker = m_socListener.EndAccept (asyn);
                         
                        WaitForData(m_socWorker,asyn);
                  }
                  catch(ObjectDisposedException)
                  {
                        System.Diagnostics.Debugger.Log(0,"1","\n OnClientConnection: Socket has been closed\n");
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show ( se.Message + " IN OnClientConnect"  );
                  }
                  
            }
            public class CSocketPacket
            {
                  public System.Net.Sockets.Socket thisSocket;
                  public byte[] dataBuffer = new byte[9];
            }

            public void WaitForData(System.Net.Sockets.Socket soc, IAsyncResult asyn)
            {
                  try
                  {
                        if  ( pfnWorkerCallBack == null )
                        {
                              pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
                        }
                        CSocketPacket theSocPkt = new CSocketPacket ();
                        theSocPkt.thisSocket = soc;
                        // now start to listen for any data...
                        soc .BeginReceive (theSocPkt.dataBuffer ,0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnWorkerCallBack,theSocPkt);
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show (se.Message + " IN WaitForData"  );
                  }

            }

            public  void OnDataReceived(IAsyncResult asyn)
            {
                  try
                  {
                        
                        CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
                        //end receive...
                        int iRx  = 0 ;
                        iRx = theSockId.thisSocket.EndReceive (asyn);
                        char[] chars = new char[iRx +  1];
                        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                        int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                        System.String szData = new System.String(chars);
                        txtDataRx.Text = txtDataRx.Text + szData;
                        if(txtDataRx.Text.EndsWith("QUIT" + Environment.NewLine))
                        {
                              txtDataTx.Text = "221\n";
                              Object objData = txtDataTx.Text;
                              byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
                              m_socWorker.Send (byData);
                              pfnWorkerCallBack = null;
                              txtDataRx.Text = null;
                              txtDataTx.Text = null;
                                          
                        }
                        
                              WaitForData(m_socWorker,asyn );
                        
                  }
                  catch (ObjectDisposedException )
                  {
                        System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show (se.Message + " IN OnDataReceived"  );
                  }
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        Object objData = txtDataTx.Text;
                        byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
                        m_socWorker.Send (byData);
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show (se.Message + " IN button1_click"  );
                  }
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
            
            }


      }
}
[+][-]12/29/06 11:10 AM, ID: 18215807Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/29/06 11:25 AM, ID: 18215896Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/29/06 11:34 AM, ID: 18215954Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/29/06 11:45 AM, ID: 18215998Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/29/06 11:50 AM, ID: 18216021Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/29/06 12:26 PM, ID: 18216195Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/29/06 12:28 PM, ID: 18216204Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/29/06 12:31 PM, ID: 18216223Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/29/06 12:43 PM, ID: 18216270Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/29/06 02:40 PM, ID: 18216853Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/02/07 08:50 AM, ID: 18228862Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/02/07 01:11 PM, ID: 18230994Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/02/07 01:29 PM, ID: 18231143Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/02/07 02:02 PM, ID: 18231509Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/02/07 02:07 PM, ID: 18231555Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/02/07 03:09 PM, ID: 18232064Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/03/07 08:56 AM, ID: 18236690Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/03/07 12:28 PM, ID: 18238569Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Programming for iSeries / AS400
Tags: as400
Sign Up Now!
Solution Provided By: tliotta
Participating Experts: 3
Solution Grade: A
 
[+][-]01/23/07 05:29 PM, ID: 18381564Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89