By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,274 Members | 2,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,274 IT Pros & Developers. It's quick & easy.

Problem sending Hexadecimal as part of a string using socket.send

P: n/a
Hi, I'm able to make connection to a server using socket connection.
However, when I send a command string the server just ignores it. All
command string needs to start with "0xF9" at Byte 0. During the run-time
debug, I see it to be "u" with a "~" on top of it. Is that OxF9? Can
someone tell me what I'm doing wrong?
Thanks, Alpha

private void Connect(String server, String message)
{
//Socket Connect
try
{
//create a new client socket ...
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
String szIPSelected = "192.168.1.7";
String szPort = "22333";
int alPort = System.Convert.ToInt16 (szPort,10);

System.Net.IPAddress remoteIPAddress =
System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new
System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
// String message = "Hello There";
// byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
// m_socClient.Send(byData);

}
catch (SocketException se)
{
MessageBox.Show ( se.Message );
}
//Send ATS Data
try
{
byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
m_socClient.Send (byData);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Receive ATS Data
// Receive the TcpServer.response.

try
{
byte [] buffer = new byte[256];
int iRx = m_socClient.Receive (buffer);
char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String Retmessage = new System.String(chars);
MessageBox.Show("Reply from ATS server: "+ Retmessage.ToString() ,
"ATS Reply", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Close Socket Connection
m_socClient.Close ();

}

private void menuItem9_Click(object sender, System.EventArgs e)
{
string Message;
Message = "\xF9" + "1800000007CATT,TO";
Connect("Map", Message);
}
Dec 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alpha,

0xF9 is definitely not in the ASCII character set, so it's very possible
that the character you see is correct. However, this doesn't mean that what
you are sending is wrong. It just is that the "u" character is the
representation of that byte. However, how the service interprets it is
completely irrelevant.

If you actually want to send the byte F9, then do not send it as a
string. Rather, create the byte array with this value yourself, and send
that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
Hi, I'm able to make connection to a server using socket connection.
However, when I send a command string the server just ignores it. All
command string needs to start with "0xF9" at Byte 0. During the run-time
debug, I see it to be "u" with a "~" on top of it. Is that OxF9? Can
someone tell me what I'm doing wrong?
Thanks, Alpha

private void Connect(String server, String message)
{
//Socket Connect
try
{
//create a new client socket ...
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
String szIPSelected = "192.168.1.7";
String szPort = "22333";
int alPort = System.Convert.ToInt16 (szPort,10);

System.Net.IPAddress remoteIPAddress =
System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new
System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
// String message = "Hello There";
// byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
// m_socClient.Send(byData);

}
catch (SocketException se)
{
MessageBox.Show ( se.Message );
}
//Send ATS Data
try
{
byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
m_socClient.Send (byData);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Receive ATS Data
// Receive the TcpServer.response.

try
{
byte [] buffer = new byte[256];
int iRx = m_socClient.Receive (buffer);
char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String Retmessage = new System.String(chars);
MessageBox.Show("Reply from ATS server: "+ Retmessage.ToString() ,
"ATS Reply", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Close Socket Connection
m_socClient.Close ();

}

private void menuItem9_Click(object sender, System.EventArgs e)
{
string Message;
Message = "\xF9" + "1800000007CATT,TO";
Connect("Map", Message);
}

Dec 24 '05 #2

P: n/a
Thank you Nicholas for the help. Can you show me how to send F9 and the rest
of my message in the bye array?

Thanks, Alpha

"Nicholas Paldino [.NET/C# MVP]" wrote:
Alpha,

0xF9 is definitely not in the ASCII character set, so it's very possible
that the character you see is correct. However, this doesn't mean that what
you are sending is wrong. It just is that the "u" character is the
representation of that byte. However, how the service interprets it is
completely irrelevant.

If you actually want to send the byte F9, then do not send it as a
string. Rather, create the byte array with this value yourself, and send
that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
Hi, I'm able to make connection to a server using socket connection.
However, when I send a command string the server just ignores it. All
command string needs to start with "0xF9" at Byte 0. During the run-time
debug, I see it to be "u" with a "~" on top of it. Is that OxF9? Can
someone tell me what I'm doing wrong?
Thanks, Alpha

private void Connect(String server, String message)
{
//Socket Connect
try
{
//create a new client socket ...
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
String szIPSelected = "192.168.1.7";
String szPort = "22333";
int alPort = System.Convert.ToInt16 (szPort,10);

System.Net.IPAddress remoteIPAddress =
System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new
System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
// String message = "Hello There";
// byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
// m_socClient.Send(byData);

}
catch (SocketException se)
{
MessageBox.Show ( se.Message );
}
//Send ATS Data
try
{
byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
m_socClient.Send (byData);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Receive ATS Data
// Receive the TcpServer.response.

try
{
byte [] buffer = new byte[256];
int iRx = m_socClient.Receive (buffer);
char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String Retmessage = new System.String(chars);
MessageBox.Show("Reply from ATS server: "+ Retmessage.ToString() ,
"ATS Reply", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
//Close Socket Connection
m_socClient.Close ();

}

private void menuItem9_Click(object sender, System.EventArgs e)
{
string Message;
Message = "\xF9" + "1800000007CATT,TO";
Connect("Map", Message);
}


Dec 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.