Hope you all doing great.
I wrote this code in C# win.Form but it crashes in line 41 and the will be freezed and not responding.
I put the Whole code in the button click so that when i click the button the server runs and the client could connect to that.
I would be happy if you solve this with your solution.
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace Server_SP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Socket listenerr = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress localAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndpoint = new IPEndPoint(localAddress, 8575);
listenerr.Bind(localEndpoint);
IPAddress ipAddress = IPAddress.Parse("0.0.0.0");
int port = 8575;
TcpListener listener = new TcpListener(ipAddress, port);
listener.Start();
label2.Text = "Serving on Port 8587";
while (true)
{
label1.Text = " Waiting for connection...";
TcpClient cliend = listener.AcceptTcpClient();
label1.Text = "Client Accepted";
NetworkStream stream = cliend.GetStream();
StreamReader sr = new StreamReader(cliend.GetStream());
StreamWriter sw = new StreamWriter(cliend.GetStream());
try
{
byte[] buffer = new byte[1024];
stream.Read(buffer, 0, buffer.Length);
int count = 0;
//////how many bytes
foreach (byte b in buffer)
{
if (b!=0)
{
count++;
}
}
string reced = Encoding.UTF8.GetString(buffer, 0, count);
label3.Text = "Request Reced: " + reced;
sw.WriteLine("Hiiii.I am serverrr");
sw.Flush();
}
catch (Exception errorr)
{
label1.Text = "Errorrrr";
sw.WriteLine(errorr.ToString());
}
}
}
}
}```
i removed the while loop and also replaced the line 41 whith the code below but didnt help.
``` TcpClient cliend = listenerr.AcceptTcpClient(); ```
Actually I did this job in console properly and it works but when i tried to port that code into windows application i faced these issues.