socket的简单通讯实例

 分为服务端和客服端,先看服务端吧:

做一个控制台程序,

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Socket
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            string host = "127.0.0.1";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip,port);
            System.Net.Sockets.Socket  s = new System.Net.Sockets.Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(0);
            Console.WriteLine("等待客服端的连接...");
            System.Net.Sockets.Socket temp = s.Accept();
            Console.WriteLine("建立连接");
            string recvStr = "";
            byte[] recvBytes=new byte[1024];
            int bytes;
            bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
            recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
            Console.WriteLine("取到信息:{0}",recvStr);
            string sendStr = "客户端发送信息成功";
            byte[] bs = Encoding.UTF8.GetBytes(sendStr);
            temp.Send(bs, bs.Length, 0);
            temp.Close();
            s.Close();
            Console.ReadLine();


        }
    }
}

再看客服端:

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace socketclient
{
    class Program
    {
        static void Main(string[] args)
        {
             try
            {
                int port = 2000;
                string host = "127.0.0.1";
               /**////创建终结点EndPoint
                IPAddress ip = IPAddress.Parse(host);
                //IPAddress ipp = new IPAddress("127.0.0.1");
                IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndpoint实例

              /**////创建socket并连接到服务器
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
                Console.WriteLine("连接…");
                Console.ReadLine();
                c.Connect(ipe);//连接到服务器

              /**////向服务器发送信息
                string sendStr = "这个是客户端发来的消息~~";
                byte[] bs = Encoding.UTF8.GetBytes(sendStr);//把字符串编码为字节
                Console.WriteLine("发送消息");
                Console.ReadLine();
                c.Send(bs, bs.Length, 0);//发送信息

             /**////接受从服务器返回的信息
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
               
                Console.WriteLine("服务端返回信息:{0}", recvStr);//显示服务器返回信息
              /**////一定记着用完socket后要关闭
                c.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("argumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}", e);
            }
            Console.WriteLine("Press Enter to Exit");
            Console.ReadLine();

        }
    }
}

 

先运行服务端,再运行客服端就可以实现通信啦

你可能感兴趣的:(socket的简单通讯实例)