using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
namespace WindowsFormsApp14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient myClient;
NetworkStream ns;
bool connected = false;
//gönderilecek karakterler
char[] led1 = { 'a' };
char[] sayacl = { 'b' };
char[] sıfır= { 'c' };
byte[] readData = new byte[256];
string[] stringSeparators = new string[] { "|" };
private void Form1_Load(object sender, EventArgs e)
{
//timer lar 200ms' de tetikleniyor
timer1.Interval = 200;
timer2.Interval = 200;
{
}
}
private void button1_Click(object sender, EventArgs e)
{
//bağlı ise bağlantıyı kes
if (!connected)
{
try
{
myClient = new TcpClient("169.254.145.11", 61); //server IP ve port
label1.Text = "Bağlandı";
connected = true;
ns = myClient.GetStream();
button1.Text = "DURDUR";
}
catch (Exception ex)
{
MessageBox.Show("Bağlantı Kurulamadı!\n\r" + ex.Message, "Baglanti Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
connected = false;
button1.Text = "Bağlan";
}
}
else
{
try
{
myClient.Close(); //server ile bağlantıyı kes
button1.Text = "Bağlan";
label1.Text = "Bağlantı Yok";
connected = false;
}
catch (Exception ex)
{
MessageBox.Show("Bağlantı Kurulamadı!\n\r" + ex.Message, "Baglanti Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
connected = false;
button1.Text = "Bağlan";
label1.Text = "Bağlantı Yok";
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{ ns.Write(Encoding.GetEncoding("UTF-8").GetBytes(sayacl), 0, sayacl.Length);
int recv = ns.Read(readData, 0, readData.Length); //gelen veriyi aldık
String s = Encoding.ASCII.GetString(readData, 0, recv);
String[] ss = s.Split(stringSeparators, StringSplitOptions.None); //parseleyip lable' e yazdırdık
label3.Text = ss[1];
}
private void button2_Click(object sender, EventArgs e)
{
if (connected)
{
ns.Write(Encoding.GetEncoding("UTF-8").GetBytes(led1), 0, led1.Length); //led1 karakterini gönderdik
int recv = ns.Read(readData, 0, readData.Length); //gelen veriyi aldık
String s = Encoding.ASCII.GetString(readData, 0, recv);
String[] ss = s.Split(stringSeparators, StringSplitOptions.None); //parseleyip lable' e yazdırdık
label2.Text = ss[1];
}
}
private void button4_Click(object sender, EventArgs e)
{
ns.Write(Encoding.GetEncoding("UTF-8").GetBytes(sıfır), 0, sıfır.Length);
}
private void timer2_Tick(object sender, EventArgs e)
{
ns.Write(Encoding.GetEncoding("UTF-8").GetBytes(sayacl), 0, sayacl.Length);
int recv = ns.Read(readData, 0, readData.Length);
String s = Encoding.ASCII.GetString(readData, 0, recv);
String[] ss = s.Split(stringSeparators, StringSplitOptions.None);
label3.Text = ss[1];
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (connected)
{
timer2.Start();
}
else
{
checkBox1.Checked = false;
}
}
else
{
timer2.Stop();
}
}
}
}