Im trying to host a text file which can be viewed via a browser. This server will be running on a WindowsCE machine.
Currently when i try and view the file on my windows 7 machine using internet explorer 9 it works fine.
Once i host it on WindowsCE internet explorer just sits there waiting with the working circle rotating.
If i run a telnet on port 80 to the webserver i can see my text file is returned:
HTTP/1.1 200 OK
Content-Type: text/plain
MyTextFileContents
Below is the complete method. No exceptions are logged and it logs "Finished serving log".
static void launchLogWebServer()
{
new Thread(delegate()
{
LogIt(0, "Starting Log Server");
TcpListener server = new TcpListener(IPAddress.Any, 80);
server.Start();
TcpClient newConn;
while (true)
{
try
{
newConn = server.AcceptTcpClient();
LogIt(0, "Connection Made to Log Server");
NetworkStream stream = newConn.GetStream();
StreamReader sr = new StreamReader(stream);
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine("Content-Type: text/plain");
sw.WriteLine();
String line = null;
while ((line = sr.ReadLine()).Length != 0)
{
}
string text = string.Empty;
LogIt(0, "Reading Log File");
try
{
using (StreamReader logReader = new StreamReader("mylog.log"))
{
text = logReader.ReadToEnd();
logReader.Close();
}
}
catch (Exception ex)
{
LogIt(0, ex.ToString());
text = ex.ToString();
}
LogIt(0, "Completed Reading Log File");
sw.WriteLine(text);
sw.Flush();
newConn.Close();
LogIt(0, "Finished serving log");
}
catch(Exception ex)
{
LogIt(0, ex.ToString());
}
}
}).Start();
}
EDIT: I should also note my WindowsCE is running .NET Compact Framework 2.0
You need to add 'Content-Length' parameter in HTTP header.
Related
I am working on my project where I want to make local P2P communication in C# using Sockets and Threads. I am faced with some troubles when I run my code.
I think the problem is inside next while loop
while (true)
{
try
{
TcpListener listener = new TcpListener(IPAddress.Parse(STD_IP), HOST_PORT);
listener.Start();
using (TcpClient client = listener.AcceptTcpClient())
using (NetworkStream nwStream = client.GetStream())
{
Thread listenerThread = new Thread(() =>
{
string message = Receive(nwStream);
updateBoard(message);
});
listenerThread.Start();
string response = stanje;
Send(nwStream, response);
}
}
catch (Exception er)
{
MessageBox.Show("Communication error: \n\n" + er.Message + "\n" + er.StackTrace);
}
}
while (true) is an infinite loop that your posted code has no way of escaping from.
Sometimes, on some machines often and on some seldom, the clients which are using my program are getting a "pipe is being closed" exception. This happens on the NamedPipeServerStream that is on .WaitForConnection(). After that, the Application totally crashes and releases a windows exception. This happens when a NamedPipeClientStream transfers information to the standalone application.
main Funktionality:
I wrote several Tools (Office Toolbars, a Service, a standalone .net Application, and a litle starter exe) that communicate together with NamedPipes.
The Service runs a NamedPipeServerStream that is always open (in state .WaitForConnection();) and the Standalone Application has a NamedPipeServerStream too.
The Toolbars and starter .exe communicate with the service. The Service then with the Standalone Application.
what kind of problems can release the pipe is being closed Exception?
Is it possible that the server sends information to the Standalone Application but closes the stream to early because the Standalone Application is not ready or something? on each NamedPipeClientStream i do a waitforpipedrain if pipeClient.IsConnected befor i close the pipeclient..
thanks for help
edit: Here an Example of a clientstream
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream(".", pipename, PipeDirection.Out))
{
// Wait for a client to connect
try
{
pipeClient.Connect(3000);
// send params to the form
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
sw.WriteLine(sendtext);
}
}
// Catch the IOException that is raised if the pipe is
// broken or disconnected.
catch (Exception e)
{
if (sid != "")
{
connections.Remove(conName);
}
eventLog1.WriteEntry("SendText Fehler 1 " + e.Message);
}
finally
{
if (pipeClient.IsConnected)
{
pipeClient.WaitForPipeDrain();
}
pipeClient.Close();
pipeClient.Dispose();
}
Example of an pipeserver (that runs in a seperad thread)
NamedPipeServerStream pipeServer;
PipeSecurity pipe_security = CreateSystemIoPipeSecurity();
do
string pipename = global::TOfficeCenter.Properties.Settings.Default.pipename;
string aText = "";
pipeServer = new NamedPipeServerStream(pipename, PipeDirection.In, ONE_INSTANCE, PipeTransmissionMode.Byte,
PipeOptions.None, IN_BUF_SIZE, OUT_BUF_SIZE, pipe_security);
try
{
// Verbindung zu TOfficeCenter.exe aufbauen
try
{
IsWaiting = true;
pipeServer.WaitForConnection();
IsWaiting = false;
using (StreamReader sr = new StreamReader(pipeServer))
{
string temp;
while ((temp = sr.ReadLine()) != null)
{
aText = aText + temp;
}
}
try
{
if (aText == "")
{
empfang(null);
}
else
{
if (aText != "KillPipe")
{ // XML empfangen
XmlDocumentTC xml = new XmlDocumentTC();
xml.LoadXml(aText);
empfang(xml);
}
}
}
catch (Exception)
{
empfang(null);
}
}
catch
{...........
}
}
catch (Exception e)
{...........
}
} while (running);
pipeServer.Close();
It's possible that i finally found the problem..
I found out that after this code:
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
sw.WriteLine(sendtext);
}
the pipeClient.IsConnected(); returns directly false, so that it never comes to the WaitForPipeDrain. I Now did it like that and hope that the client does not close the connection before the server is finished with reading..
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
sw.WriteLine(sendtext);
pipeClient.WaitForPipeDrain();
}
Do You Think that could have solved the problem? Since i did it, i never got the error on two test-machines. But the Errors anyway happened seldom..
My use is a bit different, but I'll include the server thread in sum as it's mostly being hacked from the MSDN page at present:
MSDN: How to Use Named Pipes
Not sure if the "WaitForPipeToDrain()" is needed for me, but I took it from your code :)
I think the reset of the pipeServer each time is what cleaned up my IOException.
int threadId = Thread.CurrentThread.ManagedThreadId;
bool sentinel = true;
while (sentinel)
{
NamedPipeServerStream pipeServer =
new NamedPipeServerStream("shapspipe", PipeDirection.InOut, 1);
// Wait for a client to connect
pipeServer.WaitForConnection();
Console.WriteLine("Client connected on thread[{0}].", threadId);
try
{
// Read the request from the client. Once the client has
// written to the pipe its security token will be available.
StreamString ss = new StreamString(pipeServer);
// Verify our identity to the connected client using a
// string that the client anticipates.
ss.WriteString("I am the one true server!");
string message = ss.ReadString();
Console.WriteLine("received from client: " + message);
ss.WriteString("echo from server: " + message);
Console.WriteLine("Received from client: {0} on thread[{1}] as user: {2}.",
message, threadId, pipeServer.GetImpersonationUserName());
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
pipeServer.WaitForPipeDrain();
pipeServer.Close();
}
I've read a couple of posts on SignalR and thought for a fun test project that I could create a web application to poll my onkyo receiver for status and display the results in a browser. For an initial test, I was successfully able to send the current time on the server back to the client by using this code in Application_Start:
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
while (true)
{
clients.addMessage(DateTime.Now.ToString());
Thread.Sleep(1000);
}
});
In the client javascript, i have the following code:
// Proxy created on the fly
var kHub = $.connection.kudzuHub;
// Declare a function on the hub so that the server can invoke it
kHub.addMessage = function (message) {
console.log('message added');
$('#messages').append('<li>' + message + '</li>');
};
// start the connection
$.connection.hub.start();
So all of that works fine. Every second, I get a new list item containing the current server date and time.
Now when I add this code to read data from the Onkyo receiver, it breaks: (still in Application_Start)
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
try
{
while (true)
{
string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);
TcpClient tcpClient = new TcpClient(host, port);
NetworkStream clientSockStream = tcpClient.GetStream();
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
tcpClient.Close();
clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
Thread.Sleep(50);
}
}
catch (SocketException ex)
{
// do something to handle the error
}
});
I set a break point and stepped through the code. It gets to this line and then returns.
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
It never finishes the rest of the code to send the message to the client. What am I doing wrong?
Thanks.
I would make some structural changes to your loop to allow the receiver time to respond, remove the overhead of retrieving the configuration every 50 milliseconds, and cleanup the open network stream:
ThreadPool.QueueUserWorkItem(_ =>
{
dynamic clients = Hub.GetClients<KudzuHub>();
TcpClient tcpClient = null;
NetworkStream clientSockStream = null;
try
{
string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);
while (true)
{
if (tcpClient == null) {
tcpClient = new TcpClient(host, port);
clientSockStream = tcpClient.GetStream();
}
if (clientSockStream.CanRead) {
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
try {
clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
} catch (Exception ex) {
// Add some debug code here to examine the exception that is thrown
}
tcpClient.Close();
// Closing the client does not automatically close the stream
clientSockStream.Close();
tcpClient = null;
clientSockStream = null;
clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
}
Thread.Sleep(50);
}
}
catch (SocketException ex)
{
// do something to handle the error
} finally {
if (tcpClient != null) {
tcpClient.Close();
clientSockStream.Close();
}
}
});
I've been trying to access my GMail account to retrieve the unread emails from my email account. However, I only con perform login... Anything after that doesn't work.
First of all I connect to the server, then send the login command and finally the examine command. The thing is that the responses that are receive refers only to the connection and to the login. After that, it just stops waiting for someting to read from the StreamReader.
try
{
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
//tcpclient.Connect("pop.gmail.com", 995);
tcpclient.Connect("imap.gmail.com", 993);
// This is Secure Stream // opened the connection between client and POP Server
SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("imap.gmail.com");
bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
sw.WriteLine("tag LOGIN user#gmail.com pass");
sw.Flush();
sw.WriteLine("tag2 EXAMINE inbox");
sw.Flush();
sw.WriteLine("tag3 LOGOUT ");
sw.Flush();
string str = string.Empty;
string strTemp = string.Empty;
try
{
while ((strTemp = reader.ReadLine()) != null)
{
Console.WriteLine(strTemp);
// find the . character in line
if (strTemp == ".")
{
//reader.Close();
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
//reader.Close();
break;
}
str += strTemp;
}
}
catch (Exception ex)
{
string s = ex.Message;
}
//reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
I was looking for just this sort of "Hello World" example to get me started. With the help of dkarp's answers, here's my take on Miguel's example:
static void Main( string[] args ) {
try {
TcpClient tcpclient = new TcpClient();
tcpclient.Connect( "imap.gmail.com", 993 );
SslStream sslstream = new SslStream( tcpclient.GetStream() );
sslstream.AuthenticateAsClient( "imap.gmail.com" );
if ( sslstream.IsAuthenticated ) {
StreamWriter sw = new StreamWriter( sslstream );
StreamReader sr = new StreamReader( sslstream );
sw.WriteLine( "tag LOGIN user#gmail.com pass" );
sw.Flush();
ReadResponse( "tag", sr );
sw.WriteLine( "tag2 EXAMINE inbox" );
sw.Flush();
ReadResponse( "tag2", sr );
sw.WriteLine( "tag3 LOGOUT" );
sw.Flush();
ReadResponse( "tag3", sr );
}
}
catch ( Exception ex ) {
Console.WriteLine( ex.Message );
}
}
private static void ReadResponse( string tag, StreamReader sr ) {
string response;
while ( ( response = sr.ReadLine() ) != null ) {
Console.WriteLine( response );
if ( response.StartsWith( tag, StringComparison.Ordinal ) ) {
break;
}
}
}
You might look at using a canned IMAP/SSL library instead - there is one that is still active here.
This alternative is not free.
The basis for one of these has source code that might be helpful since you want to roll your own protocol handler.
Your problem is that you're expecting POP responses from an IMAP server. POP terminates fetched messages with . and responds to other commands with a line beginning with either +OK or -ERR. IMAP doesn't. You're consuming all the server responses and then hanging, waiting for something to match your POP-like response parser. If you examine the returned data, you should see the remainder of the server's responses to your (properly-formatted) requests.
There is a possibility that the server isn't sending back responses to your second and third commands. This could be because you're trying to pipeline three requests; that is, you're sending the requests without waiting for the responses. The server is obliged to allow pipelining while in the SELECTED state, but the protocol doesn't guarantee that you can pipeline commands from the NOT AUTHENTICATED state.
I'm trying to read mails from my live.com account, via the POP3 protocol.
I've found the the server is pop3.live.com and the port if 995.
I'm not planning on using a pre-made library, I'm using NetworkStream and StreamReader/StreamWriter for the job. I need to figure this out. So, any of the answers given here: Reading Email using Pop3 in C# are not usefull.
It's part of a larger program, but I made a small test to see if it works. Eitherway, i'm not getting anything. Here's the code I'm using, which I think should be correct.
EDIT: this code is old, please refer to the second block problem solved.
public Program() {
string temp = "";
using(TcpClient tc = new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"),8000))) {
tc.Connect("pop3.live.com",995);
using(NetworkStream nws = tc.GetStream()) {
using(StreamReader sr = new StreamReader(nws)) {
using(StreamWriter sw = new StreamWriter(nws)) {
sw.WriteLine("USER " + user);
sw.Flush();
sw.WriteLine("PASS " + pass);
sw.Flush();
sw.WriteLine("LIST");
sw.Flush();
while(temp != ".") {
temp += sr.ReadLine();
}
}
}
}
}
Console.WriteLine(temp);
}
Visual Studio debugger constantly falls over tc.Connect("pop3.live.com",995); Which throws an "A socket operation was attempted to an unreachable network 65.55.172.253:995" error.
So, I'm sending from port 8000 on my machine to port 995, the hotmail pop3 port.
And I'm getting nothing, and I'm out of ideas.
Second block: Problem was apparently that I didn't write the quit command.
The Code:
public Program() {
string str = string.Empty;
string strTemp = string.Empty;
using(TcpClient tc = new TcpClient()) {
tc.Connect("pop3.live.com",995);
using(SslStream sl = new SslStream(tc.GetStream())) {
sl.AuthenticateAsClient("pop3.live.com");
using(StreamReader sr = new StreamReader(sl)) {
using(StreamWriter sw = new StreamWriter(sl)) {
sw.WriteLine("USER " + user);
sw.Flush();
sw.WriteLine("PASS " + pass);
sw.Flush();
sw.WriteLine("LIST");
sw.Flush();
sw.WriteLine("QUIT ");
sw.Flush();
while((strTemp = sr.ReadLine()) != null) {
if(strTemp == "." || strTemp.IndexOf("-ERR") != -1) {
break;
}
str += strTemp;
}
}
}
}
}
Console.WriteLine(str);
}
What happens if you view the Network Traffic using Wireshark? Is it sending anything at all?
Edit: I can't connect via telnet to pop3.live.com at that port either. Have you managed to successfully connect via a pop3 email client ever?