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.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Windows;
namespace NetworkCardReader
{
public partial class Form1 : Form
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
const int BufferSize = 1024;
byte[] buffer=new byte[BufferSize];
public Form1()
{
InitializeComponent();
radioButtonTCPServer.Checked = true;
radioButtonTCPClient.Checked = false;
radioButtonUDPServer.Checked = false;
radioButtonUDPClient.Checked = false;
textBoxLocalPort.ReadOnly = false; //本地端口
textBoxRemotePort.ReadOnly = true; //远程端口
textBoxRemoteIP.ReadOnly = true; //远程IP
buttonConnectNet.Text = "开始监听";
}
private void buttonConnectNet_Click(object sender, EventArgs e)
{
if (radioButtonTCPClient.Checked == true) //TCP 客户端
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (buttonConnectNet.Text == "连接网络")
{
//连接到的目标IP
IPAddress ip = IPAddress.Parse(textBoxRemoteIP.Text);
IPEndPoint point = new IPEndPoint(ip, int.Parse(textBoxRemotePort.Text));
try
{
//连接到服务器
client.Connect(point);
//连接成功后就可以接收服务器发送的消息了
Thread th = new Thread(ReceiveMsg);
th.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "TCP连接错误");
return;
}
buttonConnectNet.Text = "断开连接";
tspIP.Text = "本地端口:" + client.LocalEndPoint + " ";
}
else if (buttonConnectNet.Text == "断开连接")
{
buttonConnectNet.Text = "连接网络";
tspIP.Text = "无连接!!! ";
//client.Dispose();
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
else if (radioButtonTCPServer.Checked == true) //TCP 服务器
{
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
string localip = ipEntry.AddressList[2].ToString();
IPEndPoint point = new IPEndPoint(IPAddress.Parse(localip), Int32.Parse(textBoxLocalPort.Text));
//使用IPv4地址,流式Socket方式,tcp协议传递数据
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (buttonConnectNet.Text == "开始监听")
{
//创建好socket后,必须告诉socket绑定的IP地址和端口号
//让socket监听point
try
{
//socket监听哪个端口
socket.Bind(point);
//同一时间点过来10个客户端 ,排队
socket.Listen(10);
Thread thread = new Thread(AcceptInfo);
thread.Start(socket);
buttonConnectNet.Text = "停止监听";
tspIP.Text = "开始监听:" + point;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if (buttonConnectNet.Text == "停止监听")
{
buttonConnectNet.Text = "开始监听";
tspIP.Text = "无连接!!! ";
socket.Close();
// socket.Disconnect();
}
}
}
public void ReceiveMsg() //TCP客户端 接收数据
{
while (true)
{
try
{
byte[] buffer = new byte[1024 * 1024];
int n = client.Receive(buffer);
string s = Encoding.UTF8.GetString(buffer, 0, n);
richTextBoxRecData.Text += s;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
break;
}
if (buttonConnectNet.Text == "连接网络")
{
break;
}
}
}
public void ReceiveMsgFromClient(object o)//TCP服务器 接收数据
{
Socket client = o as Socket;
while (true)
{
//接收客户端发来的数据
try
{
byte[] buffer = new byte[1024 * 1024];
int n = client.Receive(buffer);
string s = Encoding.UTF8.GetString(buffer, 0, n);
richTextBoxRecData.Text += s;
tspIP.Text = "本地端口:" + client.LocalEndPoint + " ";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
break;
}
if (buttonConnectNet.Text == "开始监听")
{
// break;
}
}
}
//发送数据
private void buttonSend_Click(object sender, EventArgs e)
{
if (radioButtonTCPClient.Checked == true)//TCP客户端
{
if (buttonConnectNet.Text == "断开连接")
{
if (client != null)
{
try
{
byte[] buffer = Encoding.UTF8.GetBytes(richTextBoxSendData.Text);
client.Send(buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
MessageBox.Show("请先连接网络!");
}
else if (radioButtonTCPServer.Checked == true)//TCP服务器
{
if (buttonConnectNet.Text == "停止监听")
{
try
{
string ip = Serverpoint;
byte[] buffer = Encoding.UTF8.GetBytes(richTextBoxSendData.Text);
dic[ip].Send(buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
MessageBox.Show("请先连接网络!");
}
}
//记录通信用的socket
Dictionary<string, Socket> dic = new Dictionary<string, Socket>();
string Serverpoint;
void AcceptInfo(object o)
{
Socket socket = o as Socket;
while (true)
{
try
{
//创建通信用的Socket
Socket tSocket = socket.Accept();
string point = tSocket.RemoteEndPoint.ToString();
tspIP.Text = "远程端口:" + point + " ";
Serverpoint=point;
dic.Add(point, tSocket);
//接收消息
Thread th = new Thread(ReceiveMsgFromClient);
th.Start(tSocket);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void radioButtonTCPServer_Click(object sender, EventArgs e)
{
radioButtonTCPServer.Checked = true;
radioButtonTCPClient.Checked = false;
radioButtonUDPServer.Checked = false;
radioButtonUDPClient.Checked = false;
textBoxLocalPort.ReadOnly = false; //本地端口
textBoxRemotePort.ReadOnly = true; //远程端口
textBoxRemoteIP.ReadOnly = true; //远程IP
buttonConnectNet.Text = "开始监听";
}
private void radioButtonTCPClient_Click(object sender, EventArgs e)
{
radioButtonTCPServer.Checked = false;
radioButtonTCPClient.Checked = true;
radioButtonUDPServer.Checked = false;
radioButtonUDPClient.Checked = false;
textBoxLocalPort.ReadOnly = true; //本地端口
textBoxRemotePort.ReadOnly = false; //远程端口
textBoxRemoteIP.ReadOnly = false; //远程IP
buttonConnectNet.Text = "连接网络";
}
private void radioButtonUDPServer_Click(object sender, EventArgs e)
{
radioButtonTCPServer.Checked = false;
radioButtonTCPClient.Checked = false;
radioButtonUDPServer.Checked = true;
radioButtonUDPClient.Checked = false;
textBoxLocalPort.ReadOnly = false;
textBoxRemotePort.ReadOnly = true;
buttonConnectNet.Text = "开始监听";
}
private void radioButtonUDPClient_Click(object sender, EventArgs e)
{
radioButtonTCPServer.Checked = false;
radioButtonTCPClient.Checked = false;
radioButtonUDPServer.Checked = false;
radioButtonUDPClient.Checked = true;
textBoxLocalPort.ReadOnly = true;
textBoxRemotePort.ReadOnly = false;
buttonConnectNet.Text = "连接网络";
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void buttonCleanRevbuffer_Click(object sender, EventArgs e)
{
richTextBoxRecData.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
richTextBoxSendData.Text = "";
}
}
}
C# socket通讯
最新推荐文章于 2020-07-17 17:54:02 发布