12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using SocketToolbox;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace socketTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- Task.Factory.StartNew(()=> {
- while (true)
- {
- if (SocketLinker.ins != null && SocketLinker.CurrentHandler!=null)
- {
- richTextBox2.Text = Encoding.ASCII.GetString(SocketLinker.CurrentHandler.receiveBuffer);
- }
- Thread.Sleep(2000);
- }
- });
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if(SocketLinker.ins == null)
- {
- SocketLinker.ins = new SocketLinker();
- MessageBox.Show("已连接");
- }
- else
- {
- MessageBox.Show("请勿重复连接");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- IPEndPoint remoteIPE = new IPEndPoint(IPAddress.Parse("192.168.1.102"), 7);
- SocketHandler sh = new SocketHandler(999, remoteIPE);
- sh.StartSend();
- string str = richTextBox1.Text;
- char[] chars = str.ToCharArray();
- byte[] bytes = new byte[chars.Length];
- for(int i = 0; i < chars.Length; i++)
- {
- bytes[i] = Convert.ToByte(chars[i]);
- }
- while (!sh.writeAvailable) { }
- sh.Send(bytes);
- MessageBox.Show("已发送:"+Encoding.ASCII.GetString(bytes));
- }
- }
- }
|