how to ensure about sending multiple serial data from server to client

  • Thread starter Thread starter mahmoodi1072
  • Start date Start date
M

mahmoodi1072

Guest
My program is divided into two parts , a server and a client. when the client connects to the server , At this time, the server should send numbers 1 to 10 to the client. but only one or two first number is send.

this is the server side : in the AcceptClient sub i create a for loop to send numbers 1 to 10 to client.

using System.IO;using System.Net;
using System.Net.Sockets;
class Module1 {
private TcpClient pClient;
private TcpListener Listener;
private Threading.ManualResetEvent mre = new Threading.ManualResetEvent(false);

static void Main()
{
mre.Reset();
Listener = new TcpListener(IPAddress.Any, 6000);
Listener.Start();
Listener.BeginAcceptTcpClient(new AsyncCallback(new System.EventHandler(this.AcceptClient)), Listener);
mre.WaitOne();
}

static void AcceptClient(IAsyncResult ar)
{
pClient = Listener.EndAcceptTcpClient(ar);
Listener.BeginAcceptTcpClient(new AsyncCallback(new System.EventHandler(this.AcceptClient)), Listener);
for (i = 1; (i <= 10); i++)
{
Module1.Send(i);
}

}

public static void Send(string Messsage)
{
StreamWriter sendMessage = new StreamWriter(pClient.GetStream);
sendMessage.WriteLine(Messsage);
sendMessage.Flush();
}
}

and this is client side :

using System.Net;using System.IO;
using System.Net.Sockets;
class Module1
{
private TcpClient client;
private StreamWriter sWriter;
private Threading.ManualResetEvent mre = new Threading.ManualResetEvent(false);

static void Main()
{
mre.Reset();
try
{
client = new TcpClient("localhost", 6000);
client.GetStream.BeginRead(new byte[] {0}, 0, 0, new AsyncCallback(new System.EventHandler(this.read)), client.GetStream);
}
catch (Exception ex)
{
MsgBox(ex.Message);
}

mre.WaitOne();
}

static void read(IAsyncResult ar)
{
try
{
NetworkStream ns = ar.AsyncState;
Int16 l = ns.EndRead(ar);
string msg = (new StreamReader(ns) + ReadLine);
Console.WriteLine(msg);
ns.BeginRead(new byte[] {0}, 0, 0, new AsyncCallback(new System.EventHandler(this.read)), ns);
}
catch (Exception ex)
{
MsgBox(ex.Message);
return;
}

}
}

i want to know how can i correct this code to send all of these numbers?


Continue reading...
 
Back
Top