Results 1 to 2 of 2

Thread: Sending information to clients through two ports on one server-pls guide

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Posts
    97

    Sending information to clients through two ports on one server-pls guide

    Hello Everybody:
    I have developed a multithreaded client server application. The server has two listener with two ports (8000 and 8001).. When the server loads the two ports are listening…The client connects through port 8000 and sends message to the server through port 8000. The thread through which the client connects is identified by using Thread.GetHashCode(). Now I want to sent the same message to another client through port 8001. That thread is also identified by using the Thread.GetHashCode(). When the client at port 8001 receives the message , it has to sent the same message to the server through the same thread. After the server receives the message it has to sent back to the same client at port 8000 through the same thread identified by using the Thread.GetHashCode()…
    My problem is how do I carry out the entire process sending the information through two ports, through the server.to the same two clients two and fro?


    Should I send the data Asynchronously or synchronously….

    I will really appreciate it for any help and guidance….

    I have attached the code for your reference…

    I will really appreciate it for any help and guidance…. And comments.


    Thanks,,,

    Rahil


    public MultiThreadXml()
    {
    try
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    InitializeListView();



    tcpLsn1= new TcpListener(System.Net.IPAddress.Parse(ipAddress),8000);// start the server
    tcpLsn1.Start();
    statusBar2.Text="Listen at:" + tcpLsn1.LocalEndpoint.ToString();

    for( int NumOfThds=1; NumOfThds <=100; NumOfThds++)// For loop for 100 threads
    {
    tcpThd1=new Thread(new ThreadStart(ClientProcess));
    tcpThd1.Start();

    }


    tcpLsn=new TcpListener(System.Net.IPAddress.Parse(ipAddress),8001);// start the server
    tcpLsn.Start();
    statusBar1.Text = "Listen at: " + tcpLsn.LocalEndpoint.ToString();

    for( int NoOfThds=1; NoOfThds <=10; NoOfThds++)// For loop for 10 threads
    {
    tcpThd=new Thread(new ThreadStart(ServerProcess));
    tcpThd.Start();

    }


    }

    catch (SocketException socketEx)

    {
    MessageBox.Show(socketEx.Message);
    }

    }

    public void ServerProcess()
    {


    try
    {


    {
    Byte[] bytes = new Byte[5000]; //Buffer for reading data
    //Buffer for reading data
    String data = null;
    // Enter the listening loop.
    while (true)
    {




    TcpClient client = tcpLsn.AcceptTcpClient();
    MessageBox.Show("Connected to the client");


    data = null;
    // Get a stream object for reading and writing
    NetworkStream stream = client.GetStream();
    Int32 i;
    // Loop to receive all the ata sent by the client
    while ((i=stream.Read(bytes, 0,bytes.Length))!=0)
    {
    //Translate data bytes to a ASCII string.


    data=System.Text.Encoding.ASCII.GetString(bytes,0,i);



    txtDataRx.Text= data + txtDataRx.Text;





    }

    }

    }




    }




    catch (SocketException e)
    {

    MessageBox.Show("SocketException");

    }

    if ( tcpThd.IsAlive )
    {
    tcpThd.Abort();
    }


    }

    public void ClientProcess()//

    {


    try
    {

    Byte[] bytes = new Byte[5000]; //Buffer for reading data
    String data = null;

    while (true)
    {
    TcpClient client1 = tcpLsn1.AcceptTcpClient();
    MessageBox.Show("Connected to the client");
    data = null;
    NetworkStream stream = client1.GetStream();
    Int32 i;
    while ((i=stream.Read(bytes, 0,bytes.Length))!=0)
    {


    data=System.Text.Encoding.ASCII.GetString(bytes,0,i);


    txtDataRx.Text= data + txtDataRx.Text;

    }


    }




    }



    catch (SocketException e)
    {

    MessageBox.Show("SocketException");

    }

    if(tcpThd.IsAlive)
    {
    tcpThd.Abort();
    }
    }

  2. #2
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    hmm,

    first, maybe you could give us some more background on what exactly you're trying to do here. What is the goal of your program?

    What is the purpose of using 2 connections like you are doing? Wouldn't it be easier just to have all clients connect to the server on port 8000 and let the server handle the messaging?

    Once you give us a bit more information as to what you're doing, maybe we can make some recommendations... It's just that from what I can gather about your program design, there may be a much easier way to design what you're trying to do...

    As for the question, async versus nonasync, well it's almost always going to be a better idea to make your program non-blocking whenever possible or whenever you are even faced with the decision...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width