页眉 private string _username; private string _password; private bool _needPasswordl; private bool _passwordWrong; public TcpServerForm()
private void TcpReceiveForm_Load(object sender, EventArgs e) private void btnListen_Click(object sender, EventArgs e) private void btnDisconnect_Click(object sender, EventArgs e) void AcceptedConnection(IAsyncResult iar) void ReceivedData(IAsyncResult iar) void SentData(IAsyncResult iar) void ProcessDisconnection()
private void txtUserName_TextChanged(object sender, EventArgs e) private void txtPassword_TextChanged(object sender, EventArgs e) private void lblUserName_Click(object sender, EventArgs e) private void gnConnectInfo_Enter(object sender, EventArgs e)
private void lbMessage_SelectedIndexChanged(object sender, EventArgs e) private void lbNativeIP_SelectedIndexChanged(object sender, EventArgs e) private void lbConnectLog_SelectedIndexChanged(object sender, EventArgs e) }
TCP客户端:
public partial class TcpClientForm : Form {
Socket _client;
byte[] _receivedData = new byte[1024]; public TcpClientForm()
private void btnConnect_Click(object sender, EventArgs e)
5 / 22
页眉 private void btnDisconnect_Click(object sender, EventArgs e) private void btnSend_Click(object sender, EventArgs e) void Connected(IAsyncResult iar) void ReceivedData(IAsyncResult iar) void SentData(IAsyncResult iar) void ProcessDisconnection()
private void txtIP_TextChanged(object sender, EventArgs e) }
UDP接收端:
public partial class UdpReceiveForm : Form {
private bool ReadFlag = true; private Thread th;
private IPEndPoint remote; private UdpClient server; private int count = 0; private double num; public UdpReceiveForm() private void read()
private void btnReceive_Click(object sender, EventArgs e)
private void UdpReceiveForm_FormClosing(object sender, FormClosingEventArgs e) }
UDP发送端:
public partial class UdpSendForm : Form {
6 / 22
页眉 public UdpSendForm()
private void btnSend_Click(object sender, EventArgs e) private void txtIP_TextChanged(object sender, EventArgs e) private void lblInfo_Click(object sender, EventArgs e) }
主要算法描述;
异步调用与回调(部分): TCP服务端:
_server.BeginAccept(new AsyncCallback(AcceptedConnection), _server);//异步调用,开始接收连接 -> void AcceptedConnection(IAsyncResult iar) _client.BeginSend(sendMessage,
0,
sendMessage.Length,
SocketFlags.None,
new
AsyncCallback(SentData), _client);//异步调用,消息发送完毕执行->void SentData(IAsyncResult iar)
_client.BeginReceive(_receiveData, 0, _receiveData.Length, SocketFlags.None, new AsyncCallback(ReceivedData), _client);//密码正确,开始接收消息->void ReceivedData(IAsyncResult iar)
用户使用手册;
TCP客户端、TCP服务端:
1、用户打开TCP客户端并且输入服务端IP、用户名与密码(密码事先在服务端设置好);
7 / 22
页眉 2、服务端点击“开始监听”,客户端点击“连接”;
3、客户端在消息框内输入消息,点击“发送”,则消息发送到服务端,; 4、服务端接收并且在“接收到的消息”框里显示消息与发送消息时的日期; 注:客户端与服务端均能断开连接。 一、
UDP发送端、UDP接收端:
1、 用户打开UDP发送端并且输入目标IP、需发送的消息; 2、 点击“发送按钮”;
3、 接收端点击“接收”则接收到消息。
程序源代码;
Tcp服务端:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; using System.Net;
using System.Net.Sockets; namespace TCP服务端 {
public partial class TcpServerForm : Form {
private Socket _server; private Socket _client;
8 / 22
相关推荐: