まずはラズパイソース(これはまだWin7でしか実験していない)
from __future__ import print_function
import socket
from contextlib import closing
import random
def get_temp():
return random.randint(25, 70)
def main():
host = '127.0.0.1'
port = 4000
backlog = 10
bufsize = 4096
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with closing(sock):
sock.bind((host, port))
sock.listen(backlog)
while True:
conn, address = sock.accept()
with closing(conn):
msg = conn.recv(bufsize)
print(msg[0])
if msg[0] == 97:# char is 'a'
sys.exit(0)
tmp = get_temp()
stmp = str(tmp)
moji = stmp.encode('utf-8')
conn.send(moji)
print( tmp )
return
if __name__ == '__main__':
main()
●c#側
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace client
{
public partial class Form1 : Form
{
string sendMsg = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
///
/// </summary>
private void send()
{
//サーバーのIPアドレス(または、ホスト名)とポート番号
string ipOrHost = "127.0.0.1";
//string ipOrHost = "localhost";
int port = 4000;
System.Net.Sockets.TcpClient tcp;
//TcpClientを作成し、サーバーと接続する
try
{
tcp = new System.Net.Sockets.TcpClient(ipOrHost, port);
}
catch
{
MessageBox.Show("Connection Error Occure!");
return;
}
//NetworkStreamを取得する
System.Net.Sockets.NetworkStream ns = tcp.GetStream();
//読み取り、書き込みのタイムアウトを10秒にする
//デフォルトはInfiniteで、タイムアウトしない
//(.NET Framework 2.0以上が必要)
ns.ReadTimeout = 10000;
ns.WriteTimeout = 10000;
//サーバーにデータを送信する
//文字列をByte型配列に変換
System.Text.Encoding enc = System.Text.Encoding.UTF8;
byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
//データを送信する
ns.Write(sendBytes, 0, sendBytes.Length);
//textBox1.Text += sendMsg;
//サーバーから送られたデータを受信する
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] resBytes = new byte[256];
int resSize = 0;
do
{
//データの一部を受信する
resSize = ns.Read(resBytes, 0, resBytes.Length);
//Readが0を返した時はサーバーが切断したと判断
if (resSize == 0)
{
//textBox1.Text +="close server";
break;
}
//受信したデータを蓄積する
ms.Write(resBytes, 0, resSize);
//まだ読み取れるデータがあるか、データの最後が\nでない時は 受信を続ける
} while (ns.DataAvailable || resBytes[resSize - 1] != '\n');
//受信したデータを文字列に変換
string resMsg = enc.GetString(ms.GetBuffer(), 0, (int)ms.Length);
ms.Close();
//末尾の\nを削除
resMsg = resMsg.TrimEnd('\n');
textBox1.Text += "\r\n" + resMsg;
try
{
if (int.Parse(resMsg) > 40)
{
textBox1.Text += " 40℃以上";
}
}
catch
{
MessageBox.Show("Server is down");
}
//閉じる
ns.Close();
tcp.Close();
//textBox1.Text += "\r\n切断しました。";
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
sendMsg = textBox2.Text;
send();
//for (i = 0; i < 4; i++)
//{
// Thread.Sleep(200);
// Application.DoEvents();
//}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
textBox1.Text = string.Empty;
}
}
}
No tags for this post.

