Reputation: 123
I am trying to send text messages to my Raspberry pi (Python Server) on a click of a button using a C# GUI program..My python Server suppose to print my text messages..I can compile and run both the programs..No error messages whatsoever..However I'm not receiving any messages on my Raspberry pi and the text data is not printing on my Raspberry Pi's Terminal.
Here is C# Client code :
public partial class MainWindow : Window
{
bool button1WasClicked = false;
public MainWindow()
{
InitializeComponent();
}
UdpClient client = new UdpClient();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (button1WasClicked)
{
byte[] a1 = Encoding.ASCII.GetBytes(textbox.Text);
client.Send(a1, a1.Length);
button1WasClicked = false;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Send data when button is clicked
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipbox.Text), int.Parse(portbox.Text)); // endpoint where server is listening
client.Connect(ep);
button1WasClicked = true;
byte[] a1 = Encoding.ASCII.GetBytes(textbox.Text);
client.Send(a1, a1.Length);
}
}
My python Server code :
# Echo server program
import socket
import RPi.GPIO as GPIO
import os
import sys
HOST = '192.168.1.12' # Symbolic name meaning all available interfaces
PORT = 9050 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by', addr)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
while 1:
data = conn.recv(1024)
if not data: break
if data =="1":
GPIO.output(7,True)
if data =="2":
GPIO.output(7,False)
conn.sendall(data)
os.system(str(data)) //prints data on the terminal
conn.sendall(data)
conn.close()
Upvotes: 0
Views: 5369
Reputation: 6826
Your server uses TCP (SOCK_STREAM) but your client is using UDP - both need to use the same protocol.
Upvotes: 1