Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to Network Ports greater than 32767

Status
Not open for further replies.

datavalue

Programmer
Nov 8, 2002
12
CA
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.
 
What does IPEndPoint.MaxPort say? According to the docs, if you pass a port number greater than that, you get an ArgumentOutOfRangeException.

You might want to check for personal firewall software, etc., that might be running.

You don't need the Hton/Ntoh functions for this. You /may/ need this for passing your data, but that's an application issue.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top