Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

How do you send messages to Async Sockets consecutively - (Python and C#)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the problem comes from your python server.

the accept function blocks your loop until a client asks the server for a connection.

After you connect your client, your server accepts the connection, but the loop after, the server blocks until it can accept another connection. That is why it can't receive multiple data.

I see two ways to encounter this problem :

using select function, so you can either accept or receive instantly

using threads so you can accept and receive as parallel tasks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...