我遇到了一个我自己无法解决的问题。
我有一个带有“价格”属性的ObservableCollection“product”。 每一秒,每件产品的价格都会发生变化。这是我的服务器部分。 我有一个带有一些文本框的窗口,它绑定在price属性上。
在另一方面,我有一个客户。客户需要获得所有产品的价格。
所以,首先,我的客户端连接到我的服务器(这里没问题)。它向服务器发送消息,服务器接收它。
我的问题在于:价格属性的价值每秒都在变化,但在我的帖子中,我无法获得新的价值......
这是我的代码: - 产品:
private volatile float price;
public float Price
{
get { return price; }
set
{
price = value;
notifyPropertyChanged("Price");
}
}
public Product(int time)
{
timer = new Timer(time);
timer.Elapsed += timer_Elapsed;
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Price++;
}
我的服务器:
private ObservableCollection<product> listGroupProduct;
public MainWindow()
{
Thread thread;
InitializeComponent();
this.DataContext = this;
// Create the server
thread = new Thread(() => createServer(ref listGroupProduct));
thread.Start();
}
public static void createServer(ref ObservableCollection<Product> list)
{
string client = "";
try
{
IPAddress ipAdress = IPAddress.Parse("192.168.1.50");
TcpListener listener = new TcpListener(ipAdress, 1220);
listener.Start();
socket = listener.AcceptSocket();
// Receive client name
client = ReceiveMessage(100);
MessageBox.Show("New client connected : " + client);
// Send number of products
SendMessage(list.Count.ToString());
// Get articles request from a client
ReceiveMessage(8);
// Send all articles
while (true)
{
for (int i = 0; i < 3; i++)
{
if (articlesString != "")
articlesString += "|";
articlesString += list[i].Price + ";";
}
byte[] bytes = new byte[list.Count * 50];
bytes = System.Text.Encoding.ASCII.GetBytes(articlesString.ToCharArray());
socket.Send(bytes);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private static void SendMessage(string p)
{
byte[] bytes = new byte[p.Length];
bytes = System.Text.Encoding.ASCII.GetBytes(p.ToCharArray());
socket.Send(bytes);
}
private static string ReceiveMessage(int p)
{
string tmp = "";
byte[] b = new byte[p];
int k = socket.Receive(b);
for (int i = 0; i < k; i++)
tmp += Convert.ToChar(b[i]);
return tmp;
}
我的客户:
private StreamSocket streamSocket;
public string Server = "192.168.1.89";
public int Port = 1220;
IInputStream inputStream;
IOutputStream outputStream;
public MainPage()
{
this.InitializeComponent();
CreateSocket();
}
void timer_Tick(object sender, object e)
{
SendMessage("articles");
toto.Text = "Message send : articles";
GetAllArticles();
}
private async void GetAllArticles()
{
toto.Text = await GetMessage(50);
toto.Text = "Waiting articles...";
toto.Text = await GetMessage(articlesNumber * 50));
}
private async Task CreateConnection()
{
SendMessage("tablet");
toto.Text = "message send : tablet";
articlesNumber = int.Parse(await GetMessage(1));
toto.Text = "Number articles : " + articlesNumber.ToString();
}
private async void CreateSocket()
{
DispatcherTimer timer = new DispatcherTimer();
streamSocket = new StreamSocket();
await streamSocket.ConnectAsync(new HostName(Server), Port.ToString());
inputStream = streamSocket.InputStream;
outputStream = streamSocket.OutputStream;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
// Envoi du nom et réception du nombre d'articles
await CreateConnection();
// Réception de tous les articles chaque secondes
SendMessage("tablet");
timer.Start();
}
private async void SendMessage(string message)
{
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
await outputStream.WriteAsync(buffer);
}
private async Task<string> GetMessage(int size)
{
byte[] tmp = new byte[size];
IBuffer buffer1 = CryptographicBuffer.CreateFromByteArray(tmp);
toto.Text = "Waiting message... (size : " + size.ToString() + ")";
await inputStream.ReadAsync(buffer1, (uint)size, InputStreamOptions.None);
toto.Text = "Message received !";
return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer1);
}
(顺便说一下:“toto”是我用来调试的文本框:))
您知道为什么我的客户端能够很好地获得第一个值,但是当我在服务器端更改值时,我的客户端会继续获得相同的值而不是新值吗?
答案 0 :(得分:0)
看起来服务器具有无限循环并且一遍又一遍地重新发送相同的数据。在“发送所有文章”评论后,CreateServer立即拥有无限循环。