Welcome Guest Search | Active Topics | Members | Log In | Register

build 0.8.307 Options
admin
Posted: Monday, October 13, 2008 4:26:08 PM
Rank: Administration
Groups: Administration

Joined: 2/15/2008
Posts: 130
Points: 835
This build contains a fix for client API

Example:
This code sample is based on the code provided by rajaguru731 member.
It connects to a server, sends request and receives response.

Initially it sends correct HTTP request in ConnectAsync (using optimized ConnectEx WinSock2 function) and receives HTML page.
The second request is invalid and the server responses accordingly.

Code:

using System;
using System.Text;
using XF;

namespace Client
{
    class Program
    {
        private const string request =
@"GET /?sample HTTP/1.1
Host: kodart.com

";
        static void Main()
        {
            Network.ConnectAsync("kodart.com", 80, Encoding.Default.GetBytes(request), OnConnect);
            Console.ReadKey();
        }

        private static void OnConnect(IConnection connection)
        {
            connection.WriteAsync(Encoding.Default.GetBytes("wrong verb example\r\n\r\n"));
            connection.ReadAsync(OnReadComplete);
        }

        private static void OnReadComplete(IConnection sender, IMemoryBuffer buffer, int bytes)
        {
            if (bytes == 0) return;
            Console.WriteLine(Encoding.ASCII.GetString(buffer.Data, buffer.Offset, bytes));
        }
    }
}


File Attachment(s):
XF.Server.dll (195kb) downloaded 59 time(s).


rajaguru731
Posted: Tuesday, October 14, 2008 4:37:15 AM
Rank: Member
Groups: Member

Joined: 8/25/2008
Posts: 9
Points: 33
Location: india
Can you please provide some samples to know the steps to check, is the server running in client side?
admin
Posted: Tuesday, October 14, 2008 4:41:53 AM
Rank: Administration
Groups: Administration

Joined: 2/15/2008
Posts: 130
Points: 835
Quote:
Can you please provide some samples to know the steps to check, is the server running in client side?


Please describe the problem in details, I didn't understand your point.
rajaguru731
Posted: Tuesday, October 14, 2008 5:30:39 AM
Rank: Member
Groups: Member

Joined: 8/25/2008
Posts: 9
Points: 33
Location: india
How to know the server status whether running or not through client side.
admin
Posted: Tuesday, October 14, 2008 5:43:45 AM
Rank: Administration
Groups: Administration

Joined: 2/15/2008
Posts: 130
Points: 835
Quote:
How to know the server status whether running or not through client side.

Try to establish a connection to the server, if you succeed than it is running otherwise it is not.
rajaguru731
Posted: Friday, October 17, 2008 1:44:34 AM
Rank: Member
Groups: Member

Joined: 8/25/2008
Posts: 9
Points: 33
Location: india
I got the following error:

"ContextSwitchDeadlock was detected.


The CLR has been unable to transition from COM context 0x1f7a00 to COM context 0x1f7c28 for 60

seconds. The thread that owns the destination context/apartment is most likely either doing a

non pumping wait or processing a very long running operation without pumping Windows messages.

This situation generally has a negative performance impact and may even lead to the application

becoming non responsive or memory usage accumulating continually over time. To avoid this

problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as

CoWaitForMultipleHandles) and routinely pump messages during long running operations."


please give solution for that.
admin
Posted: Friday, October 17, 2008 4:00:04 AM
Rank: Administration
Groups: Administration

Joined: 2/15/2008
Posts: 130
Points: 835
Please show your code.
alsan
Posted: Sunday, October 19, 2008 4:32:11 PM
Rank: Member
Groups: Member

Joined: 10/19/2008
Posts: 7
Points: 21
Hi,

Just write a test program, and found that it's quite unstable when working as TCP client AND outside the VS environment.

It works just fine while under VS C# 2008 express (works every time, and almost response immediately :)). But don't known why it just not work properly under command line. Almost 6~10 times retry with 1 success response, and it even can't connect to the target site. Here is my code.

Code:

namespace ConsoleApplication1 {
    internal sealed class TCPClient {
        private string _host;
        private int _port;

