C#笔记,socket通信代码

来源:互联网 发布:看工程图纸软件 编辑:程序博客网 时间:2024/06/07 00:47
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;namespace SocketServer{    class Program    {        static Socket ServerSocket;        static Socket MyClient;        static void Main(string[] args)        {            IPAddress ip = IPAddress.Parse("127.0.0.1");//服务器端IP地址              IPEndPoint iep = new IPEndPoint(ip, 5555);//服务端地址及端口                                                        //1.建立套接字,以Tcp协议链接,字节流的方式进行数据传输              ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            //设置Socket地址可重复使用              ServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);            //2.绑定套接字              ServerSocket.Bind(iep);            //3.监听套接字              ServerSocket.Listen(10);            //4.等待客户端请求              MyClient = ServerSocket.Accept();            //5.向客户端发送消息              MyClient.Send(Encoding.Unicode.GetBytes("链接成功"));            while (true)            {                new Thread(receive).Start();                new Thread(send).Start();            }            //关闭套接字的发送与接收              MyClient.Shutdown(SocketShutdown.Both);            //关闭MyClient              MyClient.Close();            //关闭MySocket              ServerSocket.Close();        }        //接收消息        public static void receive()        {            //定义字节数组              byte[] buf = new byte[1024];            //3.接收来自服务端的信息,并保存在字节数组中              int k = MyClient.Receive(buf);            //输出显示服务端发送的信息              Console.WriteLine(Encoding.Unicode.GetString(buf, 0, k));        }        //发送消息        public static void send()        {            string sendstr = Console.ReadLine();            if (sendstr.Length > 0 && !sendstr.Equals(null))                MyClient.Send(Encoding.Unicode.GetBytes(sendstr));            else Console.WriteLine("不能发送空字符,请重新输入");        }    }}
using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;namespace SocketClient{    class Program    {        static Socket ClientSocket;        static void Main(string[] args)        {            //所连接服务器端IP地址              IPAddress remoteIp = IPAddress.Parse("127.0.0.1");            //所连接服务端地址及端口              IPEndPoint iep = new IPEndPoint(remoteIp, 5555);            //1.建立套接字,以Tcp协议链接,字节流的方式进行数据传输              ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            try            {                //2.建立与服务端的链接                  ClientSocket.Connect(iep);                //3.向服务端发送信息                  ClientSocket.Send(Encoding.Unicode.GetBytes("客户已经连接"));                while (true)                {                    new Thread(receive).Start();                    new Thread(send).Start();                }                            }                     catch (Exception ex)            {                Console.WriteLine("Unexpection Exception:{0}", ex.ToString());            }            finally            {                //4.关闭套接字                  ClientSocket.Shutdown(SocketShutdown.Both);                ClientSocket.Close();            }        }        //接收消息        public static void receive()        {            //定义字节数组              byte[] buf = new byte[1024];            //3.接收来自服务端的信息,并保存在字节数组中              int k = ClientSocket.Receive(buf);            //输出显示服务端发送的信息              Console.WriteLine(Encoding.Unicode.GetString(buf, 0, k));        }        //发送消息        public static void send()        {            string sendstr = Console.ReadLine();            if (sendstr.Length > 0 && !sendstr.Equals(null))                ClientSocket.Send(Encoding.Unicode.GetBytes(sendstr));            else Console.WriteLine("不能发送空字符,请重新输入");        }    }}