c# socknet编程

c#服务端

using System.Text;
using System.Net.Sockets;
using System.Net;

class proj
{
    internal static void Main(string[] args)
    {
        string ip = "";
        int port = 5000;
        byte[] buffer=new byte[1024*1024*2];
        Socket socket =null;
        //1 实例化套接字(ipv4寻址协议,流式协议,Tcp协议)
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //创建一个ip对象
        IPAddress address=IPAddress.Parse(ip);
        //创建一个网络端口
        IPEndPoint endPoint = new IPEndPoint(address, port);
        Console.WriteLine("endPoint: {0} ,typeof(endpoint):{1}", endPoint,endPoint.GetType);
        //绑定套接字
        socket.Bind(endPoint);
        //设置最大连接数
        socket.Listen(int.MaxValue);
        //
        Console.WriteLine("监听{0}消息成功", socket.LocalEndPoint.ToString());
        //阻塞,等待客户端连接
        Socket clinetSocket=socket.Accept();
        Console.WriteLine("客户端信息{0}",clinetSocket.LocalEndPoint.ToString());
        string mess=Console.ReadLine();
        //发送消息
        clinetSocket.Send(Encoding.UTF8.GetBytes(mess));
        //接收消息
        clinetSocket.Receive(buffer);
        Console.WriteLine(Encoding.UTF8.GetString(buffer));

    }
}

python 客户端

import socket

a="你好"
b=bytes(a,encoding='utf-8')
print(b)
c=str(b,encoding='utf-8')
print(c)

b=a.encode('utf-8')
print(b)
c=b.decode('utf-8')
print(c)

cs=socket.socket()
# 连接服务端
h=cs.connect(("192.168.42.191",5000))
print('hh',h)
# 接受消息
data=cs.recv(1024)
print(type(data))
print(data)
print(data.decode('utf-8'))

#发送消息
mess=input("请输入你想发的消息")
cs.sendall(mess.encode('utf-8'))

c# 客户端

using System.Text;
using System.Net.Sockets;
using System.Net;

class proj
{
    internal static void Main(string[] args)
    {
        string ip = "127.0.0.1";
        int port = 5000;
        byte[] buffer=new byte[1024*1024*2];
        Socket socket =null;
        //1 实例化套接字(ipv4寻址协议,流式协议,Tcp协议)
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //创建一个ip对象
        IPAddress address=IPAddress.Parse(ip);
        //创建一个网络端口
        IPEndPoint endPoint = new IPEndPoint(address, port);
        Console.WriteLine("endPoint: {0} ,typeof(endpoint):{1}", endPoint,endPoint.GetType);
        // 建立连接
        socket.Connect(endPoint);
        Console.WriteLine("连接服务器成功");
        //接收数据
        int lenth = socket.Receive(buffer);
        Console.WriteLine("接受服务器{0},消息{1}", socket.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(buffer));
        //发送数据
        socket.Send(Encoding.UTF8.GetBytes("我是c#"));
      
    }
}

python 服务端

import socket
# 服务端应用

ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.bind(("0.0.0.0", 5000))  # 服务器绑定ip
ss.listen(5)  # 开始监听
tcp1, add = ss.accept()  # 阻塞进程,等待客户端连接
print('tcp1', tcp1)
print('客户端连接成功,客户端IP地址', add)


tcp1.sendall("我是python".encode('utf-8'))
data = tcp1.recv(102400)  # 接受消息
data = str(data, encoding='utf-8')
print('接受的消息是:',data)


参考:
c# socket编程

你可能感兴趣的:(c#开发,c#,网络,php)