(服务端简易界面)
/******************************************服务端**********************************/ using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ;
using .Sockets;
using System.Threading;
namespace服务器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Socket mysocket;//Socket对象
bool Stop = false;//标志位
///打开服务器监听客户端连接请求
private void OpenServer_Click(object sender, EventArgs e)
{
try
{
IPAddress ipa = IPAddress.Parse("192.168.1.36");//服务端IP地址
TcpListener mylistrn = new TcpListener(ipa, 6001);//实例化监听助手类对象
mylistrn.Start();//开始监听
listBox1.Items.Add("服务器启动!");//提示信息
while (true)
{
if (Stop == true) //标志位
{
timer1.Stop();//关闭计时器
break;
}
Application.DoEvents();//防止死循环时程序未响应
if (mylistrn.Pending())//判断是否有客户端连接请求
{
mysocket = mylistrn.AcceptSocket();//接受客户端请求
mysocket.Send(Encoding.Unicode.GetBytes("服务器连接成功!!"));//向客户端发送消息,收到表示连接成功
timer1.Start();//启动定时器,循环监听客户端发送的消息,此处也可用线程,不过timer也是多线程的而且可以跨线程共享资源
}
}
Application.Exit();//释放资源,关闭程序
}
catch (Exception ex)
{
MessageBox.Show("Listen Error" + ex.Message);//异常提示
}
}
///为客户端开辟独立线程监听消息100毫秒执行一次
private void timer1_Tick(object sender, EventArgs e)
{
byte[] data = new byte[1024];
if (mysocket.Available > 0)//判断是否有数据可供读取
{
mysocket.Receive(data);//读取数据
string content = Encoding.Unicode.GetString(data);//把字节流转换为字符串
listBox1.Items.Add(content);//显示字符串
}
}
///用于给标志位Stop赋值
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Stop = true;
}
}
}
(客户端简易界面)
/*************************************客户端*******************************************/ using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ;
using .Sockets;
namespace客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/*********************************************客户端
************************************************/
TcpClient tcline = new TcpClient();//客户端助手类
NetworkStream ns;//数据流对象
bool Stop = false;//标志位,用于释放资源
/// send_Click事件用于客户端向服务端发送数据
private void send_Click(object sender, EventArgs e)
{
ns = tcline.GetStream();//实例化NetworkStream
string count = textBox1.Text.ToString();//要发送的字符串
byte[] data = Encoding.Unicode.GetBytes(count);//把字符串转化为字节数组,因为网络数据传输的是字节
ns.Write(data, 0, data.Length);//将字节流写入数据流(即发送数据)
}
/// conn_Click事件用于连接服务端并监听消息接收。
private void conn_Click(object sender, EventArgs e)
{
try
{
tcline.Connect(textBox2.Text.Trim(), 6001);//连接客户端
ns = tcline.GetStream();//实例化NetworkStream
byte[] data1 = Encoding.Unicode.GetBytes("客户端上线!!");//把字符串转化为字节数组
ns.Write(data1, 0, data1.Length);//向服务端发送数据,收到表示连接成功
while (true)//循环监听服务端发送的数据
{
if (Stop == true) //标志位跳出死循环
{
break;
}
Application.DoEvents();//防止死循环时程序未响应
if (tcline.Available > 0)//判断是否有数据可供读取
{
byte[] data = new byte[1024];
ns.Read(data, 0, 1024);//读取数据
string content = Encoding.Unicode.GetString(data);//把字节流转换为字符串 listBox1.Items.Add(content);//显示字符串
}
}
Application.Exit();//释放资源,关闭程序
}
catch (Exception es)
{
MessageBox.Show("服务未开启!" + es.Message);//异常提示
}
}
///用于给标志位Stop赋值
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Stop = true;
}
}
}。