Witam
Chce napisać program który wysyła do serwera zapytanie co 1 sekunde a ten odpowiada, na podstawie informacji w internecie udało mi się napisać kod lecz program mi się lekko zawiesza no i niedziała.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace testwa
{
public partial class Form1 : Form
{
TcpClient tcpclnt = new TcpClient();
Timer timer = new Timer();
byte[] BuforOut = new byte[] { 0x03, 0x03, 0x00, 0x00, 0x00, 0x0a, 0xc4, 0x2f };
byte[] BuforIn = new byte[50];
public Form1()
{
InitializeComponent();
tcpclnt.ReceiveTimeout = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 1000;
}
private void button1_Click(object sender, EventArgs e)//start
{
button1.Enabled=false;
timer.Enabled = true;
}
private void button2_Click(object sender, EventArgs e) //stop
{
timer.Enabled = false;
if (tcpclnt.Connected) tcpclnt.Close();
this.Close();
}
void timer_Tick(object sender, EventArgs e)
{
try
{
if (!(tcpclnt.Connected)) tcpclnt.Connect("192.168.0.100", 501);
Stream str = tcpclnt.GetStream();
str.Write(BuforOut, 0, BuforOut.Length);
//System.Thread.Sleep(30);
str.Read(BuforIn, 0, BuforIn.Length);
textBox11.Text = Convert.ToString(BuforIn[0])+","+Convert.ToString(BuforIn[1])+","+Convert.ToString(BuforIn[2])+","+Convert.ToString(BuforIn[3])+","+ Convert.ToString(BuforIn[4])+","+Convert.ToString(BuforIn[5]);
str.Close();
}
catch (Exception ee)
{
textBox10.Text = ee.Message;
textBox1.Text = "0";
textBox2.Text = "0";
textBox3.Text = "0";
textBox4.Text = "0";
textBox5.Text = "0";
textBox6.Text = "0";
textBox7.Text = "0";
textBox8.Text = "0";
pictureBox1.BackColor = Color.Red;
pictureBox2.BackColor = Color.Red;
}
}
}
}