
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
public class Internet
{
MyForm MyF;
:
:
// Constructor
public Internet(MyForm mf, String Port)
{
MyF = mf;
:
:
|
// Message の受信
public void ReciveMessage()
{
TcpClient client = new TcpClient();
while(Listener!=null)
{
if (Listener.Pending()) //待機中の接続要求があるとき
{
ReciveMsg = string.Empty;
//接続
client = Listener.AcceptTcpClient();
Console.WriteLine("接続されました");
NetworkStream stream = client.GetStream();
Byte[] bytes = new Byte[33];
int i;
//メッセージを受信
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
String data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
ReciveMsg = ReciveMsg + data;
}
MyF.Invalidate();
client.Close();
}
}
}
|
//☆ MyForm Program
public Thread threads; //スレッドの領域
public Internet Inet = null; //Internet Object Class
public String MsgLog;
|
private void Button1_Click(object sender, EventArgs e)
{
Inet = new Internet(this, textBox1.Text);
threads = new Thread(new ThreadStart(Inet.ReciveMessage));
threads.Start();
MsgLog += "StartUP: ";
MsgLog += textBox3.Text;
MsgLog += " / ";
MsgLog += textBox1.Text;
MsgLog += "\r\n";
textBox5.Text = MsgLog;
}
|
private void Button2_Click(object sender, EventArgs e)
{
if (Inet != null)
{
Inet.Listener = null;
threads.Join(); // スレッドの終了を待つ
}
this.Dispose();
}
|
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Inet != null)
{
MsgLog += "受信: ";
MsgLog += Inet.ReciveMsg;
MsgLog += "\r\n";
textBox5.Text = MsgLog;
}
}
|
![]()