博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Socket通信
阅读量:6341 次
发布时间:2019-06-22

本文共 3660 字,大约阅读时间需要 12 分钟。

服务端:

namespace WindowsFormsApplication2

{
public partial class Form1 : Form
{

List<CocketSend> CocketSendArr = new List<CocketSend>();

public Form1()

{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}

private void button1_Click(object sender, EventArgs e)

{
//当点击的时候穿件一个负责IP地址监听的socket
Socket socketWatch = new Socket(SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any;
//创建端口号
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.textBox1.Text));
socketWatch.Bind(point);
Show("监听成功");

socketWatch.Listen(10);

 

Thread rh = new Thread(new ParameterizedThreadStart(ListenClientConnect));

rh.Start(socketWatch);

}

/// <summary>

/// 等待客户端链接并且创建与之通信的socket
/// </summary>
private void ListenClientConnect(object o)
{
//负责和客户端通信的socket
Socket socketWatch = o as Socket;
while (true)
{

//等待客户端链接 ,建立负责通信的socket

Socket socektSend = socketWatch.Accept();

//将通信的socket保存到集合中
CocketSend csModel = new CocketSend();
csModel.RemoteEndPoint = socektSend.RemoteEndPoint.ToString();
csModel.socektSend = socektSend;
CocketSendArr.Add(csModel);
ComboBoxBind();

Show(socektSend.RemoteEndPoint.ToString() + " 链接成功");

Thread th = new Thread(new ParameterizedThreadStart(Recive));

th.Start(socektSend);
}

}

//服务器端不停的接收来的消息
void Recive(object o)
{
Socket socektSend = o as Socket;
while (true)
{
try
{
//客户端链接成功后。服务器接收来自客户端的消息
byte[] buffer = new byte[1024 * 1024 * 2];
int lR = socektSend.Receive(buffer);
if (lR == 0) //判断客户端下线
{
foreach (var item in CocketSendArr)
{
if (item.RemoteEndPoint == socektSend.RemoteEndPoint.ToString())
{
CocketSendArr.Remove(item);
}
}
ComboBoxBind();
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, lR);
Show(string.Format("{0}:{1}\r\n", socektSend.RemoteEndPoint.ToString(), str));
}
catch
{ }
}
}
public void Show(string str)
{
textBox2.AppendText(string.Format("{0}\r\n", str)); ;
}

private void button2_Click(object sender, EventArgs e)

{

string strCode = this.comboBox1.SelectedItem.ToString();

Socket sModel = null;

foreach (var item in CocketSendArr)
{
if (item.RemoteEndPoint == strCode)
{
sModel = item.socektSend;
break;
}
}
if (sModel != null)
{
string str = textBox3.Text;
byte[] buffer = Encoding.UTF8.GetBytes(str);
sModel.Send(buffer);
}
}

public void ComboBoxBind()

{
this.comboBox1.Items.Clear();
foreach (var item in CocketSendArr)
{
this.comboBox1.Items.Add(item.RemoteEndPoint);
}
}
public class CocketSend
{
public string RemoteEndPoint { get; set; }
public Socket socektSend { get; set; }
}
}
}

 

客户端:

namespace WindowsFormsApplication_c

{
public partial class Form1 : Form
{
Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}

private void button1_Click(object sender, EventArgs e)

{

string ipStr = textBox1.Text;

int pointI = Convert.ToInt32(textBox2.Text);

//创建客户端的Socket

IPAddress ip = IPAddress.Parse(ipStr);
IPEndPoint point = new IPEndPoint(ip, pointI);
//与远程服务器建立链接
socketSend.Connect(point);
textBox3.AppendText("建立链接\r\n");

Thread th = new Thread(new ThreadStart(Rccive));

th.Start();

}

private void button2_Click(object sender, EventArgs e)

{
string str = textBox3.Text;
byte[] buffer = Encoding.UTF8.GetBytes(str);
socketSend.Send(buffer);
textBox3.Text = string.Empty;
}

/// <summary>

/// 不停接收数据
/// </summary>
void Rccive()
{

while (true)

{
try
{
byte[] buffer = new byte[1024 * 1024 * 3];
int l = socketSend.Receive(buffer);
if (l == 0)
{ break; }

string str = Encoding.UTF8.GetString(buffer);

textBox4.AppendText(str);
textBox4.AppendText("\r\n");
}
catch { }
}
}
}
}

转载于:https://www.cnblogs.com/KangWeiLu/p/7883327.html

你可能感兴趣的文章
Word 2003中编辑标记与格式标记大讨论
查看>>
调试网页PAIP HTML的调试与分析工具
查看>>
路径工程OpenCV依赖文件路径自动添加方法
查看>>
玩转SSRS第七篇---报表订阅
查看>>
WinCE API
查看>>
Linux常用基本命令[cp]
查看>>
CSS 相对|绝对(relative/absolute)定位系列(一)
查看>>
关于 Nginx 配置 WebSocket 400 问题
查看>>
Glide和Govendor安装和使用
查看>>
Java全角、半角字符的关系以及转换
查看>>
前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
查看>>
UOJ#179. 线性规划(线性规划)
查看>>
整合spring cloud云架构 - SSO单点登录之OAuth2.0登录认证(1)
查看>>
windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
redis 安装
查看>>
SQL some any all
查看>>
电子书下载:Programming Windows Identity Foundation
查看>>
有理想的程序员必须知道的15件事
查看>>
用于测试的字符串
查看>>