public Socket soc;
public Socket acc;
public int port = 9000;
public IPAddress ip;
public Thread rec;
public string name="";
public string GetIp()
{
string hostname = Dns.GetHostName();
IPHostEntry ipentry = Dns.GetHostEntry(hostname);
IPAddress[] addr = ipentry.AddressList;
return addr[addr.Length - 1].ToString();
}
public void RecV()
{
while (true)
{
Thread.Sleep(500);
byte[] buffer = new byte[300];
int rece = acc.Receive(buffer, 0, buffer.Length, 0);
Array.Resize(ref buffer, rece);
richTextBox1.Text= (Encoding.Default.GetString(buffer)).ToString();
}
}
public void MainFunc()
{
rec = new Thread(RecV);
string prt = "";
name = textBox2.Text;
prt = textBox1.Text;
richTextBox3.Text = richTextBox3.Text + "\nConnected!";
try
{
port = Convert.ToInt32(prt);
}
catch
{
port = 9000;
}
ip = IPAddress.Parse(GetIp());
//soc.Shutdown(SocketShutdown.Both);
//soc.Close();
//acc.Shutdown(SocketShutdown.Both);
//acc.Close();
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Bind(new IPEndPoint(ip, port));
soc.Listen(0);
MessageBox.Show("Test");
acc = soc.Accept(); (The Problem is that next message box having text "Test1" is not working...)
MessageBox.Show("Test1");
rec.Start();
while (true)
{
byte[] sdata = Encoding.Default.GetBytes("<" + name + ">" + richTextBox2.Text);
acc.Send(sdata, 0, sdata.Length, 0);
}
}
private void button3_Click(object sender, EventArgs e)
{
MainFunc();
}