(how to receive data from earlier connections made by clients?

I am creating a multi-thread client/server program with c# in which the server receives requests for download from clients and send the file immediately to that client. The problem is after starting server, I open a client app and send a name of a file to server so that the server take that and sends the file to me.It works fine for the last client requesting but when I send the request from earlier clients, the server won't get the request. For example I open the first client app and I send a request by it and I get the request on the server side and show the data in a MessageBox and it's shown right. This procedure goes well for the second client but when I go back to the first client and send request from it, MessageBox in server shows nothing while it works fine for second client connected.

Here is the code: Server Side code

private void StartListening_Click(object sender, EventArgs e)     {         //MessageBox.Show();         if (root == "")         {             MessageBox.Show("You must choose a root folder to show a list to your users");             return;         }         //IPAddress IPAddr = IPAddress.Parse("127.0.0.1");         int Port;         if (!int.TryParse(tbPort.Text, out Port))             Port = 21000;         /*if (!IPAddress.TryParse(tbIPAddress.Text, out IPAddr))         {             MessageBox.Show("IP is Invalid");             return;         }*/         listener = new TcpListener(IPAddress.Any, Port);         listener.Start();         label3.Text += "Started Up";         listener.BeginAcceptTcpClient(onAcceptComplete, listener);     }      private void onAcceptComplete(IAsyncResult iar)     {         TcpListener listener = (TcpListener) iar.AsyncState;         TcpClient TCPC;         //EndAccept method returns a TCPclient         try         {             TCPC = listener.EndAcceptTcpClient(iar);             //sendL();             IPEndPoint IpEndPT = TCPC.Client.RemoteEndPoint as IPEndPoint;              //MessageBox.Show("client connected with IpEndPoint:"+IpEndPT);             ClientsInfo.Add(TCPC.Client,IpEndPT);             lbxServer.Items.Add(IpEndPT);             sendL(TCPC);             //sendFile();             listener.BeginAcceptTcpClient(onAcceptComplete, listener);             buffer = new byte[512];             //TCPC.Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, onReadComplete, TCPC);             //listener.BeginAcceptTcpClient(onAcceptComplete, listener);             TCPC.GetStream().BeginRead(buffer, 0, buffer.Length, onReadComplete, TCPC);             //buffer = new byte[512];         }         catch(Exception ex)         {             MessageBox.Show(ex.Message);             return;         }        }      private void onReadComplete(IAsyncResult iar)     {         int ReadBytesNumber;         TcpClient client;         string data;          try         {             client = (TcpClient)iar.AsyncState;             IPEndPoint IPEPT = client.Client.RemoteEndPoint as IPEndPoint;             //MessageBox.Show(IPEPT.ToString()+"  requested");             ReadBytesNumber = client.GetStream().EndRead(iar);             //ReadBytesNumber = client.Client.EndReceive(iar);             MessageBox.Show("bytes in onreadcomplete  " + ReadBytesNumber);             //MessageBox.Show("buffer size   ="+buffer.Length);             if (ReadBytesNumber == 0)             {                 //when no bytes are arrived from the otherside we consider it as a disconnection                 MessageBox.Show("Client disconnected");                 return;             }             //getting request from buffer             //data = Encoding.ASCII.GetString(buffer, 0, buffer.Length);             //MessageBox.Show("data = " + data);             data = Encoding.UTF8.GetString(buffer, 0, ReadBytesNumber);             MessageBox.Show("data = " + data);             //assigning request to dictionary of requests             bool existence = false;             foreach (KeyValuePair<IPEndPoint, string> req in requests.ToList())             {                 if (IPEPT == req.Key)                 {                     existence = true;                     requests[IPEPT] = data;                 }             }             if (existence == false)                 requests.Add(IPEPT, data);             /*foreach (KeyValuePair<string,IPEndPoint> Idic in requests)             {                 MessageBox.Show(Idic.Key + Idic.Value.ToString());             }*/             FileToSend = data;             //MessageBox.Show("FileToSend: "+data);             tbOutPut.Text += data + "\r\n";             sendFile(IPEPT, client);             //clearing buffer to get new value             buffer = new byte[512];             //calling read method to be continously in transferring data with it             client.GetStream().BeginRead(buffer, 0, buffer.Length, onReadComplete, client);             //sendFile(IPEPT);         }         catch(Exception ex)         {             MessageBox.Show(ex.Message);         }     } 

and here is the Client Side

private void Send_Click(object sender, EventArgs e)     {         byte[] bufferToSend;         if (string.IsNullOrEmpty(lbxDL.SelectedItem.ToString()))         {             MessageBox.Show("You must choose a file to request first");             return;         }         fileNameReceipt = false;          data = lbxDL.SelectedItem.ToString();          try         {             bufferToSend = Encoding.UTF8.GetBytes(data);             if (client != null)             {                 if (client.Client.Connected)                 {                     MessageBox.Show(data+"    "+bufferToSend.Length);                     //client.Client.BeginSend(bufferToSend, 0, bufferToSend.Length, SocketFlags.None, onCompleteSend, client);                     client.GetStream().BeginWrite(bufferToSend, 0, bufferToSend.Length, onCompleteSend, client);                 }             }         }         catch (Exception ex)         {             MessageBox.Show(ex.Message);         }     }      private void onCompleteSend(IAsyncResult iar)     {          try         {             client = (TcpClient)iar.AsyncState;             //client.Client.EndSend(iar);             client.GetStream().EndWrite(iar);         }         catch (Exception ex)         {             MessageBox.Show(ex.Message);         }     } 

I guess there is a problem with connections of clients but I don't know how to solve it or maybe it's something else. In the server side method onReadComplete, in this part of code shown below, I'm showing the data which is arrived by the server inside a messagebox

client = (TcpClient)iar.AsyncState;         IPEndPoint IPEPT = client.Client.RemoteEndPoint as IPEndPoint;         //MessageBox.Show(IPEPT.ToString()+"  requested");         ReadBytesNumber = client.GetStream().EndRead(iar);         //ReadBytesNumber = client.Client.EndReceive(iar);         MessageBox.Show("bytes in onreadcomplete  " + ReadBytesNumber);         //MessageBox.Show("buffer size   ="+buffer.Length);         if (ReadBytesNumber == 0)         {             //when no bytes are arrived from the otherside we consider it as a disconnection             MessageBox.Show("Client disconnected");             return;         }         //getting request from buffer         //data = Encoding.ASCII.GetString(buffer, 0, buffer.Length);         //MessageBox.Show("data = " + data);         data = Encoding.UTF8.GetString(buffer, 0, ReadBytesNumber);         MessageBox.Show("data = " + data); 

By the way, sorry for my bad English and junk comment codes:)

Replay

Category: c# Time: 2016-07-31 Views: 13

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 3.272 (s). 13 q(s)