场景:使用C#开发的VB6应用程序和Windows服务。目标是让VB6应用程序通过TCP与服务进行通信。
我在服务中实现了一个TcpListener:
if (listener == null)
{
listener = new TcpListener(IPAddress.Any, 64827);
listener.Start();
}
State = State.Running;
Logger.Instance.Log.Info("ListenerHandler: Waiting for a connection on port 64827.....");
while (true)
{
if (State == State.Running)
{
using (TcpClient c = listener.AcceptTcpClient())
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
// handle the message and construct return value;
string retVal = "Return value..."
BinaryWriter w = new BinaryWriter(n);
w.Write(result);
w.Flush();
Logger.Instance.Log.Info(string.Format("Awnser sent: {0}", result));
}
}
}
我还用C#创建了一个简单的客户端来测试服务:
using (TcpClient client = new TcpClient(ipaddress, port))
using (NetworkStream n = client.GetStream())
{
string msg = messages[counter];
Console.WriteLine();
Console.WriteLine("{0} Message sent: {1}", GetTimeStamp(), msg);
BinaryWriter w = new BinaryWriter(n);
w.Write(msg);
w.Flush();
Console.WriteLine("{0} Recieved: {1}", GetTimeStamp(), new BinaryReader(n).ReadString());
counter++;
if (counter % 3 == 0)
counter = 0;
}
这一切都有效。消息由客户端发送并接收服务器,反之亦然。
在VB6代码中使用WinSock对象。如果来自VB6团队的人员尝试与服务器通信,则服务器会记录以下错误: 无法读取超出流的末尾
我是否必须在服务器中进行更改以使WinSock正常工作,或者VB6团队是否必须进行一些更改?
感谢所有帮助!