I am trying to connect my C# application with a local service that is based on Java. I dont have access to the Java code and the service details, the only thing I know is to communicate via socket through a specified port.
The problem is I can connect BUT cannot send / receive any data. My C# client code is as follows;
=====================
IPHostEntry ipAddress = Dns.GetHostEntry("127.0.0.1");
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipAddress.AddressList[0].ToString()), 8888);
socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ip);
Encoding ASCII = Encoding.ASCII;
Byte[] byteGetString = ASCII.GetBytes("Service_Command_Message");
Byte[] receivedBytes = new Byte[256];
string l_Response = string.Empty;
socket.Send(byteGetString, byteGetString.Length, SocketFlags.None);
//debugger gets lost at this line. I also tried stream.read
Int32 bytes = socket.Receive(receivedBytes, receivedBytes.Length, SocketFlags.None);
l_Response += ASCII.GetString(receivedBytes, 0, bytes);
while (bytes > 0)
{
bytes = socket.Receive(receivedBytes, receivedBytes.Length, SocketFlags.None);
l_Response = l_Response + ASCII.GetString(receivedBytes, 0, bytes);
}
socket.Close();
=====================
I tried using some port capture tool, that shows the connectivity but length of transmitted data is shown to be 0.
Thanks for any help.
======= update (using TcpClinet to send) ==========
client = new TcpClient("127.0.0.1", 8888);
stream = client.GetStream();
Byte[] data = System.Text.Encoding.ASCII.GetBytes("Service_Command_Message");
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine(p_Message);
sw.Flush();
========================================
I've used Flush() but still data is not transmitted till I close the application.