基本信息
源码名称:eqiqiu server
源码大小:0.16M
文件格式:.rar
开发语言:C#
更新时间:2018-04-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace reqiqiu_server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread threadWatch = null; //负责监听客户端的线程
Socket socketWatch = null; //负责监听客户端的套接字
//创建一个负责和客户端通信的套接字
List<Socket> socConnections = new List<Socket>();
List<Thread> dictThread = new List<Thread>();
private void button1_Click(object sender, EventArgs e)
{
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议)
IPAddress ipaddress = IPAddress.Parse(textBox1.Text.Trim()); //服务端发送信息 需要1个IP地址和端口号,获取文本框输入的IP地址
IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(textBox2.Text.Trim())); //将IP地址和端口号绑定到网络节点endpoint上,获取文本框上输入的端口号
socketWatch.Bind(endpoint); //监听绑定的网络节点
socketWatch.Listen(20); //将套接字的监听队列长度限制为20
threadWatch = new Thread(WatchConnecting); //创建一个监听线程
threadWatch.IsBackground = true; //将窗体线程设置为与后台同步
threadWatch.Start(); //启动线程
textBox4.AppendText("开始监听客户端传来的信息!" "\r\n"); //启动线程后 txtMsg文本框显示相应提示
}
private void WatchConnecting()
{
while (true) //持续不断监听客户端发来的请求
{
Socket socConnection = socketWatch.Accept();
textBox4.AppendText("客户端连接成功" "\r\n");
//创建一个通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
Thread thr = new Thread(pts);
thr.IsBackground = true;
socConnections.Add(socConnection); //启动线程
thr.Start(socConnection);
dictThread.Add(thr);
}
}
private void ServerRecMsg(object obj)
{
throw new NotImplementedException();
}
private void button2_Click(object sender, EventArgs e)
{
//调用 ServerSendMsg方法 发送信息到客户端
ServerSendMsg(textBox3.Text.Trim());
}
/// <summary>
/// 发送信息到客户端的方法
/// </summary>
/// <param name="sendMsg">发送的字符串信息</param>
private void ServerSendMsg(string sendMsg)
{
//将输入的字符串转换成 机器可以识别的字节数组
byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg);
//向客户端发送字节数组信息
foreach (Socket socConnection in socConnections)
{
socConnection.Send(arrSendMsg);
}
//将发送的字符串信息附加到文本框txtMsg上
textBox4.AppendText("So-flash:" GetCurrentTime() "\r\n" sendMsg "\r\n");
//}
}
private string GetCurrentTime()
{
throw new NotImplementedException();
}
}
}