C#アプリケーションをJavaベースのローカルサービスに接続しようとしています。私はJavaコードとサービスの詳細にアクセスできません。私が知っている唯一のことは、指定されたポートを介してソケットを介して通信することです。
問題は、接続できてもデータを送受信できないことです。私のC#クライアントコードは次のとおりです。
=====================
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();
=====================
接続性を示すポートキャプチャツールを使用してみましたが、送信データの長さが0であることが示されています。
助けてくれてありがとう。
=======更新(TcpClinetを使用して送信)==========
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();
========================================
Flush()を使用しましたが、アプリケーションを閉じるまでデータは送信されません。