|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C# web server
Hi,
i'm trying to make a simple webserver using C#.net, but am havin few problems. i can get request, but canrt send back to the browser. the browser looks for response; when i manually close the app, the browser immidiatley stops. the log box show the line "Shud have sent", but nothing gets sent. it appears that connection.Close() is not working either. sorry bout the bulk of code, but i feel that the best way is to get all mistakes that ive probably made shown... privatevoid doListen() { try { //listen for connections thread listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 22222); listener.Start(); do { //assign new tcpclient socket for the connection liason connection = new TcpClient(); connection = listener.AcceptTcpClient(); //a connection has been made log.AppendText("Connection made...\r\n"); //pas the data to the getData function doData(connection); }while(true); } catch { } } privatevoid MainForm_Load(object sender, System.EventArgs e) { //new instance of the listener thread when the app starts //assign the thread to DoListen method listenerThread = new Thread(new ThreadStart(doListen)); listenerThread.Start(); } privatevoid MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { listener.Stop(); } /*GET THE ENTIRE REQUEST*/ privatevoid doData(TcpClient connection) { //extract the stream data NetworkStream stream = connection.GetStream(); //get the size of the buffer byte[] bufferIn = newbyte[connection.ReceiveBufferSize]; //now pass datastream into byte-buffer stream.Read (bufferIn, 0, (int)connection.ReceiveBufferSize); //convert into string request = Encoding.ASCII.GetString(bufferIn); //make a log log.AppendText(request + "\r\n"); Byte[] sendBytes = Encoding.ASCII.GetBytes("<html><body>HELLO WORLD</body></html>"); stream.Write (sendBytes, 0, sendBytes.Length); log.AppendText("Shud have sent" + sendBytes.Length.ToString() + " bytes\r\n"); connection.Close(); } many thanks in advance Si |
|
#2
|
|||
|
|||
|
you need to add
stream.Close() before connection.Close(); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > C# web server |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|