《C#完整的通信代码.doc》由会员分享,可在线阅读,更多相关《C#完整的通信代码.doc(20页珍藏版)》请在金锄头文库上搜索。
1、C#完整的通信代码(一)(点对点,点对多,同步,异步,UDP,TCP) 2008-08-06 14:31:40 | 分类: C#应用 | 标签: qq源码 p2p 源码 通讯 p2p通讯 点对点通讯 聊天 | 字号 订阅 C# codenamespace UDPServer class Program static void Main(string args) int recv; byte data = new byte1024; /构建TCP 服务器 /得到本机IP,设置TCP端口号 IPEndPoint ipep = new IPEndPoint(IPAddress.Any , 8001)
2、; Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp); /绑定网络地址 newsock.Bind(ipep); Console.WriteLine(This is a Server, host name is 0,Dns.GetHostName(); /等待客户机连接 Console.WriteLine(Waiting for a client.); /得到客户机IP IPEndPoint sender = new IPEndPoint(IPAddress.A
3、ny, 0); EndPoint Remote = (EndPoint)(sender); recv = newsock.ReceiveFrom(data, ref Remote); Console .WriteLine (Message received from 0: , Remote.ToString (); Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv ); /客户机连接成功后,发送欢迎信息 string welcome = Welcome ! ; /字符串与字节数组相互转换 data = Encoding .
4、ASCII .GetBytes (welcome ); /发送信息 newsock .SendTo (data ,data.Length ,SocketFlags .None ,Remote ); while (true ) data =new byte 1024; /发送接受信息 recv =newsock.ReceiveFrom(data ,ref Remote); Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv); newsock .SendTo (data ,recv ,SocketFlags .None ,Re
5、mote ); C# codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace UDPClient class Program static void Main(string args) byte data = new byte1024; string input ,stringData; /构建TCP 服务器 Console.WriteLine(This is a Client
6、, host name is 0, Dns.GetHostName(); /设置服务IP,设置TCP端口号 IPEndPoint ipep = new IPEndPoint(IPAddress .Parse (127.0.0.1) , 8001); /定义网络类型,数据连接类型和网络协议UDP Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string welcome = Hello! ; data = Encoding.ASCII.GetBytes(wel
7、come); server.SendTo(data, data.Length, SocketFlags.None, ipep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; data = new byte1024; int recv = server.ReceiveFrom(data, ref Remote); Console.WriteLine(Message received from 0: , Remote.ToString(); Console.Wri
8、teLine(Encoding .ASCII .GetString (data,0,recv); while (true) input = Console .ReadLine (); if (input =exit) break ; server .SendTo (Encoding .ASCII .GetBytes (input ),Remote ); data = new byte 1024; recv = server.ReceiveFrom(data, ref Remote); stringData = Encoding.ASCII.GetString(data, 0, recv); C
9、onsole.WriteLine(stringData); Console .WriteLine (Stopping Client.); server .Close (); C# codeTCPClientTCPClient 类提供了一种使用 TCP 协议连接到某个端点的简化方法。它还通过 NetworkStream 对象展现在连接过程中读取或写入的数据。请参见下面从 QuickStart 文档中摘录的日期/时间客户机示例。使用 C# 编写using System;using System.Net;using System.Net.Sockets;using System.IO;using S
10、ystem.Text;class Clientpublic static void Main(String args)TCPClient tcpc = new TCPClient();Byte read = new Byte32;if (args.Length != 1)Console.WriteLine(“请在命令行中指定服务器名称”);return;String server = args0;/ 验证服务器是否存在if (DNS.GetHostByName(server) = null)Console.WriteLine(“找不到服务器:” + 服务器);return;/ 尝试连接到服务器
11、if (tcpc.Connect(server, 13) = -1)Console.WriteLine(“无法连接到服务器:” + 服务器);return;/ 获取流Stream s = tcpc.GetStream();/ 读取流并将它转换为 ASCII 码形式int bytes = s.Read(read, 0, read.Length);String Time = Encoding.ASCII.GetString(read);/ 显示数据Console.WriteLine(“已接收到的” + 字节 + “字节”);Console.WriteLine(“当前日期和时间是:” + 时间);t
12、cpc.Close();TCPListenerTCPListener 类便于在来自某个客户机的 TCP 连接的特定套接字上进行侦听的工作。请参见下面包括在 QuickStart 文档中的日期/时间服务器示例。使用 C# 编写using System;using System.Net;using System.Net.Sockets;using System.Text;class Serverpublic static void Main()DateTime now;String strDateLine;Encoding ASCII = Encoding.ASCII;/ 在端口 13 进行侦听TCPListener tcpl = new TCPListener(13);tcpl.Start();Console.WriteLine(“正在等待客户进行连接”);Console.WriteLine