简体   繁体   English

Windows上的TCP服务器C#,TCP客户端Python树莓派

[英]tcp server c# on windows, tcp client python raspberry pi

I am working on my project where I have to communicate between my tcp server written in c# on windows and my client written in python on raspbian (raspberry pi). 我正在我的项目上工作,我必须在Windows上用C#编写的tcp服务器和在raspbian(raspberry pi)上用python编写的客户端之间进行通信。 My server is working fine (tested on local machine with client in c#), but when run client data isn't sent to the server side. 我的服务器运行正常(在本地计算机上使用c#进行了客户端测试),但是运行时客户端数据未发送到服务器端。

c# code: C#代码:

static void Main(string[] args)
    {

        IPAddress localAdd = IPAddress.Parse(SERVER_IP);
        TcpListener listener = new TcpListener(localAdd, PORT_NO);
        Console.WriteLine("Krenuo sa radom...");
        listener.Start();

        while (true)
        {

            TcpClient client = listener.AcceptTcpClient();


            NetworkStream nwStream = client.GetStream();
            byte[] buffer = new byte[client.ReceiveBufferSize];


            int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);


            string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Primljeno : " + dataReceived);


            Console.WriteLine("Dobijena poruka na serveru : " + dataReceived);
            nwStream.Write(buffer, 0, bytesRead);

            Console.WriteLine("\n");

            client.Close();
        }
        listener.Stop();

python code: python代码:

def send(ctrl_cyc):

HOST, PORT = "10.93.34.41", 5000
data = ""
data += str(ctrl_cyc)

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
# Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

finally:
    sock.close()
return True

I found solution... Well i think I found it. 我找到了解决方案...好吧,我想我找到了。 The thing is that probably my account doesn't have rights to listen other devices in network (network configuration). 问题是,我的帐户可能没有监听网络中其他设备的权限(网络配置)。 I tried my solution on admin's computer and it is working, and also I put it on company's server and it is working just fine. 我在管理员的计算机上尝试了我的解决方案,并且该解决方案正在运行,并且将其放在公司的服务器上,并且运行良好。

Here are codes if anyone needs them. 如果有人需要,这里是代码。

c# code: C#代码:

static void Main(string[] args)
    {

        IPAddress localAdd = IPAddress.Parse(SERVER_IP);
        TcpListener listener = new TcpListener(IPAddress.Any, PORT_NO);
        Console.WriteLine("Krenuo sa radom...");
        listener.Start();

        while (true)
        {

            TcpClient client = listener.AcceptTcpClient();


            NetworkStream nwStream = client.GetStream();
            byte[] buffer = new byte[client.ReceiveBufferSize];


            int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);


            string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Primljeno : " + dataReceived);


            Console.WriteLine("Dobijena poruka na serveru : " + dataReceived);
            nwStream.Write(buffer, 0, bytesRead);

            Console.WriteLine("\n");

            client.Close();
        }
        listener.Stop();
    }

python code: python代码:

import socket

HOST, PORT = "10.XX.XX.XX", 5000
ctrl_cyc="1234567"
data = ""
data += str(ctrl_cyc)

    # Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n"))
    data = sock.recv(1024)
    print data

finally:
    sock.close()

When you bind to a specific IP Address, the server only listens on the interface used for this IP Address. 当您绑定到特定的IP地址时,服务器仅侦听用于该IP地址的接口。 So if you have more then one Network adapter and you want to listen on all of them, use IpAddress.Any in the Constructor of the TcpListener. 因此,如果您有一个以上的网络适配器,并且想要监听所有这些适配器,请在TcpListener的构造函数中使用IpAddress.Any

If this is not the case, could you give us some more information: Is the client giving any error information/exception? 如果不是这种情况,您能否给我们更多信息:客户端是否提供任何错误信息/异常? Have you sniffed the Traffic between client and server? 您是否嗅探客户端和服务器之间的流量? Is the connection established? 是否建立连接? ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM