《TCP和UDP编程习题.doc》由会员分享,可在线阅读,更多相关《TCP和UDP编程习题.doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1.使用TCP协议编写服务器端和客户端的控制台程序,要求在连接成功后首先由服务器端向客户端发送欢迎标语,然后由客户端连续发送5条具体的信息,再由服务器端发送5条具体的信息,最后实现在服务器端和客户端之间的同步通信,其中通信的信息要能支持中文。服务器端代码using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;namespace VarTcpClient class VarTcpClient private static int SendDa
2、ta(Socket s, byte data) int total = 0; int size = data.Length; int dataleft = size; int sent; byte datasize = new byte4; datasize = BitConverter.GetBytes(size); sent = s.Send(datasize); while (total size) sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; return
3、 total; private static byte ReceiveData(Socket s) int total = 0; byte datasize = new byte4; int recv; recv = s.Receive(datasize, 0, 4, SocketFlags.None); int size = BitConverter.ToInt32(datasize, 0); int dataleft = size; byte data = new bytesize; while (total size) recv = s.Receive(data, total, data
4、left, SocketFlags.None); if (recv = 0) data = Encoding.ASCII.GetBytes(BYE); break; total += recv; dataleft -= recv; return data; static void Main(string args) byte data = new byte1024; byte data1 = new byte1024; byte data2 = new byte1024; int sent; IPEndPoint iep = new IPEndPoint(IPAddress.Parse(127
5、.0.0.1), 9050); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try client.Connect(iep); catch (SocketException e) Console.WriteLine(连接失败.); Console.WriteLine(e.ToString(); return; data = ReceiveData(client); string welcome = Encoding.ASCII.GetString(data
6、); Console.WriteLine(welcome); string message01 = This is the first test.; string message02 = a; string message03 = This string is a even longer test.This string is a even longer test.This string is a even longer test.; string message04 = a short test.; string message05 = This is a story.; sent = Se
7、ndData(client, Encoding.ASCII.GetBytes(message01); sent = SendData(client, Encoding.ASCII.GetBytes(message02); sent = SendData(client, Encoding.ASCII.GetBytes(message03); sent = SendData(client, Encoding.ASCII.GetBytes(message04); sent = SendData(client, Encoding.ASCII.GetBytes(message05); while (tr
8、ue) string input = Console.ReadLine(); if (input.ToString().ToUpper() = EXIT) break; data1 = Encoding.Default.GetBytes(input); sent = SendData(client, data1); data2 = ReceiveData(client); string welcome1 = Encoding.Default.GetString(data2); Console.WriteLine(welcome1); Console.WriteLine(与服务器端断绝连接: +
9、 (IPEndPoint)client.RemoteEndPoint).ToString(); client.Shutdown(SocketShutdown.Both); Console.ReadLine(); client.Close(); 客户端代码using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;namespace VarTcpSrvr class VarTcpSrvr private static int SendData(S
10、ocket s, byte data) int total = 0; int size = data.Length; int dataleft = size; int sent; byte datasize = new byte4; datasize = BitConverter.GetBytes(size); sent = s.Send(datasize); while (total size) sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; return tot
11、al; private static byte ReceiveData(Socket s) int total = 0; byte datasize = new byte4; int recv; recv = s.Receive(datasize, 0, 4, SocketFlags.None); int size = BitConverter.ToInt32(datasize,0); int dataleft = size; byte data = new bytesize; while (total size) recv = s.Receive(data, total, dataleft,
12、 SocketFlags.None); if (recv = 0) data = Encoding.ASCII.GetBytes(BYE); break; total += recv; dataleft -= recv; return data; static void Main(string args) byte data = new byte1024; byte data1 = new byte1024; IPEndPoint iep = new IPEndPoint(IPAddress.Parse(127.0.0.1), 9050); Socket server = new Socket
13、(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(iep); server.Listen(5); Console.WriteLine(正在等待客户端连接.); Socket newSocket = server.Accept(); Console.WriteLine(连接客户端: + (IPEndPoint)newSocket.RemoteEndPoint).ToString(); string welcom = welcome to my test server.; data = En
14、coding.ASCII.GetBytes(welcom); int sent = SendData(newSocket, data); for (int i = 0; i 5; i+) data = ReceiveData(newSocket); string message = Encoding.Default.GetString(data); Console.WriteLine(message); while(true) data = ReceiveData(newSocket); string message = Encoding.Default.GetString(data); st
15、ring recv=message; if(recv=EXIT) break; Console.WriteLine(message); string input = Console.ReadLine(); data1 = Encoding.Default.GetBytes(input); int sent1 = SendData(newSocket, data1); Console.WriteLine(与客户端断绝连接: + (IPEndPoint)newSocket.RemoteEndPoint).ToString(); Console.ReadLine(); 2.使用UDP协议编写服务器端
16、和客户端的控制台程序,要求在连接成功后首先由服务器端向客户端发送欢迎标语,然后由客户端连续发送5条具体的信息,再由服务器端发送5条具体的信息,最后实现在服务器端和客户端之间的同步通信,其中通信的信息要能支持中文。服务器端代码using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;namespace TestUdpClient class TestUdpClient static void Main(string args) int recv
17、; string input, StringData; byte data = new byte1024; IPEndPoint iep = new IPEndPoint(IPAddress.Parse(127.0.0.1), 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string welcome = Hello,are you there ?; data = Encoding.ASCII.GetBytes(welcome); server
18、.SendTo(data, 0, welcome.Length, SocketFlags.None, iep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint remote = (EndPoint)(sender); data = new byte1024; recv = server.ReceiveFrom(data, ref remote); Console.WriteLine(Message received from + remote.ToString(); Console.WriteLine(本地地址:
19、+ server.LocalEndPoint.ToString(); input = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(input); server.SendTo(Encoding.ASCII.GetBytes(message01), remote); server.SendTo(Encoding.ASCII.GetBytes(message02), remote); server.SendTo(Encoding.ASCII.GetBytes(message03), remote); server.SendTo
20、(Encoding.ASCII.GetBytes(message04), remote); server.SendTo(Encoding.ASCII.GetBytes(message05), remote); while (true) recv = server.ReceiveFrom(data, ref remote); StringData = Encoding.Default.GetString(data, 0, recv); Console.WriteLine(StringData); data = new byte1024; input = Console.ReadLine().Tr
21、im(); if (input = exit) break; data = Encoding.Default.GetBytes(input); server.SendTo(data, remote); data = new byte1024; Console.WriteLine(停止连接.); Console.ReadLine(); server.Close(); 客户端代码using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;names
22、pace TestUdpSrvr class TestUdpSrvr static void Main(string args) int recv; string input, StringData; byte data = new byte1024; IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.Bind(iep); Console.W
23、riteLine(等待客户端连接.); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint remote = (EndPoint)(sender); recv = server.ReceiveFrom(data, ref remote); StringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(Message received from + remote.ToString(); Console.WriteLine(StringData
24、); string welcome = Welcome to my test server.; data = Encoding.ASCII.GetBytes(welcome); server.SendTo(data, 0, welcome.Length, SocketFlags.None, remote); for (int i = 0; i 5; i+) data = new byte1024; recv = server.ReceiveFrom(data, ref remote); StringData = Encoding.ASCII.GetString(data, 0, recv);
25、Console.WriteLine(StringData); while (true) input = Console.ReadLine().Trim(); if (input = exit) break; data = new byte1024; data = Encoding.Default.GetBytes(input); server.SendTo(data, remote); data = new byte1024; recv = server.ReceiveFrom(data, ref remote); StringData = Encoding.Default.GetString(data, 0, recv); Console.WriteLine(StringData); Console.ReadLine(); server.Close();