在通用Windows平台(UWP)应用中,我尝试使用套接字将对象发送到Windows窗体程序:
$data = [
'param1' => urlencode($param1),
'param2' => urlencode($param2)
];
echo json_encode($data);
但接收对象卡在public void CreateListener()
{
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
while (true)
{
if (listener.Pending())
{
var childSocketThread = new Thread(() =>
{
string data = null;
var Client = listener.AcceptTcpClient();
using (NetworkStream ns = Client.GetStream())
using (StreamReader sr = new StreamReader(ns))
{
data = sr.ReadToEnd();
var Items = JsonConvert.DeserializeObject<Models.Orders>(data);
Process(Items);
ns.Flush();
ns.Close();
}
});
childSocketThread.IsBackground = true;
childSocketThread.Start();
}
else
{
Thread.Sleep(50); //<--- timeout
}
}
}
并且永不退出。
我该怎么办?
答案 0 :(得分:1)
当您想要发送数据时似乎有问题,在刷新StreamWriter对象后,您必须使用StreamReader作为响应。
Stream streamIn = socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
string response = await reader.ReadLineAsync();
希望这会对你有所帮助。