tcp/IP

I am lookig for some one that knows how to do multithreding with tcp socket. I have some code below however i am not sure if it is right. If some could tell me if i am on the right track or have some better code please let me know
I am useing thread pool is that right.

class TCPIO

{

private TcpListener client;

int port = 0;

TCPIO()

{

client = new TcpListener(port);

client.Start();

while (true)

{ // start while

while (!client.Pending())

{

Thread.Sleep(1000);

} // end inner while

ConnectionThread newcnnection = new ConnectionThread();

newcnnection.Threadlistener = this.client;

ThreadPool.QueueUserWorkItem(new WaitCallback(newcnnection.handleconnection));

} // End while

} //end outer while

}

class ConnectionThread : StationBaseClass

{

public TcpListener Threadlistener;

private static int connections = 0;

public void handleconnection(object state)

{

int recv;

byte[] data = new byte[1024];

TcpClient client = Threadlistener.AcceptTcpClient();

NetworkStream ns = client.GetStream();

connections++;

string on = "ON";

data = Encoding.ASCII.GetBytes(on);

ns.Write(data, 0, data.Length);

while (true)

{ // start while loop

data = new byte[1024];

recv = ns.Read(data, 0, data.Length);

if (recv == 0)

{ // start of if

break;

} // end of if

ns.Write(data, 0, data.Length);

} //end while loop

ns.Close();

//client.Client();

connections--;

}

}

}



Answer this question

tcp/IP

  • scott t

    hmmm..

    i can pronunce my self a little but not very much .. an expert in tcp/ip.. server and client side..
    if that is what you wanna achive .. you are on a wrong.. way..

    but you could make your "way of programing work"..

    Anyway.. if i was you.. i would :

    1) Study the sync and async methods..
    2) Study what is blocking and unblocking..
    3) Study windows sockets..
    4) Study BeginAccept, BeginRecive, EndAccept, EndRecive methods of socket object
    5) Go to Work!!!

    Hmm That Simple maybe...

    i just found out on the DirectX sdk another way.. on server and client side.. with DirectPlay.. interesting.. they show you how multiplayer games works.. BUT i would recommand the first 4 steps.. then when you really handle them you can pass on to the DirectPlay..

    What i saw in your code.. is that you are using a loop in another thread to watch for connection of a client .. See.. if you will study what i told you.. you will find out that
    BeginAccept method of a socket object.. will "callback" on you when a client connects .. so you wont need loop.. And BeginRecive.. will start monotoring for data send from the client or server.. and when some data arrives a AsynCallBack delegate will "call" your functions..

    Please Study the first 4 Step!! and the i will help you to understand .. and help you to create a reall powerfull server class that can handle as much client as windows has sockets..

    But first things first..



  • KillJones

    those item should take max 1 h


    we can meet on messnger and .. talk there.. coz here is to complicated..


  • MainePaco

    where are you i am online . was sick yesterday
  • WernerP

    hi

    I need this soon. if you are that good when can you help me I will read the four items now

    Cisco


  • tcp/IP