using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.IO;
namespace net
{
public partial class Form1 : Form
{
public Form2 yourchoice;
public Form1()
{
InitializeComponent();
this.Hide();
yourchoice = new Form2();
yourchoice.ShowDialog();
this.Show();
localhost = Class1.host;
remoteconnect = Class1.remote;
if (localhost == true && remoteconnect == false) { ishost(); }
}
private int sent = 0;
private Socket s;
private TcpClient x;
private string[] timer2str;
private int timer2i = 0;
private string[] timer1str;
private int timer1i = 0;
private string previousrichtext="echo";
private bool localhost;
private bool remoteconnect;
private void Form1_Load(object sender, EventArgs e)
{
}
//host server
private void ishost()
{
button1.Enabled = false;
textBox1.Enabled = false;
textBox3.Enabled = false;
MessageBox.Show("local address 127.0.0.2:8001\n wait for remote connection");
IPAddress ipAd = IPAddress.Parse("127.0.0.2");
TcpListener myList = new TcpListener(ipAd, 8001);
listBox1.Items.Add("my local address:" + myList.LocalEndpoint);
myList.Start();
s = myList.AcceptSocket();
listBox1.Items.Add("remote address: " + s.RemoteEndPoint);
textBox2.Text = s.RemoteEndPoint.ToString();
button4.Enabled = true;
timer1.Enabled = true;
}
//remote connect
private void button1_Click(object sender, EventArgs e)
{
if (remoteconnect == true && localhost == false)
{
try
{
x = new TcpClient();
listBox1.Items.Add("connecting to " + textBox1.Text);
x.Connect(textBox1.Text, int.Parse(textBox3.Text));
listBox1.Items.Add("connected");
textBox2.Text = textBox1.Text;
timer2.Enabled=true;
}
catch (Exception)
{
listBox1.Items.Add("server not found");
}
Stream stm = x.GetStream();
try
{
string str = "echo";
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
}
catch (Exception) { }
}
}
//search server
private void button2_Click(object sender, EventArgs e)
{
try
{
IPHostEntry IPHost = Dns.GetHostEntry(textBox4.Text);
listBox1.Items.Add("HostName: " + IPHost.HostName);
string[] aliases = IPHost.Aliases;
listBox1.Items.Add("count of host aliases: " + aliases.Length);
for (int i = 0; i < aliases.Length; i++)
{
listBox1.Items.Add(aliases[i]);
}
IPAddress[] addr = IPHost.AddressList;
listBox1.Items.Add("host IP list: ");
for (int i = 0; i < addr.Length; i++)
{
listBox1.Items.Add(addr[i]);
}
button4.Enabled = true;
}
catch (Exception)
{
listBox1.Items.Add("server not found");
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void button5_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
//host server timer
private void timer1_Tick_2(object sender, EventArgs e)
{
try
{
byte[] b = new byte[1000];
int k = s.Receive(b);
string richtext = "";
for (int i = 0; i < k; i++)
{
richtext += (Convert.ToChar(b[i])).ToString();
}
if (richtext != "echo")
{
if (previousrichtext == "echo")
{
DateTime now = DateTime.Now;
richTextBox1.AppendText(now.ToShortDateString() + " " + now.ToShortTimeString() + " remote"+"\u2028");
}
richTextBox1.AppendText(richtext + "\u2028");
}
previousrichtext = richtext;
}
catch (Exception) { listBox1.Items.Add("timer1 write error"); }
if (sent == 1)
{
richTextBox2.Enabled = false;
button4.Enabled = false;
sent = 0;
try
{
timer1str = richTextBox2.Lines;
if (timer1i < timer1str.Length)
{
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(timer1str[timer1i]+" "));
timer1i++;
sent = 1;
}
}
catch (Exception) { listBox1.Items.Add("timer1 send rtf error"); }
}
if(sent==0)
{
timer1i = 0;
richTextBox2.Enabled = true;
try
{
string str = "echo";
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(str));
}
catch (Exception) { listBox1.Items.Add("timer1 send echo error"); }
button4.Enabled = true;
}
}
//remote timer
private void timer2_Tick(object sender, EventArgs e)
{
Stream stm = x.GetStream();
try
{
byte[] bb = new byte[1000];
int k = stm.Read(bb, 0, 1000);
string richtext = "";
for (int i = 0; i < k; i++)
{
richtext += (Convert.ToChar(bb[i])).ToString();
}
if (richtext != "echo")
{
if (previousrichtext == "echo")
{
DateTime now = DateTime.Now;
richTextBox1.AppendText(now.ToShortDateString() + " " + now.ToShortTimeString() + " remote" + "\u2028");
}
richTextBox1.AppendText(richtext + "\u2028");
}
previousrichtext = richtext;
}
catch (Exception) { listBox1.Items.Add("timer2 write error"); }
if (sent == 1)
{
richTextBox2.Enabled = false;
button4.Enabled = false;
sent = 0;
try
{
timer2str = richTextBox2.Lines;
if (timer2i < timer2str.Length)
{
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(timer2str[timer2i]+" ");
stm.Write(ba, 0, ba.Length);
timer2i++;
sent = 1;
}
}
catch (Exception) { listBox1.Items.Add("timer2 send rtf error"); }
}
if(sent==0)
{
timer2i = 0;
richTextBox2.Enabled = true;
try
{
string str = "echo";
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
}
catch (Exception) { listBox1.Items.Add("timer2 sent echo error"); }
button4.Enabled = true;
}
}
//send message
private void button4_Click(object sender, EventArgs e)
{
sent = 1;
DateTime now = DateTime.Now;
richTextBox1.AppendText(now.ToShortDateString() + " " + now.ToShortTimeString() + " me" + "\u2028");
richTextBox1.Text += richTextBox2.Text;
}
//save
private void button6_Click(object sender, EventArgs e)
{
richTextBox1.SaveFile(@"C:\Users\abc\Desktop\chathistory.rtf", RichTextBoxStreamType.RichText);
}
}
}
----------------------------------------------------------------------------------------------------
//class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace net
{
static class Class1
{
private static Form1 x = new Form1();
public static Form1 mainForm
{
set { x = value; }
get { return x; }
}
private static bool y;
public static bool isremote
{
set { y = value; }
get { return y; }
}
public static bool remote
{
get { return y; }
}
private static bool z;
public static bool ishost
{
set { z = value; }
get { return z; }
}
public static bool host
{
get { return z; }
}
}
}
--------------------------------------------------------------------------------------
form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace net
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1.ishost = true;
Class1.isremote = false;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Class1.isremote = true;
Class1.ishost = false;
this.Close();
}
}
}
--------------------------------------------------------------------------------
//program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace net
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Class1.mainForm);
}
}
}
c# richtextbox
No comments:
Post a Comment