c# - problems about getting repeated sockets which i do not intend -


i'm working on messenger program using c#, , have issues.

the server, client has 3 connections(each chatting, filetrans, cardgames).

for first, , second connection, it's working fine.

but problem occurred on third one, handles less amount of packet types compared first 2 sockets.

it's not not receiving packet or not getting connection, it's getting(or sending) more one(which intend send) packets @ time. server log keeps on saying on 1 click, server receives 3~20 same packets , sends them targeted client.

before partial codes third connection, i'll explain how thing suppose work.

the difference between connection1,2 , connection3(which making issue) time when make connection. 1,2 makes it's connection on main form's form_load function, , works fine.

the connection 3 makes connection when load gaming form(not main form). also, first 2 socket's listening thread on main form, , third has it's listening thread on it's own form. that's difference can find. connections, , listening threads same. here codes gaming form.

public void gpacket() //a thread function receiving packets server {     int read = 0;     while (isgametcpclientonline)     {          try         {             read = 0;             read = gamenetstream.read(greceivebuffer, 0, 1024 * 4);              if (read == 0)             {                 isgametcpclientonline = false;                 break;             }         }         catch         {             isgametcpclientonline = false;             gamenetstream = null;         }         packet.packet packet = (packet.packet)packet.packet.desirialize(greceivebuffer);          switch ((int)packet.type)         {              case (int)packettype.gameinit:                 {                     gameinit = (gameinit)packet.packet.desirialize(greceivebuffer);                      //codes handling datas packet...                     break;                 }             case (int)packettype.gamepacket:                 {                     gp = (gamepacket)packet.packet.desirialize(greceivebuffer);                     //codes handling datas packet...                      break;                 }          }     }  }  public void setpacket(bool turn) //makes packet, , sends server.. {     if (turn)         turnsetting(false);     else         turnsetting(true);      gps = new gamepacket();     gps.type = (int)packettype.gamepacket;     gps.isfirstpacket = false;     gps.sym = symbol;     gps.ccapacity = cardcapacity;     gps.currentlist = current_list[0].tag.tostring();     gps.isturn = turn;     gps.opname = opid;     list<string> templist = new list<string>();      foreach (picturebox pb in my_list)     {         templist.add(pb.image.tag.tostring());     }      gps.img_list = templist;       packet.packet.serialize(gps).copyto(this.gsendbuffer, 0);     this.send();     label5.text = symbol + ", " + current_list[0].tag.tostring();  }  public void send() //actually part sends packet through netstream. {      gamenetstream.write(this.gsendbuffer, 0, this.gsendbuffer.length);     gamenetstream.flush();      (int j = 0; j < 1024 * 4; j++)     {         this.gsendbuffer[j] = 0;     }  } 

i don't know why i'm having problem. connection point? or receiving point? or sending point? if establish connection on same place connection1,2(which on main form. if this, should make "gpacket" function running on main form well)?

this looks classic "assume read entire packet", "packet" here mean logical message, not underlying transport packet. example:

read = gamenetstream.read(greceivebuffer, 0, 1024 * 4); ... packet.packet packet = (packet.packet)packet.packet.desirialize(greceivebuffer); 

firstly, strikes me off read wouldn't needed in desirialize, but: makes think read entire packet? have read:

  • one entire packet (only)
  • half of 1 packet
  • one byte
  • three packets
  • the last 2 bytes of 1 packet, 1 entire packet, , first 5 bytes of third packet

tcp stream; read guaranteed give "at least 1 byte , @ {count} bytes, or eof". unusual calls write map calls read. job understand protocol, , decide how data buffer, , how of buffer treat 1 packet vs holding them next packet(s).

see also: how many ways can mess io?, in partuclar "network packets: send not (usually) get".


to fill 4096 byte buffer:

int expected = 4096, offset = 0, read; while(expected != 0 &&     (read = gamenetstream.read(greceivebuffer, offset, expected)) > 0) {     offset += read;     expected -= read; } if(expected != 0) throw new endofstreamexception(); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -