上海大学计算机网络实验报告2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《网络与通信》课程实验报告实验2:Socket通信编程
附件:
系统概述:运行环境、编译、使用方法、实现环境、程序文件列表等;
运行环境:Windows7系统下的
编译:C#
使用方法:为一般窗口程序,双击即可
实现环境:Microsoft Visio Studio
程序文件列表:
主要数据结构;
TCP服务端:
public partial class TcpServerForm : Form
{
private Socket _server;
private Socket _client;
private byte[] _receiveData = new byte[1024];
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)
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
{
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、用户名与密码(密码事先在服务端设置好);