123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 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.111.254"), 8888);
- SocketHandler sh = new SocketHandler(999, remoteIPE);
- sh.StartSend();
- string str = richTextBox1.Text;
- //char[] chars = str.ToCharArray();
- byte[] bytes = Encoding.ASCII.GetBytes(str);
- //for(int i = 0; i < chars.Length; i++)
- //{
- // bytes[i] = Convert.ToByte(chars[i]);
- //}
- while (!sh.writeAvailable) {
- Console.WriteLine("waiting");
- }
- sh.Send(bytes);
- MessageBox.Show("已发送:"+Encoding.ASCII.GetString(bytes));
- }
- }
- }
|