Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Networking problem - client/server

Status
Not open for further replies.

luvcloud

Programmer
Apr 23, 2002
24
0
0
MY
I have a method (OnConnected()) in a class - ServiceClient which will another method (SendToClient()) in another class PlayerMsgHandler.

The code for OnConnected() in ServiceClient is as below:

Code:
 public void OnConnected(object sender, EventArgs e)
  {
  PlayerMsgHandler myplayer = (PlayerMsgHandler) sender;
  
  //get players list and send it the new player
  playersList.Add(myplayer.thePlayer.Username, myplayer.thePlayer);
  msg = getPlayersList();
  myplayer.SendToClient(msg);

  //get gamelist and send to all players
  string temp = gameController.GetGameList();
  myplayer.SendToClient (temp);
     

  //get BePopular list and send to all players
  string bpone = gameController.GetBePopularList("ALL", "");
  myplayer.SendToClient(bpone);

  }
And the code for SendToClient() method in PlayerMsgHandler is as below:

Code:
 public void SendToClient(String s) 
 {
  lock (player.sock)
  {
   try {
    
   Byte[] toClient = Encoding.ASCII.GetBytes(s.ToCharArray());
   
   AsyncCallback sendmsg = new AsyncCallback(SendComplete);
   player.sock.BeginSend(toClient, 0, toClient.Length, SocketFlags.None, 
   sendmsg, player.sock);
    
   } catch (Exception ex) 
    {
     Notification(this, "Error sending msg." + ex);
    }
  }
  
 }

As you can see in OnConnected() method, the SendToClient method is invoked several times. The problem is, the last SendToClient call (which sends the BePopular list) does not get sent at all, which means that the client does not receive any message from this particular call. No exception messages are thrown either.

I think the problem here is the SendToClient call is made too close together, and as such, before a previous SendToClient() method finish executing, another SendToClient() call is made, and as such, the second call does not get executed at all.

Does anyone know how can i solve this problem? Would creating threads be a good idea here? A lot of clients could be connecting at once, and the OnConnected() method could be invoked often, so i'm not sure whether threads would be a good idea as i read that too much threads can reduce performance.

Also how could i synchronize between the threads created (if creating thread is the answer).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top