|
|
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).
|
|
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?
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
Rank: Administration Groups: Administration
Joined: 2/15/2008 Posts: 130 Points: 835
|
Please show your code.
|
|
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.comLength: 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.cnLength: 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?
|
|
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.comLength: 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.cnLength: 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.cnTrying to connect www.shangen-tech.comAll done.
|
|
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.
|
|
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.
|
|
|
Guest |