1.先展示效果:
TCPIP效果展示
2.代码功能实现如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TCPIP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient tcpClient;
NetworkStream networkStream;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient(textBox1.Text, Convert.ToInt16(textBox2.Text));
networkStream = tcpClient.GetStream();
}
private void button2_Click(object sender, EventArgs e)
{
byte[] data = Encoding.UTF8.GetBytes(textBox3.Text);
networkStream.Write(data, 0, data.Length);
}
byte[] bytes = new byte[1024];
private void timer1_Tick(object sender, EventArgs e)
{
if (bytes.Length == 1024) return;
}
private void button3_Click(object sender, EventArgs e)
{
int abs = networkStream.Read(bytes, 0, bytes.Length);
string response = Encoding.ASCII.GetString(bytes, 0, abs);
listBox1.Items.Add(response);
}
}
}
3.感谢浏览