        public TCPClient(string host, int port) {
            _host = host;
            _port = port;
        }

        public void Send(string request, Action<string> action) {
            Network.ConnectAsync(_host, _port, Encoding.UTF8.GetBytes(request), connection => {
                connection.OnClose += flag => Console.WriteLine(_host + " connection closed.");
                connection.ReadAsync((ignored, buf, size) => {
                    Console.WriteLine(_host + " data readed.");

                    if (0 < size) {
                        string response = Encoding.UTF8.GetString(buf.Data, buf.Offset, size);
                        action(response);
                    }
                });
            });
        }
    }

    class Program {
        private const string REQUEST =
@"GET / HTTP/1.1
Host: {0}

";
        static void Main(string[] args) {
            new Program();

            if (Process.GetCurrentProcess().ProcessName.EndsWith(".vshost")) {
                Console.WriteLine("All done. Press any key to exit...");
                Console.ReadKey();
            }
        }

        private Program() {
            test("www.shangen-tech.com.cn", "shangen-tech.com.cn");
            test("www.shangen-tech.com", "shangen-tech.com");
        }

        private void test(string host, string domain) {
            new TCPClient(host, 80).Send(REQUEST.Set(domain), response => {
                Console.WriteLine("Response from " + host);
                Console.WriteLine("Length: " + response.Length.ToString());
            });
        }

        private void writeResponse(string sessionName, string response) {
            Console.WriteLine(sessionName + ": " + response.Length.ToString());
            Console.WriteLine(response);
        }
    }
}


And here is the expected output (under VS):
All done. Press any key to exit...
www.shangen-tech.com data readed.
Response from www.shangen-tech.com
Length: 713
www.shangen-tech.com connection closed.
www.shangen-tech.com data readed.
www.shangen-tech.com.cn data readed.
Response from www.shangen-tech.com.cn
Length: 168
www.shangen-tech.com.cn connection closed.
www.shangen-tech.com.cn data readed.

Can I have your opinion on what's wrong here? or it just a bug?
alsan
Posted: Sunday, October 19, 2008 4:42:01 PM
Rank: Member
Groups: Member

Joined: 10/19/2008
Posts: 7
Points: 21
alsan wrote:
Hi,

Just write a test program, and found that it's quite unstable when working as TCP client AND outside the VS environment.

It works just fine while under VS C# 2008 express (works every time, and almost response immediately :)). But don't known why it just not work properly under command line. Almost 6~10 times retry with 1 success response, and it even can't connect to the target site. Here is my code.

Code:

namespace ConsoleApplication1 {
    internal sealed class TCPClient {
        private string _host;
        private int _port;

        public TCPClient(string host, int port) {
            _host = host;
            _port = port;
        }

        public void Send(string request, Action<string> action) {
            Network.ConnectAsync(_host, _port, Encoding.UTF8.GetBytes(request), connection => {
                connection.OnClose += flag => Console.WriteLine(_host + " connection closed.");
                connection.ReadAsync((ignored, buf, size) => {
                    Console.WriteLine(_host + " data readed.");

                    if (0 < size) {
                        string response = Encoding.UTF8.GetString(buf.Data, buf.Offset, size);
                        action(response);
                    }
                });
            });
        }
    }

    class Program {
        private const string REQUEST =
@"GET / HTTP/1.1
Host: {0}

";
        static void Main(string[] args) {
            new Program();

            if (Process.GetCurrentProcess().ProcessName.EndsWith(".vshost")) {
                Console.WriteLine("All done. Press any key to exit...");
                Console.ReadKey();
            }
        }

        private Program() {
            test("www.shangen-tech.com.cn", "shangen-tech.com.cn");
            test("www.shangen-tech.com", "shangen-tech.com");
        }

        private void test(string host, string domain) {
            new TCPClient(host, 80).Send(REQUEST.Set(domain), response => {
                Console.WriteLine("Response from " + host);
                Console.WriteLine("Length: " + response.Length.ToString());
            });
        }

