2

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

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Firstly tnx for reply. I changed to 'IpAddress.Any' as you proposed and now I can communicate with another windows c# client on different computer, but still I can communicate with my rpi. I got timeout error while trying to connect on server. Any guesses?
also -ping from command line from both devices working just fine
If you can connect from the C# client to the server from another computer your server should be OK. As I have no experience with neither Python Sockets nor Rasperty PI I can only guess what to do next. Maybe you can try to connect to a web server on the internet as in the first example on docs.python.org/2/howto/sockets.html or you check if the Raspery PI has some configuration that prevents outgoing socket connections...
Tnx for your assitance, but unfortunatelly everything seems the same like in the tutorial. Maybe the problem is with the network privilages on my win com server or sth else. I'll keep trying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.