简单的tcp一对一,一对多通信

上传人:豆浆 文档编号:31928633 上传时间:2018-02-09 格式:DOC 页数:8 大小:179.50KB
返回 下载 相关 举报
简单的tcp一对一,一对多通信_第1页
第1页 / 共8页
简单的tcp一对一,一对多通信_第2页
第2页 / 共8页
简单的tcp一对一,一对多通信_第3页
第3页 / 共8页
简单的tcp一对一,一对多通信_第4页
第4页 / 共8页
简单的tcp一对一,一对多通信_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《简单的tcp一对一,一对多通信》由会员分享,可在线阅读,更多相关《简单的tcp一对一,一对多通信(8页珍藏版)》请在金锄头文库上搜索。

1、课程实验报告课 程 名 称: C#网络编程 实验项目名称: 实验三 窗体 TCP 通信 专 业 班 级: B11522 姓 名: 学 号: 201320120 指 导 教 师: 完 成 时 间: 2014 年 3 月 24 日计算机科学与工程系实验三 窗体 TCP 通信一、实验目的1、掌握 SOKCET 面向连接通信的基本原理;2、掌握窗体程序下套接字通信程序中服务器端的通信流程;3、掌握窗体程序下套接字通信程序中客户端的通信流程;二、实验内容1. 完善控制台程序,实现一对多服务器端和客户端的程序代码,其中服务器只需接收多个客户端的信息,无需给每一个客户端发送回复。2. 实现窗体程序下tcp通

2、信中,服务器端和客户端的通信程序,实现一对一多次通信。3. 尝试服务器能和多个客户端通信,并行模式(服务器可以同时和多个客户端进行收发信息的通信) ,提示:服务器端程序每来一客户端连接请求,需要打开一个新的窗口来实现和客户端的通信。(此部分内容,实验报告上可以不体现)三、实验过程第一题:服务器端代码:namespace server_moreclass Programprivate static byte result = new byte1024;private static int myprot = 8889;static Socket serverSocket;static void M

3、ain(string args)IPAddress ip = IPAddress.Parse(127.0.0.1);serverSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);serverSocket.Bind(new IPEndPoint(ip, myprot);serverSocket.Listen(10);Console.WriteLine(启动监听成功|, serverSocket.LocalEndPoint.ToString();Thread myThred = new T

4、hread(ListenClientConnect); myThred.Start();Console.ReadLine();private static void ListenClientConnect() while (true) Socket clientsocket = serverSocket.Accept();clientsocket.Send(Encoding.ASCII.GetBytes(Server Say Hello);Thread receiveThred = new Thread(ReceiveMessage);receiveThred.Start(clientsock

5、et); private static void ReceiveMessage(object clientSocket)Socket myClientSocket = (Socket)clientSocket;while (true)tryint receiveNumber = myClientSocket.Receive(result);Console.WriteLine(接受客户端消息1, myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber);catch (E

6、xception ex)Console.WriteLine(ex.Message);myClientSocket.Shutdown(SocketShutdown.Both);myClientSocket.Close();break; 客户端代码:namespace server_clientclass Programprivate static byte result=new byte1024;static void Main(string args) IPAddress ip = IPAddress.Parse(127.0.0.1); Socket clinetsocket = new So

7、cket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);tryclinetsocket.Connect(new IPEndPoint(ip, 8889);Console.WriteLine(连接服务器成功!);catch Console.WriteLine(连接服务器失败!);return; int receiveLength=clinetsocket.Receive(result);Console.WriteLine(接收服务器消息0,Encoding.ASCII.GetString(result,0,rece

8、iveLength);string senstr;for(int i=0;i10;i+)try Console.WriteLine(请输入要给服务器发送的信息:);senstr = Console.ReadLine();if (senstr != 0)clinetsocket.Send(Encoding.ASCII.GetBytes(senstr);Console.WriteLine(向服务器发送消息:0, senstr); catch clinetsocket.Shutdown(SocketShutdown.Both);clinetsocket.Close();break; Console.

9、WriteLine(发送完毕,按任意键继续:);Console.ReadLine(); 第二题服务器代码:namespace server_onepublic partial class Form1 : Formpublic Form1()InitializeComponent(); Socket communisoc;Socket listensoc;delegate void richtbdel(string recvstr);void addstr(string add) if (richTextBox1.InvokeRequired) richtbdel del = addstr; r

10、ichTextBox1.Invoke(del, add); elserichTextBox1.AppendText(add); void recvfun() while (true)try byte recv = new byte100;int len = communisoc.Receive(recv);addstr(Encoding.Default.GetString(recv, 0, len); catch break; private void button2_Click(object sender, EventArgs e)listensoc=newSocket(AddressFam

11、ily.InterNetwork,SocketType.Stream, rotocolType.Tcp);IPAddress serIp = IPAddress.Parse(127.0.0.1);IPEndPoint IpP = new IPEndPoint(serIp, 6666);listensoc.Bind(IpP);listensoc.Listen(3);communisoc = listensoc.Accept();communisoc.Send(Encoding.Default.GetBytes(hello i am server!);Thread t = new Thread(r

12、ecvfun);t.IsBackground = true;t.Start(); private void button1_Click(object sender, EventArgs e)communisoc.Send(Encoding.Default.GetBytes(textBox1.Text);private void Form1_FormClosing(object sender, FormClosingEventArgs e)communisoc.Shutdown(SocketShutdown.Both);communisoc.Close();listensoc.Close();

13、客户端代码:namespace client_onepublic partial class Form1 : Form Socket clisoc;delegate void richtbdel(string recvstr);void addstr(string add) if (richTextBox1.InvokeRequired) richtbdel del = addstr;richTextBox1.Invoke(del, add); elserichTextBox1.AppendText(add); void recvfun() while (true)trybyte recv =

14、 new byte100;int len = clisoc.Receive(recv);addstr(Encoding.Default.GetString(recv, 0, len);catch break; public Form1() private void button2_Click(object sender, EventArgs e) clisoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPAddress serIp = IPAddress.Parse(127.0.

15、0.1);IPEndPoint IpP = new IPEndPoint(serIp, 6666);clisoc.Connect(IpP);clisoc.Send(Encoding.Default.GetBytes(i am client!);Thread t = new Thread(recvfun);t.IsBackground = true;t.Start();private void button1_Click(object sender, EventArgs e)clisoc.Send(Encoding.Default.GetBytes(textBox1.Text);private void Form1_FormClosing(object sender, FormClosingEventArgs e)clisoc.Shutdown(SocketShutdown.Both);clisoc.Close();

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号