        private void writeResponse(string sessionName, string response) {
            Console.WriteLine(sessionName + ": " + response.Length.ToString());
            Console.WriteLine(response);
        }
    }
}


And here is the expected output (under VS):
All done. Press any key to exit...
www.shangen-tech.com data readed.
Response from www.shangen-tech.com
Length: 713
www.shangen-tech.com connection closed.
www.shangen-tech.com data readed.
www.shangen-tech.com.cn data readed.
Response from www.shangen-tech.com.cn
Length: 168
www.shangen-tech.com.cn connection closed.
www.shangen-tech.com.cn data readed.

Can I have your opinion on what's wrong here? or it just a bug?


I've change the code a little bit, like this:
Code:

namespace ConsoleApplication1 {
    internal sealed class TCPClient {
        private string _host;
        private int _port;

        public TCPClient(string host, int port) {
            _host = host;
            _port = port;
        }

        public void Send(string request, Action<string> action) {
            Console.WriteLine("Trying to connect " + _host);

            Network.ConnectAsync(_host, _port, Encoding.UTF8.GetBytes(request), connection => {
                Console.WriteLine(_host + " connected");

                connection.OnClose += flag => Console.WriteLine(_host + " connection closed.");
                connection.ReadAsync((ignored, buf, size) => {
                    Console.WriteLine(_host + " data readed.");

                    if (0 < size) {
                        string response = Encoding.UTF8.GetString(buf.Data, buf.Offset, size);
                        action(response);
                    }
                });
            });
        }
    }

    class Program {
        private const string REQUEST =
@"GET / HTTP/1.1
Host: {0}

";
        static void Main(string[] args) {
            new Program();

            Console.WriteLine("All done. ");

            if (Process.GetCurrentProcess().ProcessName.EndsWith(".vshost")) {
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }

        private Program() {
            test("www.shangen-tech.com.cn", "shangen-tech.com.cn");
            test("www.shangen-tech.com", "shangen-tech.com");
        }

        private void test(string host, string domain) {
            new TCPClient(host, 80).Send(REQUEST.Set(domain), response => {
                Console.WriteLine("Response from " + host);
                Console.WriteLine("Length: " + response.Length.ToString());
            });
        }

        private void writeResponse(string sessionName, string response) {
            Console.WriteLine(sessionName + ": " + response.Length.ToString());
            Console.WriteLine(response);
        }
    }
}


and the output from the command line like this:
Trying to connect www.shangen-tech.com.cn
Trying to connect www.shangen-tech.com
All done.
admin
Posted: Sunday, October 19, 2008 6:18:11 PM
Rank: Administration
Groups: Administration

Joined: 2/15/2008
Posts: 130
Points: 835
This part is funny:
Code:
if (Process.GetCurrentProcess().ProcessName.EndsWith(".vshost"))
{
     Console.WriteLine("Press any key to exit...");
     Console.ReadKey();
}

It waits for user to press any key before exit only if process name ends with vshost - which means running from VS.
Simply saying, in command line your program finishes before any reply from remote servers is received.
alsan
Posted: Sunday, October 19, 2008 8:08:28 PM
Rank: Member
Groups: Member

Joined: 10/19/2008
Posts: 7
Points: 21
admin wrote:
This part is funny:
Code:
if (Process.GetCurrentProcess().ProcessName.EndsWith(".vshost"))
{
     Console.WriteLine("Press any key to exit...");
     Console.ReadKey();
}

It waits for user to press any key before exit only if process name ends with vshost - which means running from VS.
Simply saying, in command line your program finishes before any reply from remote servers is received.


OK, thanks, this does the trick. All I want with this code is to by pass the "Press any key..." while in console mode, but forgot that the communication is in async mode.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.6 (NET v2.0) - 11/14/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.176 seconds.
19 queries (0.042 seconds, 23.86%).

yaf_pageload: 0.005
yaf_topic_info: 0.001
yaf_forum_list: 0.001
yaf_forum_listpath: 0.001
yaf_forum_listpath: 0.001
yaf_post_list: 0.016
yaf_usergroup_list: 0.002
yaf_attachment_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_active_listtopic: 0.004