djboris90 djboris90 - 3 months ago 142
C# Question

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

Answer

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