c# - Send client side data Queue to server side through Tcp/IP -


i wanna send data client server. there 2 queues. in client side , in server side. want client connected server , send data in client queue server. in server side wanna accept clients , objects , add server side queue

client code :

queue<person> clientqueue;   // client side queue  ipendpoint serverendpoint = new ipendpoint(ipaddress.parse("127.0.0.1"), 15884); var client = new tcpclient();  while(!clientqueue.isempty) {     person personobj = clientqueue.dequeue();      client.connect(serverendpoint);     networkstream clientstream = client.getstream();     binaryformatter bf = new binaryformatter();     bf.serialize(clientstream, personobj);     clientstream.flush(); } 

server side :

queue<person> serverqueue;   // server side queue  ipendpoint ipendpoint = new ipendpoint(ipaddress.parse("127.0.0.1"), 15884); tcplistener tcplistener = new tcplistener(ipendpoint);   while(true) {     tcpclient client = tcplistener.accepttcpclient();     networkstream clientstream = tcpclient.getstream();     binaryformatter bf = new binaryformatter();     person person = (person)bf.deserialize(clientstream);     tcpclient.close();      serverqueue.enqueue(person); } 

i know above code not working. wanna sketch design. can please send me code example. how send client queue server queue..

thanks..

  1. for queue @ both server , client side should use blockingcollection if using .net 4.0, earlier versions use combination of queue , autoresetevent. check this.

  2. at server side should use asynchronous methods or instantiate new thread handle each new client , read data @ thread. like:

    tcpclient client = tcplistener.accepttcpclient(); threadpool.queueuserworkitem(new waithandle(handlecleint), client);  private void handleclient(object clientobject) {     tcpclient client = (tcpclient)clientobject;     networkstream clientstream = client.getstream();     //handle data here } 

edit: stated @ comment:

i don't have idea how change program send entire queue server side

array or queue object:

//at client side: bf.serialize(clientstream, persons);//assume persons queue<person>  //at server side: queue<person> persons = (queue<person>)bf.deserialize(clientstream); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -