I have a program in c# that connects asynchronously to a server. It connects without a problem but I do not get any response messages back from the server.
The port number I am trying to connect to is above 32767.
I have managed to create a server and the client can connect to the server successfully and receive messages for a port on my machine with a low number.
All the code I have seen uses Convert.ToInt16(Txtportno.Text)
Obviously this should be ToInt32 but do I need to do anything with the values above 32767? In the old vb code it used htons (HostToNetworkOrder) to convert the port no.
Does the C# need to use the HostToNetwork conversion?
Why else would my OnDataReceived never get called?
The relevant code is :
[tt]
private void cmdConnect_Click(object sender, System.EventArgs e)
{
try
{
EnableCommands(true);
//create the socket instance...
m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );
IPHostEntry ipHostInfo = Dns.Resolve(txtIPAddr.Text);
// get the remote IP address...
IPAddress ip = ipHostInfo.AddressList[0];
IPHostEntry myHost = Dns.GetHostByAddress(ip);
int iPortNo = 0;
iPortNo = System.Convert.ToInt32( txtPort.Text);
//create the end point
IPEndPoint ipEnd = new IPEndPoint (ip, iPortNo);
//connect to the remote host...
m_socClient.Connect ( ipEnd );
if (!m_socClient.Connected)
{
MessageBox.Show("Winsock error: "
+ Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
}
m_socClient.Blocking = false;
EnableCommands(false);
//watch for data ( asynchronously )...
WaitForData();
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
EnableCommands(true);
}
}
public void WaitForData()
{
try
{
if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = m_socClient;
// now start to listen for any data...
m_asynResult = m_socClient.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnCallBack, theSocPkt);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
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 + "]";
WaitForData();
}
catch (ObjectDisposedException )
{ System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
[/tt]
NB. I cannot use a lower port number, I am replacing existing functionality.
The port number I am trying to connect to is above 32767.
I have managed to create a server and the client can connect to the server successfully and receive messages for a port on my machine with a low number.
All the code I have seen uses Convert.ToInt16(Txtportno.Text)
Obviously this should be ToInt32 but do I need to do anything with the values above 32767? In the old vb code it used htons (HostToNetworkOrder) to convert the port no.
Does the C# need to use the HostToNetwork conversion?
Why else would my OnDataReceived never get called?
The relevant code is :
[tt]
private void cmdConnect_Click(object sender, System.EventArgs e)
{
try
{
EnableCommands(true);
//create the socket instance...
m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );
IPHostEntry ipHostInfo = Dns.Resolve(txtIPAddr.Text);
// get the remote IP address...
IPAddress ip = ipHostInfo.AddressList[0];
IPHostEntry myHost = Dns.GetHostByAddress(ip);
int iPortNo = 0;
iPortNo = System.Convert.ToInt32( txtPort.Text);
//create the end point
IPEndPoint ipEnd = new IPEndPoint (ip, iPortNo);
//connect to the remote host...
m_socClient.Connect ( ipEnd );
if (!m_socClient.Connected)
{
MessageBox.Show("Winsock error: "
+ Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
}
m_socClient.Blocking = false;
EnableCommands(false);
//watch for data ( asynchronously )...
WaitForData();
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
EnableCommands(true);
}
}
public void WaitForData()
{
try
{
if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = m_socClient;
// now start to listen for any data...
m_asynResult = m_socClient.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnCallBack, theSocPkt);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
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 + "]";
WaitForData();
}
catch (ObjectDisposedException )
{ System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
[/tt]
NB. I cannot use a lower port number, I am replacing existing functionality.