logo资料库

基于Winsock的多客户并发程序的实现(TCP实验报告).doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
华 北 电 力 大 学 实 验 报 告 | | 实验名称 基于Winsock的多客户并发服务程序的实现 课程名称 TCP-IP协议原理 专业班级: 学 号: 指导教师: 学生姓名: 成 绩: 实验日期:2012.5.21
基于Winsock的多客户并发服务程序实现 * 实验要求 * 基于Winsock分别编写客户端和服务器端程序,服务器端进行监听,接到客户的请求后 服务器与客户建立关联,采用流套接字(TCP)或数据报套接字(UDP)均可。服务 器端接受客户传送来的数据并将其发回给客户端。 * 服务器端实现接受多个客户的服务请求并为它们服务,接受客户传送来的数据并将数 据分别发回给客户端,服务器端和客户端的数据通信采用的是异步方式。 * 实验目的 熟悉socket的操作,掌握异步socket的使用方法,理解多线程的概念,初步掌握线程的编程 方法。 * 实验流程图 客户端程序为处理按钮事件. 服务器程序流程图: 启动 结束 侦听端口 等待客户端 线程调度 显示客户端 ip 发送消息到客户端 接收客户端消息 * 实验结果: 一个客户端发消息,服务器转发到所有客户端,结果截屏如下: 服务器和客户端初始化:
建立连接后服务器端: 客户端发送接收消息: 由于服务器端监听的是本机 ip 和 端口,故使用了环回地址 127.0.0.1 发送的消息 接收的消息 (使用多线程技术实现了多客户端,图略) * 实验心得: 本实验程序基于TCP的连接,使用C#语言编写,使我熟悉了SOCKET的操作,掌握了异 步SOCKET的使用方法,理解了线程的概念,初步掌握了线程的编程方法。 * 实验程序附录: 客户端程序: using System; using … using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; 主要函数: ProtocolType.Tcp);//创建Socket //点击连接按钮 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
private void button2_Click(object sender, EventArgs e) { int port = 2000; string host = "127.0.0.1";//设置为本地环回地址,易于测试 ///创建终结点EndPoint IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndpoint实例 try { c.Connect(ipe); } catch (SocketException se) { status.Text = "连接失败"; return; } status.Text = "连接成功"; } //点击发送信息按钮 private void button3_Click(object sender, EventArgs e) { string sendStr = textBox1.Text; byte[] bs = Encoding.UTF8.GetBytes(sendStr);//把字符串编码为字节 try { c.Send(bs, bs.Length, 0);//发送信息 } catch (SocketException se) { status.Text = "发送信息失败"; } status.Text = "发送信息完成"; //接收从服务器返回的信息 string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes=0; try { } catch (SocketException se) { status.Text = "接收信息失败"; bytes = c.Receive(recvBytes, recvBytes.Length, 0); } status.Text = "接收信息成功"; //从服务器端接受返回信息 recvStr = Encoding.UTF8.GetString(recvBytes, 0, bytes);
textBox1.Text += Environment.NewLine + recvStr;//显示服务器返回 } //断开连接 private void button1_Click(object sender, EventArgs e) { c.Shutdown(SocketShutdown.Both); c.Close(); status.Text = "连接已断开"; } 服务器程序:(主要函数) static void Main(string[] args) { TcpListener mylistener = new TcpListener(2000);//侦听端口2000 try { mylistener.Start(); } catch (SocketException se) { Console.WriteLine("无法侦听端口"); return; } Console.WriteLine("成功侦听"); Console.WriteLine("等待客户端... ..."); while (true) { Socket theconn = mylistener.AcceptSocket(); printclientinfo((IPEndPoint)theconn.RemoteEndPoint); //线程调度 threadprocessor tp = new threadprocessor(theconn); Thread socketthread = new Thread(new ThreadStart(tp.processservice)); socketthread.Start(); } } public static void printclientinfo(IPEndPoint clientep) { IPAddress clientip = clientep.Address; Console.WriteLine("客户端 ip:" + clientip.ToString()); } class threadprocessor { private Socket client; public threadprocessor(Socket clientsocket) {
client = clientsocket; } public void processservice() { int bytes = 0; Byte[] recvbytes = new byte[256]; while (true) { try { bytes = client.Receive(recvbytes, recvbytes.Length, 0); } catch (SocketException se) { Console.WriteLine("an error reading data"); break; if (bytes == 0) break; } } try { } catch { client.Send(recvbytes, bytes, 0); Console.WriteLine("an error sending data"); break; } client.Close(); Console.WriteLine("客户端断开连接"); } }
分享到:
收藏