110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace TcpReceive
{
/// <summary>
/// 服务器端
/// </summary>
public partial class FrmReceive : Form
{
TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, 2222)); //2222端口
public FrmReceive()
{
InitializeComponent();
listener.Start(); //开始侦听端口
Thread acceptThread = new Thread(new ThreadStart(AcceptWorkThread));
acceptThread.Start(); //接受客户端请求
}
/// <summary>
/// 接收、解码
/// </summary>
private void AcceptWorkThread()
{
Socket socket = listener.AcceptSocket();
byte[] buffer = new byte[1024];
while (true)
{
int receiveCount = socket.Receive(buffer);
if (receiveCount > 0)
{
string recString = Encoding.UTF8.GetString(buffer, 0, receiveCount); //解码
ShowMsg(recString);
}
else
{
socket.Close();
break;
}
}
}
/// <summary>
/// 显示接收到的消息内容
/// </summary>
/// <param name="text"></param>
public void ShowMsg(string text)
{
rBoxRecevie.Text = text;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace TcpSend
{
/// <summary>
/// 客户端
/// </summary>
public partial class FrmSend : Form
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public FrmSend()
{
InitializeComponent();
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
if (socket.Connected)
BeginSend();
else
MessageBox.Show("请先连接服务器!", "提示");
}
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConn_Click(object sender, EventArgs e)
{
IPAddress serverIp = IPAddress.Parse("192.168.0.3");
IPEndPoint iep = new IPEndPoint(serverIp, 2222);
socket.Connect(iep); //连接服务器
}
private void BeginSend()
{
byte[] byteMessage;
byteMessage = Encoding.UTF8.GetBytes(rBoxContent.Text);
socket.Send(byteMessage);
}
/// <summary>
/// 窗口关闭时释放socket
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmSend_FormClosing(object sender, FormClosingEventArgs e)
{
if (socket.Connected)
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
}
int x;
int y;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
x=e.X;
y=e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();//Graphics类封装一个 GDI+ 绘图图面。无法继承此类。
SolidBrush sb = new SolidBrush(Color.Red);//Windows 显示元素的颜色
Pen p = new Pen(sb, 1);//Pen类定义用于绘制直线和曲线的对象。无法继承此类。
g.DrawLine(p, this.x, this.y, e.X, e.Y);//DrawLine()绘制一条连接由坐标对指定的两个点的线条。
}
static UdpClient udpClient;
static IPEndPoint tempEnd = new IPEndPoint(IPAddress.Any, 0);
// Udp单网卡不需要手动设置本地节点,多网卡的时候建议设置本机节点,
// 因为UdpClient不指定IP的构造函数默认绑定本地IP数组的第一个IP
static IPEndPoint localhostIPEndPoint;
static void Main(string[] args)
{
localhostIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
udpClient = new UdpClient(localhostIPEndPoint);
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
byte[] datagram = Encoding.UTF8.GetBytes("这是一个测试!!!");
// 异步发送
udpClient.BeginSend(datagram, datagram.Length, localhostIPEndPoint, new AsyncCallback(SendCallback), udpClient);
// 发送停止监听
udpClient.BeginSend(new byte[] { 0xff }, 1, localhostIPEndPoint, null, null);
}
// 异步接收回调方法
static void ReceiveCallback(IAsyncResult ar)
{
object o = new object();
try
{
lock (o)
{
byte[] buffer = udpClient.EndReceive(ar, ref tempEnd);
// 我发现udpclient连接在监听的时候强制Close会抛异常,如果用线程也会弹出一个对话框,正在终止线程
// 主要原因是因为udpClient连接正在监听,阻塞了,所以强制结束会有异常,所以最好的办法的就是让监听
// 结束,我的解决办法是给自己发送一个消息,判断字节,如果符合要求并且节点是本机就关闭udp连接,我试
// 过了,这样结束udpClient不会出现任何的异常
if (tempEnd.Equals(localhostIPEndPoint) && buffer[0] == 0xff)
{
udpClient.Close();
Console.WriteLine("udp连接成功关闭...");
return;
}
else
{
Console.WriteLine(Encoding.UTF8.GetString(buffer));
}
}
lock (o)
{
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
}
catch
{
lock (o)
{
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
}
}
// 异步发送回调方法
static void SendCallback(IAsyncResult ar)
{
Console.WriteLine("发送的字节数:{0}", ((UdpClient)ar.AsyncState).EndSend(ar));
}
class Program
{
static UdpClient udpClient;
static IPEndPoint tempEnd = new IPEndPoint(IPAddress.Any, 0);
// Udp单网卡不需要手动设置本地节点,多网卡的时候建议设置本机节点,
// 因为UdpClient不指定IP的构造函数默认绑定本地IP数组的第一个IP
static IPEndPoint localhostIPEndPoint;
static void Main(string[] args)
{
localhostIPEndPoint = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 9000);
udpClient = new UdpClient(localhostIPEndPoint);
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
byte[] datagram = Encoding.UTF8.GetBytes("这是一个测试!!!");
// 异步发送
udpClient.BeginSend(datagram, datagram.Length, localhostIPEndPoint, new AsyncCallback(SendCallback), udpClient);
// 发送停止监听
udpClient.BeginSend(new byte[] { 0xff }, 1, null, null);
}
// 异步接收回调方法
static void ReceiveCallback(IAsyncResult ar)
{
object o = new object();
try
{
lock (o)
{
byte[] buffer = udpClient.EndReceive(ar, ref tempEnd);
// 我发现udpclient连接在监听的时候强制Close会抛异常,如果用线程也会弹出一个对话框,正在终止线程
// 主要原因是因为udpClient连接正在监听,阻塞了,所以强制结束会有异常,所以最好的办法的就是让监听
// 结束,我的解决办法是给自己发送一个消息,判断字节,如果符合要求并且节点是本机就关闭udp连接,我试
// 过了,这样结束udpClient不会出现任何的异常
if (tempEnd.Equals(localhostIPEndPoint) && buffer[0] == 0xff)
{
udpClient.Close();
return;
}
// 处理buffer
}
lock (o)
{
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
}
catch
{
lock (o)
{
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
}
}
// 异步发送回调方法
static void SendCallback(IAsyncResult ar)
{
Console.WriteLine("发送的字节数:{0}", ((UdpClient)ar.AsyncState).EndSend(ar));
}
}