当前位置:文档之家› 简单的tcp一对一,一对多通信

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

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

2. 实现窗体程序下tcp通信中,服务器端和客户端的通信程序,实现一对一多次通信。

3. 尝试服务器能和多个客户端通信,并行模式(服务器可以同时和多个客户端进行收发信息的通信),提示:服务器端程序每来一客户端连接请求,需要打开一个新的窗口来实现和客户端的通信。

(此部分内容,实验报告上可以不体现)三、实验过程第一题:服务器端代码:namespace server_more{class Program{private static byte[] result = new byte[1024];private static int myprot = 8889;static Socket serverSocket;static void Main(string[] args){IPAddress ip = IPAddress.Parse("127.0.0.1");serverSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);serverSocket.Bind(new IPEndPoint(ip, myprot));serverSocket.Listen(10);Console.WriteLine("启动监听成功|", serverSocket.LocalEndPoint.ToString());Thread myThred = new Thread(ListenClientConnect);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(clientsocket); } }private static void ReceiveMessage(object clientSocket){Socket myClientSocket = (Socket)clientSocket;while (true){try{int receiveNumber = myClientSocket.Receive(result);Console.WriteLine("接受客户端消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber)); }catch (Exception ex){Console.WriteLine(ex.Message);myClientSocket.Shutdown(SocketShutdown.Both);myClientSocket.Close();break; }}}}客户端代码:namespace server_client{class Program{private static byte[] result=new byte[1024];static void Main(string[] args){IPAddress ip = IPAddress.Parse("127.0.0.1");Socket clinetsocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);try{clinetsocket.Connect(new IPEndPoint(ip, 8889));Console.WriteLine("连接服务器成功!");}catch{ Console.WriteLine("连接服务器失败!");return; }int receiveLength=clinetsocket.Receive(result);Console.WriteLine("接收服务器消息{0}",Encoding.ASCII.GetString(result,0,receiveLength));for(int i=0;i<10;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.WriteLine("发送完毕,按任意键继续:");Console.ReadLine();}}}第二题服务器代码:namespace server_one{public partial class Form1 : Form{public Form1(){InitializeComponent(); }Socket communisoc;Socket listensoc;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)try{ byte[] recv = new byte[100];int len = communisoc.Receive(recv);addstr(Encoding.Default.GetString(recv, 0, len)); }catch{break;} }private void button2_Click(object sender, EventArgs e){listensoc=new Socket(AddressFamily.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(recvfun);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(); }}}客户端代码:namespace client_one{public 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)try{byte[] recv = new byte[100];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.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();}}}第三题:服务器端Form1:namespace server2{public partial class Form1 : Form{Socket listensoc;Socket communisoc;public Form1(){ InitializeComponent(); }void accthread(){while (true){ communisoc = listensoc.Accept();communisoc.Send(Encoding.Default.GetBytes("hello i am server!"));Thread t = new Thread(openf2);t.IsBackground = true;t.Start(communisoc);}}void openf2(object comsoc){Form2 f2 = new Form2(communisoc);f2.ShowDialog();}private void button1_Click(object sender, EventArgs e){listensoc=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddress serIp = IPAddress.Parse("127.0.0.1");IPEndPoint ipP = new IPEndPoint(serIp, 6666);listensoc.Bind(ipP);listensoc.Listen(3);Thread t = new Thread(accthread);t.IsBackground = true;t.Start();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){ listensoc.Close(); }}}From2:namespace server2{public partial class Form2 : Form{ public Socket communisoc;public Form2(Socket comsoc){ InitializeComponent();comsoc = communisoc; }delegate void adddel(string addstr);void addstr(string str){ if (txtReceive.InvokeRequired){ adddel d = addstr;txtReceive.Invoke(d, str); }elsetxtReceive.AppendText(str); }void recvfun(){ while (true){ try{ byte[] recvbuf = new byte[100];int len = communisoc.Receive(recvbuf);addstr(Encoding.Default.GetString(recvbuf, 0, len));}catch { communisoc.Close(); }}}private void button1_Click(object sender, EventArgs e){ communisoc.Send(Encoding.Default.GetBytes(txtSend.Text)); }private void Form2_Load(object sender, EventArgs e){Thread t = new Thread(recvfun);t.IsBackground = true;t.Start();}}}客户端代码与上边一对一多次通信一样。

相关主题