소스 코드가 없으므로 네트워크 케이블을 분리하면 UI가있는 응용 프로그램이 중지되지 않습니다. 라스베리 파이 3에서 다음과 같은 조각 코드를 테스트했습니다. 전경 작업에서 비동기 방법이 권장됩니다. Asynchronous programming을 참조하십시오.
서버 :
TcpListener tcpListener = new TcpListener(IPAddress.Any, 6550);
tcpListener.Start();
byte[] buffer = new byte[1024];
String data = string.Empty;
while (true)
{
var client = await tcpListener.AcceptTcpClientAsync();
var stream = client.GetStream();
int i = 0;
// Loop to receive all the data sent by the client.
do
{
i = await stream.ReadAsync(buffer, 0, buffer.Length);
if(i > 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(buffer, 0, i);
//Display received message
txtRServerContent.Text = data;
}
} while (i > 0);
}
클라이언트 :
TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
await tcpClient.ConnectAsync(IPAddress.Parse("10.168.197.66"), 14400);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = tcpClient.GetStream();
// Buffer to store the response bytes.
byte[] data = new byte[1024];
// String to store the response ASCII representation.
String responseData = String.Empty;
int count = 0;
while (true)
{
// Read the first batch of the TcpServer response bytes.
count = await stream.ReadAsync(data, 0, data.Length);
if (count > 0)
{
responseData = System.Text.Encoding.ASCII.GetString(data, 0, count);
//Dispaly responsed message
txtClientContent.Text = responseData;
}
}
Raspberry PI SE에서 더 좋을 수도 있습니다. – jdv
"Raspberry PI SE"는 무엇을 의미합니까? 나는 그것을 모른다. 리눅스인가? 그렇다면 옵션이 아닙니다. – Trivalik
https://raspberrypi.stackexchange.com/ – jdv