C#:Socket通信

Posted 菜鸟程序员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#:Socket通信相关的知识,希望对你有一定的参考价值。

Server代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//添加Socket类
using System.Net;
using System.Net.Sockets;
 
namespace SockeConsoleServer
{
    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);
 
            //创建Socket并开始监听
 
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket对象,如果用UDP协议,则要用SocketTyype.Dgram类型的套接字
            s.Bind(ipe);    //绑定EndPoint对象(2000端口和ip地址)
            s.Listen(0);    //开始监听
 
            Console.WriteLine("等待客户端连接");
 
            //接受到Client连接,为此连接建立新的Socket,并接受消息
            Socket temp = s.Accept();   //为新建立的连接创建新的Socket
            Console.WriteLine("建立连接");
            string recvStr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = temp.Receive(recvBytes,recvBytes.Length,0); //从客户端接受消息
            recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
 
            //给Client端返回信息
            Console.WriteLine("server get message:{0}",recvStr);    //把客户端传来的信息显示出来
            string sendStr = "ok!Client send message successful!";
            byte[] bs = Encoding.ASCII.GetBytes(sendStr);
            temp.Send(bs,bs.Length,0);  //返回信息给客户端
            temp.Close();
            s.Close();
            Console.ReadLine();
 
        }
    }
}

  

Client代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//添加用于Socket的类
using System.Net;
using System.Net.Sockets;
 
namespace SocketConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int port = 2000;
                string host = "127.0.0.1";
                //创建终结点EndPoint
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);   //把ip和端口转化为IPEndPoint的实例
 
                //创建Socket并连接到服务器
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   //  创建Socket
                Console.WriteLine("Connecting...");
                c.Connect(ipe); //连接到服务器
 
                //向服务器发送信息
                string sendStr = "Hello,this is a socket test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);   //把字符串编码为字节
 
                Console.WriteLine("Send message");
                c.Send(bs, bs.Length, 0); //发送信息
 
                //接受从服务器返回的信息
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);    //从服务器端接受返回信息
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine("client get message:{0}", recvStr);    //回显服务器的返回信息
 
                Console.ReadLine();
                //一定记着用完Socket后要关闭
                c.Close();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("argumentNullException:{0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}",e);
            }
        }
    }
}

  

以上是关于C#:Socket通信的主要内容,如果未能解决你的问题,请参考以下文章

C#:Socket通信

C# Socket通信的服务器与客户端

Socket通信实例(C#)

c# socket 通信时如何限速?

C# Socket通信DEMO

C# Socket异步通信