I am probably making a rookie mistake but I've just started experimenting with local TCP communication between my PC and Raspberry Pi. I have the server running on my Raspberry Pi (in python) and the client on my PC (written in C#) has the ability to connect to the RPi, send one set of data correctly and then not send anymore unless a new connection is made. I am just trying to send the number 2 across the connection multiple times (e.g. I press the connect button, then I can press the send button multiple times and the server will receive the data multiple times. Any help is appreciated.
Client code (C#):
public ClientForm()
{
ipAddress = IPAddress.Parse("192.168.0.98");
port = 3333;
InitializeComponent();
}
private void connectCallback(IAsyncResult AR)
{
try
{
clientSocket.EndConnect(AR);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.BeginConnect(new IPEndPoint(ipAddress, port), new AsyncCallback(this.connectCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
byte[] buffer = { 2 };
clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SendCallback(IAsyncResult AR)
{
clientSocket.EndSend(AR);
}
Server Code (Python 3):
import socket
port = 3333
ipAddress = '192.168.0.98'
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind((ipAddress, port))
while True:
serverSocket.listen(0)
clientsocket, address = serverSocket.accept()
received = int.from_bytes(clientsocket.recv(1), 'big')
print(received)