"Hello world",
i'm a visual c# developer but i'm new to ASP.NET.
My question is this one:
it is possibile to make a TCP connection (opening socket e.g.) from my client application to a my web application asp.net? If so, could you suggest how i can start?
Thank you for any suggestion.
Regards,
Lewix
Yes, you could. You can use the System.Net.Sockets namespace. This contains thatTcpClient object and theTcpListener class. Your Web Application will be the listener in this case, and your client will connect to it using the TcpClient class. However, a much more common means of a client communicating with a Web Application is by the means of SOAPWeb Services.
Thank you for your fast reply! :)
last 2 question, then i hope i don't disturb you again:
- the client have to connect to (for example) "www.mydomain.com/listen.aspx:80" ?
- in the asp code i have to handle a "on http request" event and then i start accepting socket?
Sorry for these strange question but with desktop programming is simpler for me :) for now
Thank you for any suggestion
Lewix
Watch DNR TVhttp://www.dnrtv.com/default.aspx?showID=46
LucidInDreams:
Watch DNR TVhttp://www.dnrtv.com/default.aspx?showID=46
Thank you for the link but it explains how to build a tcp client; i would like to learn how to write a asp code for the asp sever side (tcplistener i think)
If you click on the TcpListener link in my above post, the page will show you an example of how to use that class.
So if your question is how to write client on Web, here is the answer:
<%@. Page Language="C#" AutoEventWireup="false" Trace="true" %><%@. Import namespace="System.Data"%><%@. Import namespace="System.Net"%><%@. Import namespace="System.Net.Sockets"%><script language="C#" runat="server">protected override void OnInit(EventArgs e){base.OnInit(e);this.Load += new EventHandler(this.Page_Load);}protected void Page_Load(object sender, EventArgs e){string host = "www.asp.net";string path = "/resources/default.aspx?tabid=41";int port = 80;string result = this.SocketSendReceive(host, port, path); this.Trace.Warn(result);}private static Socket ConnectSocket(string server, int port){Socket s = null;IPHostEntry hostEntry = null;// Get host related information.hostEntry = Dns.GetHostEntry(server);// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid// an exception that occurs when the host IP Address is not compatible with the address family// (typical in the IPv6 case).foreach(IPAddress address in hostEntry.AddressList){IPEndPoint ipe = new IPEndPoint(address, port);Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);tempSocket.Connect(ipe);if(tempSocket.Connected){s = tempSocket;break;}else{continue;}}return s;}// This method requests the home page content for the specified server.private string SocketSendReceive(string server, int port, string path) {// Create a socket connection with the specified server and port.Socket s = ConnectSocket(server, port);if (s == null)return ("Connection failed"); string request = "GET "+path+" HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n";byte[] bytesSent = Encoding.ASCII.GetBytes(request);byte[] bytesReceived = new Byte[256];// Send request to the server.s.Send(bytesSent, bytesSent.Length, 0); // Receive the server home page content.int bytes = 0;StringBuilder sb = new StringBuilder();// The following will block until te page is transmitted.do {bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);sb.Append( Encoding.ASCII.GetString(bytesReceived, 0, bytes) );}while (bytes > 0);return sb.ToString();}</script>
And if you are asking how to build web server, then my question is: Why not having web service if this will be web server based? In the dnrtv example they also have server by the way...
Thank you to all, really.
All yu helped me helped me for all i needed (sorry for my orrible english, i'm italian).
for LucidInDreams : thank you, no i don't need to build a web server, i was interested only to asp code tu build a socket connection. I was warried it was very different from c#, instead it is similiar :)
Thank you to all again! :)
Lewix
0 comments:
Post a Comment