c sharp与python通信

最近在学unity,想在unity调用python。因此学习了使用udp来建立通信。

python发送,c sharp接收

python代码

import socket
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverAddressPort = ("127.0.0.1", 10086)      # 5052 定义localhost与端口,当然可以定义其他的host

count = 0
while True:
    sock.sendto(str.encode(str(count)), serverAddressPort)
    count += 1
    time.sleep(1)

c sharp代码

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

namespace Client
{
    class setIpAndPort
    {
        private string ip;
        private int port;
        public void setIp(string Ip)
        {
            ip = Ip;
        }
        public void setPort(int Port)
        {
            port = Port;
        }

        public setIpAndPort(string Ip, int Port)
        {
            ip = Ip;
            port = Port;
        }
        public string getIp()
        {
            return ip;
        }
        public int getPort()
        {
            return port;
        }

    }

    class Program
    {
        
        static void Main(string[] args)
        {
            // 服务端
            setIpAndPort Setting = new setIpAndPort("127.0.0.1", 10086);
            // 1、创建Udp Socket
            Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // 2、绑定ip跟端口号
            udpServer.Bind(new IPEndPoint(IPAddress.Parse(Setting.getIp()), Setting.getPort()));
            // 3、接受数据
            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = new byte[1024];

            while (true)
            {
                int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);
                // 这个方法会把数据的来源(ip:port)放在第二个参数上。
                string message = Encoding.UTF8.GetString(data, 0, length);
                Console.WriteLine("data: "+message);
                
            }
        }
    }

}

##############################################################
python接收,c sharp发送
python代码

import socket  # 导入socket库

HOST = '127.0.0.1'
PORT = 10086
ADDR = (HOST, PORT)
BUFFSIZE = 1024  # 定义一次从socket缓冲区最多读入1024个字节
MAX_LISTEN = 5  # 表示最多能接受的等待连接的客户端的个数

# 创建UDP服务
def udpServer():
    # 创建UPD服务端套接字
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        # 绑定地址和端口
        s.bind(ADDR)
        # 等待接收信息
        while True:
            # 接收数据和客户端请求地址
            data, address = s.recvfrom(BUFFSIZE)
            if not data:
                break
            info = data.decode()
            print(f'接收请求信息:{info}')
        s.close()


if __name__ == '__main__':
    udpServer()

c sharp 代码

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

namespace Client
{
    
    class Program
    {
       
            // 1、创建socket
        static void Main(string[] args)
        {
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // 2、发送数据
            EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10086);
            string message = "客户端发来的数据";
            byte[] data = Encoding.UTF8.GetBytes(message);
            while(true)
                udpClient.SendTo(data, serverPoint);
            Console.ReadKey();
        }
    }

}
posted @ 2023-04-12 21:19  快乐码小农  阅读(108)  评论(0编辑  收藏  举报