I have a TCP server/client chat program using C#.The server that receives messages from the customer and re-sent to the target.
When client connects to the server the server creates a new Thread for him.
My problem is that the server is to receive only one message from the customer does not receive any new message from him.
When I do debugging I noticed that the server receives the message and hang in the client Thread but not run again.
The server code:
namespace server
{
public partial class Form1 : Form
{
private ArrayList alSockets;
private delegate void UpdateLogCallback(string strMessage);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
textBox1.Text = "my ip is " + IPHost.AddressList[0].ToString();
alSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(listenerThread));
thdListener.Name = "thdListener";
thdListener.Start();
}
public void listenerThread()
{
TcpListener tcpc = new TcpListener(IPAddress.Any,6666);
tcpc.Start();
while (true)
{
Socket HndSoc = tcpc.AcceptSocket();
if (HndSoc.Connected)
{
string str2 = "\n" + HndSoc.RemoteEndPoint.ToString() + " connected";
this.Invoke(new UpdateLogCallback(this.update2),new object[] {str2});
lock (this)
{
alSockets.Add(HndSoc);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Name = "thdHandler";
thdHandler.Start();
}
else if(!HndSoc.Connected)
{
string str2 = "\n" + HndSoc.RemoteEndPoint.ToString() + " desconnected";
this.Invoke(new UpdateLogCallback(this.update2), new object[] { str2 });
}
}
}
public void handlerThread()
{
byte [] rx = new byte [1024];
Socket hanso = (Socket) alSockets[alSockets.Count-1];
NetworkStream ns = new NetworkStream(hanso);
// NetworkStream ns = hanso.
ns.Read(rx,0,rx.Length);
// textBox1.Text = "\n";
// textBox1.Text = Encoding.ASCII.GetString(rx);
string str = Encoding.ASCII.GetString(rx);
// update(str);
this.Invoke(new UpdateLogCallback(this.update), new object[] { str });
// ns.Close();
// hanso = null;
rx = new byte[1024];
ns.Read(rx, 0, rx.Length);
}
And this is client code if needed:
namespace simple_clint
{
public partial class Form1 : Form
{
byte[] tx = new byte[1024];
// IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6666);
TcpClient tcp1 = new TcpClient("127.0.0.1", 6666);
public Form1()
{
InitializeComponent();
}
private void send_Click(object sender, EventArgs e)
{
tx = Encoding.ASCII.GetBytes(textBox1.Text);
try
{
NetworkStream ns = tcp1.GetStream();
ns.Write(tx, 0, tx.Length);
// ns.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Thanks to kajacx. I need to add loop into handlerThread() the new code:
public void handlerThread()
{
byte[] rx = new byte[1024];
Socket hanso = (Socket)alSockets[alSockets.Count - 1];
while (true)
{
NetworkStream ns = new NetworkStream(hanso);
ns.Read(rx, 0, rx.Length);
string str = Encoding.ASCII.GetString(rx);
this.Invoke(new UpdateLogCallback(this.update), new object[] { str });
}
}
and I remove last two lines
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.