Hi everyone. I am new to programming and trying to learn as I go however am struggling here and any assistance please I greatly appreciate.
I am trying to send hardcoded xml tags to a server that only received xml tags for processing and this is the code I have which is not working. Please help.
public void Connect(string serverIP, string message)
{
string output = "";
try
{
// Create a TcpClient.
// The client requires a TcpServer that is connected
// to the same address specified by the server andport
// combination.
Int32 port = 4566;
TcpClient client = new TcpClient(serverIP, port);
// Translate the passed message into ASCII and store it as a byte array.
Byte[] data = new Byte[4000];
data = System.Text.Encoding.UTF8.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
//output = "Received: " + responseData;
//Response.Write(output);
// Close everything.
client.Close();
stream.Close();
}
catch (ArgumentNullException e)
{
output = "ArgumentNullException: " + e;
//MessageBox.Show(output);
Response.Write(output);
}
catch (SocketException e)
{
output = "SocketException: " + e.ToString();
//MessageBox.Show(output);
Response.Write(output);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// In this code example, use a hard-coded
// IP address and message.
string serverIP = "localhost";
string message = "<NHServerMessage protocol_version=\"1.0\"><BASIC_REQUEST_INFO request_type=\"S\" request_id=\"-1\"/><DATA_LIST_NAME value=\"names\"/><SEARCH_NAME SN=\"Bob\" GN=\"Anderson\"/></NHServerMessage>";
Connect(serverIP, message);
}
PLEASE HELP. THANKS A BUNCH!