Click here to Skip to main content
15,889,753 members
Home / Discussions / C#
   

C#

 
AnswerRe: FxCop Performance Warning CA1822 Pin
Mirko198017-Apr-11 23:49
Mirko198017-Apr-11 23:49 
AnswerRe: FxCop Performance Warning CA1822 Pin
#realJSOP18-Apr-11 1:42
mve#realJSOP18-Apr-11 1:42 
GeneralRe: FxCop Performance Warning CA1822 Pin
SledgeHammer0118-Apr-11 7:54
SledgeHammer0118-Apr-11 7:54 
QuestionGlobal Keyboard hook Pin
Xelalem17-Apr-11 23:04
Xelalem17-Apr-11 23:04 
AnswerRe: Global Keyboard hook Pin
lukeer18-Apr-11 2:20
lukeer18-Apr-11 2:20 
QuestionC# Exchange Mailstore Location Pin
Jinxx198317-Apr-11 15:37
Jinxx198317-Apr-11 15:37 
QuestionRe: C# Exchange Mailstore Location Pin
Jinxx198326-Apr-11 12:41
Jinxx198326-Apr-11 12:41 
QuestionWebserver works in a console app but not windows forms? Pin
venomation17-Apr-11 13:21
venomation17-Apr-11 13:21 
I have the following class:
public class WebServer
    {
        private readonly TcpListener _listener;
        private readonly Thread _listenerThread;

        public WebServer()
        {
            _listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
            _listenerThread = new Thread(ListenForClients);
        }

        public void StartListening()
        {
            _listenerThread.Start();
        }

        private void ListenForClients()
        {
            _listener.Start();

            while (true)
            {
                TcpClient client = _listener.AcceptTcpClient();

                var clientThread = new Thread(HandleClient);
                clientThread.Start(client);
            }
            _listener.Stop();
        }


        private void HandleClient(object client)
        {
            var tcpClient = client as TcpClient;
            var clientStream = tcpClient.GetStream();
            var encoder = new ASCIIEncoding();
            var message = new byte[4096];

            while (true)
            {
                int bytesRead = 0;
                string fileToGrab;
                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                    string msg = encoder.GetString(message);
                    Console.WriteLine("Client {0} connected", tcpClient.Client.LocalEndPoint);
                    Console.WriteLine(msg);

                    string[] parts = msg.Split(' ');

                    fileToGrab = parts[1];

                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead != 0)
                {
                    if (fileToGrab == "/") fileToGrab = @"/index.html";
                    else if (!File.Exists(Environment.CurrentDirectory + fileToGrab))
                    {
                        fileToGrab = @"/error.html";
                    }

                    byte[] buffer = File.ReadAllBytes(Environment.CurrentDirectory + fileToGrab);


                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();

                    string serverIp = "From server " + _listener.LocalEndpoint;
                    clientStream.Write(encoder.GetBytes(serverIp), 0, serverIp.Length);
                    clientStream.Flush();
     
                    break;

                }
            }

            Console.WriteLine("Client {0} closed",tcpClient.Client.LocalEndPoint);
            tcpClient.Close();
        }
    }


That is used to host a small web-server for the browser to connect to and receive data.

It works fine in a console application:

static void Main()
       {
           WebServer server = new WebServer();
           server.StartListening();
       }


But in a windows forms app:

public partial class Form1 : Form
    {
        private readonly WebServer _server;

        public Form1(WebServer server)
        {
            _server = server;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _server.StartListening();
 
        }
    }


...When the button is pressed to turn it on, when connecting it only receives the client data, but the client does not receive the server data.

I dont know why, something to do with "[STAThread]" ?

Thanks Big Grin | :-D
AnswerRe: Webserver works in a console app but not windows forms? Pin
BobJanova17-Apr-11 23:53
BobJanova17-Apr-11 23:53 
QuestionWindows Notify icon Pin
Xelalem17-Apr-11 8:01
Xelalem17-Apr-11 8:01 
Questionapplication client/serveur sous c# Pin
zoubou17-Apr-11 7:15
zoubou17-Apr-11 7:15 
AnswerRe: application client/serveur sous c# Pin
RobCroll17-Apr-11 7:31
RobCroll17-Apr-11 7:31 
AnswerRe: application client/serveur sous c# Pin
Pete O'Hanlon17-Apr-11 9:34
mvePete O'Hanlon17-Apr-11 9:34 
AnswerRe: application client/serveur sous c# Pin
Ravi Sant17-Apr-11 23:50
Ravi Sant17-Apr-11 23:50 
QuestionVisual Studio 2010 Photo Gallery website or project Pin
georgehowell17-Apr-11 3:35
georgehowell17-Apr-11 3:35 
AnswerRe: Visual Studio 2010 Photo Gallery website or project Pin
Not Active17-Apr-11 4:30
mentorNot Active17-Apr-11 4:30 
AnswerRe: Visual Studio 2010 Photo Gallery website or project Pin
Prasanta_Prince18-Apr-11 0:18
Prasanta_Prince18-Apr-11 0:18 
Questionadding a different references depending on the system.(32 bit or 64 bit) Pin
prasadbuddhika16-Apr-11 20:38
prasadbuddhika16-Apr-11 20:38 
AnswerRe: adding a different references depending on the system.(32 bit or 64 bit) [modified] Pin
Luc Pattyn17-Apr-11 2:24
sitebuilderLuc Pattyn17-Apr-11 2:24 
QuestionDatabase help Pin
Dave McCool16-Apr-11 12:23
Dave McCool16-Apr-11 12:23 
AnswerRe: Database help Pin
RobCroll17-Apr-11 4:22
RobCroll17-Apr-11 4:22 
QuestionHelp loading form, progress bar and button Pin
Dave McCool16-Apr-11 12:15
Dave McCool16-Apr-11 12:15 
AnswerRe: Help loading form, progress bar and button Pin
Luc Pattyn16-Apr-11 12:29
sitebuilderLuc Pattyn16-Apr-11 12:29 
GeneralRe: Help loading form, progress bar and button Pin
Dave McCool18-Apr-11 1:14
Dave McCool18-Apr-11 1:14 
QuestionScrolling with mouse in combo box too fast Pin
RobScripta16-Apr-11 2:37
professionalRobScripta16-Apr-11 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.