By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,327 Members | 2,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy. program crashes with tcplistener

P: 13
Im making an app in, i set up the listener, when it hits the while, it crashes. The code is straight from the ms example, modified a bit for a form instead of command line.

Heres the code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Net.Sockets;
  9. using System.Net;
  10. using System.Windows.Forms;
  12. namespace Server
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         TcpListener server = null;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             Int32 port = 7156;
  25.             IPAddress localAddr = IPAddress.Parse("");
  26.             server = new TcpListener(localAddr, port);
  29.         }
  31.         private void button1_Click(object sender, EventArgs e)
  32.         {
  33.             try
  34.             {
  35.                 server.Start();
  37.               // Buffer for reading data
  38.               Byte[] bytes = new Byte[256];
  39.               String data = null;
  41.               // Enter the listening loop.
  42.               while(true) 
  43.               {
  44.                 listBox1.Items.Add("Waiting for a connection...");
  46.                 // Perform a blocking call to accept requests.
  47.                 // You could also user server.AcceptSocket() here.
  48.                 TcpClient client = server.AcceptTcpClient();
  49.                 listBox1.Items.Add("Connected");
  51.                 data = null;
  53.                 // Get a stream object for reading and writing
  54.                 NetworkStream stream = client.GetStream();
  56.                 int i;
  58.                 // Loop to receive all the data sent by the client.
  59.                 while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
  60.                 {   
  61.                   // Translate data bytes to a ASCII string.
  62.                   data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  63.                   listBox1.Items.Add("Received: " + data);
  65.                   // Process the data sent by the client.
  66.                   data = data.ToUpper();
  68.                   byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  70.                   // Send back a response.
  71.                   stream.Write(msg, 0, msg.Length);
  72.                   listBox1.Items.Add("Sent: " + data);            
  73.                 }
  75.                 // Shutdown and end connection
  76.                 client.Close();
  77.               }
  79.             }
  80.             catch(SocketException ex)
  81.             {
  82.                 listBox1.Items.Add("Error: " + ex);
  83.             }
  84.             finally
  85.             {
  86.                // Stop listening for new clients.
  87.                server.Stop();
  88.             }
  90.         }
  91.     }
  92. }
Mar 9 '09 #1
Share this Question
Share on Google+
7 Replies

Expert 5K+
P: 7,872
Did you set a breakpoint and step through until you found where it crashes?
Mar 9 '09 #2

Expert 2.5K+
P: 3,525
Im making an app in, i set up the listener, when it hits the while, it crashes. The code is straight from the ms example, modified a bit for a form instead of command line.

Heres the code:
And... ? Did you have a question?
The "How to ask a question" read me.

For future posts please include the error. It really helps those trying to help you to have all the information you have. You got an error from Visual Studio that told you the exactly line of the crash and a description of the problem. If you don't share it with the members here it handicaps them.

But... Take a good look at your line 59 (The while statement). Compare it character for character. There is an obvious but common typo.

= means to set one thing equal to another
== is a comparison of one thing to another. "Is equal to"
I my head I have to say "equals" when I type = and say "Is equal" when typing ==. One word, one symbol. Two words, two symbols.
Mar 9 '09 #3

Expert 5K+
P: 7,872
I think that is actually correct syntax tlhintoq. Not how I would do it, but I've seen other people try to use that shortcut.
i is being assigned the number of bytes read, while also being checked to be greater then 0.

without the actual error we can only speculate, but I imagine the lack of checking for null or other checks might be the culpret
Mar 9 '09 #4

P: 13
Wow thanks for the quick replies.
When I click run, the program starts, I click the button, the server starts, from then I can still move the window around, and about 4 seconds in, I cant move the window around, and clicking inside the window causes the program to become unresponsive. Im assuming the while is where it is crashing, although I would have thought this would have worked, as its from MS's official documentation, and have seen others use this or a similar method before, which worked. What would be an alternative to the method i'm currently using?

Mar 9 '09 #5

Expert 5K+
P: 7,872
Ahh, it's your blocking call. You're blocking the gui thread, which means its not getting the windows messages.
Move your code to a seperate thread and you should be ok
Mar 10 '09 #6

P: 13
Ah your a legend Plater. Thanks so much, worked like a charm, although another problem arises, in the form of cross thread calls to form controls, although il see if I can work that out on my own. Thanks everyone :)
Mar 11 '09 #7

Expert 2.5K+
P: 3,525
Try not to access controls directly. Instead raise an event that something happened. And have your UI thread subscribe to the event and update the control. This way you "loosen" the tight connection between actions and UI. By raising an event you could have a dozen listener react to it on their own.
Mar 11 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.