之前写的一个基于.NET Framework的局域网通信的软件,有些bug,不过只是为了探究一下socket通信,后续技术熟练以后会对它进行二次迭代,最开始做了一个基于控制台的,后面想了一下,还是需要用可视化界面,所以选择了.NET Framework框架又实现了一遍,也遇到了一些新的问题,最终实现了基本的聊天的发送和接收功能,通信类如下:
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;
using System.Threading;
using System.Net;
namespace Communication_software_windows
{
class Sockt_communication
{
public string user_name = "default";
public string _ip = string.Empty;
private string _ip1 = string.Empty;
private int _port = 0;
private int port1 = 0;
public Socket _socket = null;
public Socket _socket1 = null;
private byte[] buffer = new byte[1024 * 1024 * 2];
private byte[] buffer1 = new byte[1024 * 1024 * 2];
private IPAddress address;
private IPEndPoint endPoint;
public TextBox receive_box;
public TextBox sendmessage_box;
delegate void UpdateUIDelegate(string text);
void UpdateUI(string text)
{
receive_box.Text += text;
}
//无参构造函数
public Sockt_communication()
{
}
public Sockt_communication(string ip, string ip1, int port, int port1, string user_name)
{
this._ip = ip;
this._ip1 = ip1;
this._port = port;
this.port1 = port1;
this.user_name = user_name;