TCP和UDP编程习题.doc
《TCP和UDP编程习题.doc》由会员分享,可在线阅读,更多相关《TCP和UDP编程习题.doc(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TCP UDP 编程 习题
限制150内