TCP和UDP编程习题.doc
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 SendData(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 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, dataleft, 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.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); 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 = SendData(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 (true) 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("与服务器端断绝连接:" + (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(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 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, dataleft, 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(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 = Encoding.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); string 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协议编写服务器端和客户端的控制台程序,要求在连接成功后首先由服务器端向客户端发送欢迎标语,然后由客户端连续发送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; 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.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("本地地址:" + 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(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().Trim(); 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;namespace 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.WriteLine("等待客户端连接."); 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); 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); 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();