服务器ui
服务器代码:
/// <summary>
/// 连接上的客户端
/// </summary>
Dictionary<string, Socket> dic = new Dictionary<string, Socket>();
/// <summary>
/// 服务端
/// </summary>
Socket server = null;
public Home()
{
InitializeComponent();
Initialize();
}
/// <summary>
/// 服务器初始化
/// </summary>
public void Initialize()
{
text_ServerIp.Text = GetLocalIp();
text_ServerIp.ReadOnly = true;
text_ServerPort.Text = "8000";
text_ServerPort.ReadOnly = true;
}
/// <summary>
/// 获取本地ip地址
/// </summary>
/// <returns></returns>
public string GetLocalIp()
{
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
return AddressIP;
}
/// <summary>
/// 启动服务器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void but_Start_Click(object sender, EventArgs e)
{
//设置属性
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//配置ip和端口
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(text_ServerIp.Text), int.Parse(text_ServerPort.Text));
//绑定服务
try
{
server.Bind(iPEndPoint);
this.Text = "服务器连接成功";
but_Start.Enabled = false;