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.Group
Box groupBox1;
private System.Windows.Forms.Label
label1;
private System.Windows.Forms.TextB
ox txtPortNo;
private System.Windows.Forms.Butto
n cmdListen;
private System.Windows.Forms.TextB
ox txtDataRx;
private System.Windows.Forms.Group
Box groupBox2;
private System.Windows.Forms.Group
Box groupBox3;
private System.Windows.Forms.TextB
ox txtDataTx;
private System.Windows.Forms.Butto
n button1;
public string rxText;
private System.Windows.Forms.TextB
ox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
ainer 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.Group
Box();
this.cmdListen = new System.Windows.Forms.Butto
n();
this.txtPortNo = new System.Windows.Forms.TextB
ox();
this.label1 = new System.Windows.Forms.Label
();
this.txtDataRx = new System.Windows.Forms.TextB
ox();
this.groupBox2 = new System.Windows.Forms.Group
Box();
this.button1 = new System.Windows.Forms.Butto
n();
this.txtDataTx = new System.Windows.Forms.TextB
ox();
this.groupBox3 = new System.Windows.Forms.Group
Box();
this.textBox1 = new System.Windows.Forms.TextB
ox();
this.groupBox1.SuspendLayo
ut();
this.groupBox2.SuspendLayo
ut();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Ad
d(this.cmd
Listen);
this.groupBox1.Controls.Ad
d(this.txt
PortNo);
this.groupBox1.Controls.Ad
d(this.lab
el1);
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.c
mdListen_C
lick);
//
// 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.Scrol
lBars.Vert
ical;
this.txtDataRx.Size = new System.Drawing.Size(272, 80);
this.txtDataRx.TabIndex = 1;
this.txtDataRx.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.Ad
d(this.but
ton1);
this.groupBox2.Controls.Ad
d(this.txt
DataTx);
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.b
utton1_Cli
ck);
//
// txtDataTx
//
this.txtDataTx.Cursor = System.Windows.Forms.Curso
rs.IBeam;
this.txtDataTx.Location = new System.Drawing.Point(8, 16);
this.txtDataTx.Multiline = true;
this.txtDataTx.Name = "txtDataTx";
this.txtDataTx.ScrollBars = System.Windows.Forms.Scrol
lBars.Vert
ical;
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.tex
tBox1);
this.Controls.Add(this.gro
upBox2);
this.Controls.Add(this.txt
DataRx);
this.Controls.Add(this.gro
upBox1);
this.Controls.Add(this.gro
upBox3);
this.Name = "Form1";
this.Text = "Socket Server in C#";
this.Load += new System.EventHandler(this.F
orm1_Load)
;
this.groupBox1.ResumeLayou
t(false);
this.groupBox2.ResumeLayou
t(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.Inter
Network,So
cketType.S
tream,Prot
ocolType.T
cp);
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(IAsyncResu
lt asyn)
{
try
{
m_socWorker = m_socListener.EndAccept (asyn);
WaitForData(m_socWorker,as
yn);
}
catch(ObjectDisposedExcept
ion)
{
System.Diagnostics.Debugge
r.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.Soc
kets.Socke
t 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.Le
ngth ,SocketFlags.None,pfnWorke
rCallBack,
theSocPkt)
;
}
catch(SocketException se)
{
MessageBox.Show (se.Message + " IN WaitForData" );
}
}
public void OnDataReceived(IAsyncResul
t asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncS
tate ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndRe
ceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.
GetDecoder
();
int charLen = d.GetChars(theSockId.dataB
uffer, 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.To
String ());
m_socWorker.Send (byData);
pfnWorkerCallBack = null;
txtDataRx.Text = null;
txtDataTx.Text = null;
}
WaitForData(m_socWorker,as
yn );
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugge
r.Log(0,"1
","\nOnDat
aReceived:
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.To
String ());
m_socWorker.Send (byData);
}
catch(SocketException se)
{
MessageBox.Show (se.Message + " IN button1_click" );
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}