C# TCP通信(阉割版)_.net

#客户端代码:

using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsClient
{
public partial class Form1 : Form
{
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
Socket socketClient = null;//创建socket套接字
void ShowMessage(string str)//打印消息
{
show_Text.AppendText(str + "\r\n");
}
private void conn_button_Click(object sender, EventArgs e)
{
//1.创建套接字
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//ipv4、TCP
//2.获取IP及连接ip&端口号
ip_text.Text = "127.0.0.1";
port_text.Text = "8080";
IPAddress ip = IPAddress.Parse(ip_text.Text.Trim());
IPEndPoint ip_port = new IPEndPoint(ip, int.Parse(port_text.Text.Trim()));
try
{
ShowMessage("与服务器连接中...");
socketClient.Connect(ip_port);
}
catch
{
MessageBox.Show("连接失败");
return;
}
ShowMessage("与服务器连接成功!");
}
private void send_button_Click(object sender, EventArgs e)
{
string strMsg = " :" + send_Text.Text.Trim() + "\r\n";
byte[] buff = System.Text.Encoding.UTF8.GetBytes(strMsg);
socketClient.Send(buff); // 发送消息;
ShowMessage("向服务器发送"+strMsg);
send_Text.Clear();
}
private void Form1_Load(object sender, EventArgs e)
{ }
}
}

#服务端

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net; // IP,IPAddress, IPEndPoint,端口等;
using System.Threading;
namespace WindowsServer
{
public partial class From1 : Form
{
public From1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
public static bool recvStatus = false;//确认接收
Thread threadServer = null;//监听客户端连接请求的线程
Socket socketServer = null;//创建socket套接字
Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>();
private void Listenbutton_Click(object sender, EventArgs e) //(激活)监听事件
{
//1.创建套接字
socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//ipv4、TCP
//2.获取IP及连接ip&端口号
ip_text.Text = "127.0.0.1";
port_text.Text = "8080";
IPAddress ip = IPAddress.Parse(ip_text.Text.Trim());
IPEndPoint ip_port = new IPEndPoint(ip, int.Parse(port_text.Text.Trim()));
try
{
//3.绑定套接字与ip&port
socketServer.Bind(ip_port);
}
catch
{
MessageBox.Show("绑定套接字失败");
return;
}
//4.设置监听的数量
socketServer.Listen(5);
//5.****创建监听线程****
threadServer = new Thread(WatchConnecting)
{
IsBackground = true//??
};
threadServer.Start();
ShowMessage("监听服务启动成功!");
}
public void WatchConnecting()//监听线程
{
while (true)//持续监听
{
//5.1监听客户端连接请求,返回??客户端套接字
Socket sock_conn_temp = socketServer.Accept();
//5.2将客户端信息添加到dict集合中
dict.Add(sock_conn_temp.RemoteEndPoint.ToString(), sock_conn_temp);
//打印客户端信息
ShowMessage("与客户端:" + sock_conn_temp.RemoteEndPoint.ToString() + "连接成功!!!");
//5.3将当前监听线程添加的dictThread集合中
Thread thr_temp = new Thread(RecvMessage)
{
IsBackground = true
};
thr_temp.Start(sock_conn_temp);
dictThread.Add(sock_conn_temp.RemoteEndPoint.ToString(), thr_temp);
}
}
public void RecvMessage(object sokConnectionparn)//接收客户端信息;
{
Socket sock_client_temp = sokConnectionparn as Socket;//??
if (!recvStatus)
{
ShowMessage("请先确认接收消息...");
}
while (true)
{
//5.3.1存储客户端发送的数据
byte[] buff = new byte[1024 * 1024 * 2];//2M
int length = -1;
try
{
length = sock_client_temp.Receive(buff);
}
catch (SocketException se)
{
ShowMessage("发生套接字错误异常");
//5.3.2从dict集合中删除当前套接字
dict.Remove(sock_client_temp.RemoteEndPoint.ToString());
//5.3.3从dictThread集合中删除当前线程
dictThread.Remove(sock_client_temp.RemoteEndPoint.ToString());
ShowMessage("与客户端:" + sock_client_temp.RemoteEndPoint.ToString() + "中断连接!!!");
recvStatus = false;
break;
}
catch (Exception e)
{
ShowMessage("程序执行异常");
//5.3.2从dict集合中删除当前套接字
dict.Remove(sock_client_temp.RemoteEndPoint.ToString());
//5.3.3从dictThread集合中删除当前线程
dictThread.Remove(sock_client_temp.RemoteEndPoint.ToString());
ShowMessage("与客户端:" + sock_client_temp.RemoteEndPoint.ToString() + "中断连接!!!");
recvStatus = false;
break;
}
if (recvStatus)
{
string strMsg = "["+sock_client_temp.RemoteEndPoint.ToString() +"]"+ System.Text.Encoding.UTF8.GetString(buff, 1, length - 1);// 将接受到的字节数据转化成字符串;
ShowMessage(strMsg);
}
}
}
void ShowMessage(string str)//打印消息
{
show_Text.AppendText(str + "\r\n");
}
private void Recv_button_Click(object sender, EventArgs e)
{
ShowMessage("已确认");
recvStatus =true;
}
private void canc_button_Click(object sender, EventArgs e)
{
ShowMessage("已取消");
recvStatus = false;
}
private void From1_Load(object sender, EventArgs e)
{}
}
}

C# TCP通信(阉割版)_端口号_02