c#, socket,数据理应在调用socket.send之后发出去,但实际上调用socket.close之后才发送出去。全网找了2天答应,都没解决。谢谢各位。
try
{
string message = "device=camera;item=ois;command=start;\\r\\n";
byte[] bytes = Encoding.ASCII.GetBytes(message);
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 80;
hostAddress = IPAddress.Parse("127.0.0.1");
hostEndPoint = new IPEndPoint(hostAddress, 9103);
// Creates the Socket to send data over a TCP connection.
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ReceiveTimeout = 1000*15;
// Connect to the host using its IPEndPoint.
s.Connect(hostEndPoint);
if (!s.Connected)
{
// Connection failed, try next IPaddress.
Console.WriteLine("Unable to connect to host");
}
s.Send(bytes);
s.Close();
} // End of the try block.
你是怎么测得这个结论的呢,中间加了sleep延时10秒吗
否则send和close之间没有任何代码,中间到底是否有阻塞你是怎么得知的呢
send只是将数据送到发送缓冲区,至于网卡什么时候将数据真正发出去,那是物理层决定的,不归应用层管
另,有发送就有接收,接收也是你写的对吗,把接收的代码放出来看看,是不是连接的时候一直在阻塞接收,要到断开之后才print
我是用单步调试来试的。就算不设断点,直接运行,也是用把程序关闭之后,硬件才收到指令。同样的思路,python跟c++是没有问题的。因为我现在只是在试发送,没有写接收的代码。
不知道你这个问题是否已经解决, 如果还没有解决的话: