djboris90
djboris90

Reputation: 41

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). 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# code:

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:

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

Upvotes: 2

Views: 9494

Answers (2)

djboris90
djboris90

Reputation: 41

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:

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:

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()

Upvotes: 2

Roland Bär
Roland Bär

Reputation: 1730

When you bind to a specific IP Address, the server only listens on the interface used for this IP Address. 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.

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? ...

Upvotes: 1

Related Questions