When server started by the button, below code gives an SystemNullReferenceException error in function named Runner. One of my friend says that the code is working in his computer, but I can not make it work in my computer. When I started the code by executable file, it falls into not responding state, and I need to close it from task manager by killing the program. From textboxes IP is read as 127.0.0.1 and the port is 13000. I appreciate your guidance.
namespace FakeDataTCP_Server_NameSpace
{
public partial class FakeDataTCP_ServerForm : Form
{
public FakeDataTCP_ServerForm()
{
InitializeComponent();
}
private void buttonServerRUN_Click(object sender, EventArgs e)
{
textBoxSTATUS.Clear();
IPAddress sunucuIP = IPAddress.Parse(comboBoxServerIP.Text);
int sunucuPORT = Int32.Parse(textBoxServerPORT.Text);
textBoxSTATUS.AppendText("sunucu IP: " + sunucuIP.ToString() + "\r\n");
textBoxSTATUS.AppendText("PORT: " + sunucuPORT.ToString() + "\r\n");
Socket sunucuSoketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint sunucuEndIP = new IPEndPoint(sunucuIP, sunucuPORT);
sunucuSoketListener.Bind(sunucuEndIP);
sunucuSoketListener.Listen(100);
textBoxSTATUS.AppendText("Server Started (Listening) \r\n");
Socket istemciSoketi = default(Socket);
FakeDataTCP_ServerForm p = new FakeDataTCP_ServerForm();
Thread sunucuThread = new Thread(new ThreadStart(() => p.Runner(istemciSoketi)));
sunucuThread.Start();
int istemciSAYISI = 0;
while (true)
{
istemciSAYISI++;
istemciSoketi = sunucuSoketListener.Accept();
textBoxSTATUS.AppendText(istemciSAYISI + " clients connected \r\n");
}
}
public void Runner(Socket istci)
{
try
{
while (true)
{
byte[] msg = new byte[1024];
int ebat = istci.Receive(msg);
istci.Send(msg, 0, ebat, SocketFlags.None);
}
}
catch
{
MessageBox.Show("failed");
}
}
}
}