.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2004, 08:07 PM
howzat117 howzat117 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 howzat117 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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...


private
void 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





Reply With Quote
  #2  
Old December 28th, 2004, 02:13 PM
beater beater is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 beater User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
you need to add
stream.Close()
before
connection.Close();

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > C# web server


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT