0

really need you help .

my project is to connect two PC's via cable and use the tcp socket send the string in the client text box form to the server. the problem is thati can just send one string then the connection will close.

client code(c#):of course in the try catch way :

       String str = textBox2.Text;
       Stream stm = tcpclnt.GetStream();

        ASCIIEncoding asen = new ASCIIEncoding();
    byte[] ba = asen.GetBytes(str);
 textBox1.Text="Sending...";

 stm.Write(ba,0,ba.Length);

  byte[] bb = new byte[100];
  int k = stm.Read(bb,0,100);

 for (int i = 0;i < k; i++)
 {
  Console.Write(Convert.ToChar(bb[i]));
  }


    tcpclnt.Close();

 //////////////

the server code :also in the try catch way :

   int k = 0;
   byte[] b = new byte[100];
   for (; ; )
   {
       for (int i = 0; i < 100000; i++)
       { }
           k = s.Receive(b);
       MessageBox.Show(k + "");
       textBox1.Text = "Recieved...";

       for (int i = 0; i < k; i++)
       {
           textBox2.Text = textBox2.Text + Convert.ToChar(b[i]);
       }
       MessageBox.Show(k + "");
       ASCIIEncoding asen = new ASCIIEncoding();
       s.Send(asen.GetBytes("Automatic message:" + "String received by server!"));
       textBox1.Text = "\n Automatic message sent!";


       MessageBox.Show(k + "");
     s.Close();
       }

my question is : can i make a loop in the server to send not just one string ,i need to send many string without close the connection .?

note : the client and server every one will be executed after the button in each form pressed.

note : the connection on some port will be establish and succeed in the form load .

2
  • Yes, you can make a loop and send as much as you want. You don't need to close the socket until you're done. Commented Nov 20, 2013 at 5:48
  • i don't know how , i make a loop in the server , but the k=s.recieve(b) will stuck it . Commented Nov 20, 2013 at 5:54

1 Answer 1

1

For a start, move the close out of the for loop

   int k = 0;
   byte[] b = new byte[100];
   for (; ; )
   {
       for (int i = 0; i < 100000; i++)
       { }
       k = s.Receive(b);
       MessageBox.Show(k + "");
       textBox1.Text = "Recieved...";

       for (int i = 0; i < k; i++)
       {
           textBox2.Text = textBox2.Text + Convert.ToChar(b[i]);
       }
       MessageBox.Show(k + "");
       ASCIIEncoding asen = new ASCIIEncoding();
       s.Send(asen.GetBytes("Automatic message:" + "String received by server!"));
       textBox1.Text = "\n Automatic message sent!";


       MessageBox.Show(k + "");
     }
     s.Close();
Sign up to request clarification or add additional context in comments.

6 Comments

i did it 1 million time , the same . look what is the message that back to the client from the server : | An established connection was aborted by the software in your host machine
Maybe you can show us the whole code, as the client and server both close the socket after send/receiving once
the problem in the client side < the tcpclnt.close () will abort the connection . but how to fix it . if i move it to close connection button , the server will accept onceafter thebutton is pressed . so how to deal with it
@Khalid jarrah, why loop for 100,000 times ? Why not an endless loop ? There are posts on SO with good code for an async sockets; event based is also a good way to go. Also, is your code in a button's click event handler ? If so, move the stm to the form's scope not the button scope; and move the close code to a separate "close connection" button.
@Khalid jarrah. Why do you mention a button? I hope you don't run it all under a button as this kind of code requires it's own thread and should not run in the UI thread. Do not close any sockets until you are finished.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.