.NET Socket implementation

Hi, I'm new to asp.net web application and also socket programming.

I need help on socket programming. I want to create a asp.net web application page that can connect to the server using socket and for the data access part I need to use xml for communication between the server and the client applcation. The server connect to three databases and it acts as a middle man between the client and the database. The server is a program written in java.

How am I suppose to do the socket programming inside my web application. Can anyone help me with that I really need help... As I can't find any good example on socket programming. Please help...

Snow



Answer this question

.NET Socket implementation

  • JIM.H.

    Ya. Hi the problem of receiving is solved. But now another problem arised. I can receive the datapacket from the server but now I have some problem on displaying the xml data in my application for example on label, listbox.

    What is the problem you think it is for not displaying it I tried changing the parsing method but still cannot. Once i load the page i just take forever to load and nothing happen. At te server side I got finished receiving the xml data but for page load part the client send quit xml to destroy the connection to server.

    Another question I have is: How can i display a image receive from the server using socket Is there any converion needed


  • slush_puppy

    Hi everyone,

    I have a requirements that needs to send message to client app coming from an admin web page.

    What i want to do is to have the client application a listening Socket. where in my page can directly send message

    is it possible

    There is two plan that i want to use,

    1. if possible, Web page will send message to the client application using socket.

    2. if not, Web page will save messages directly to database and use a server application that will look up to the database for in coming messages and the send it to the client application.

    But i prefer to use option 1 if possible

    Can you guys help me what is the possible way to do it

    Thanks in advance.



  • ctsand

    The server structure is already set to used socket for connect,send,receive and communicate in xml with client. So I can't change the structure to use web service. My part is fixed to created a web application using ASP.Net.

    What I have being told is that I need to create a socket in my web application. Is it possible to use/create socket in ASP.Net web application  

    Or is it possible to write a vb.net servlet in between the client app and the server if socket can't be used in web app What is a servlet

    Can give me some comment/suggestion or reference for me as I really don't know how to do it..


  • Ammar1985

    This MSDN page has good samples to get you started:

    http://msdn2.microsoft.com/en-us/w89fhyex.aspx

    You should be able to do this from your code-behind files in asp.net



  • guyinkalamazoo3

    Can you be specific. Do you see any error if so what's it and from where its originated.

    Best Regards,

    Rizwan aka RizwanSharp



  • VikasGoyal

    What you mean is I created a asp.net page to put the socket connection codes right But how do I reference or used the socket connection codes from the other pages of my asp.net web application


  • Deeps_123

    The best implementation fro your scenerio is to put a Web Service as a Server Application which exposes functionality related to all 3 Databases it has to deal with. Then now you dont have problem in creating a client in sense that a client can be a Windows Application, a Windows Service, a Web Application, a Console Application, a Mobile Application etc.... in .Net or in any other language. I hope you understand what I mean here.

    As you told that your Server is a Socket Based server which uses Xml Based messages to communicate with client. You need a Client socket to communicate with Server Socket and pass the messages back and forth which seems to be impossible to use a Socket in a Web Page.

    So I would suggest you to re think your requirements and modify accordingly.

    Q) What you can re think specifically

    A) 1) To Re write the server using Web Service technology....

    2) To implement Client as a windows Application that interacts directly with that java based server without needing a middle ware ASP.Net web page.

    If i would be in your place and all client and server has to be built by me I would go with Web Service solution.......

    Best Regards and Best of Luck.

    Rizwan aka RizwanSharp



  • CalBob

    I need socket because my project requirement is to use socket to communicate with the server to access the database connected to the server. The server use xml to communicate with the client web application. For what I know, I need to put xml tags inside the socket codes for send and receive part.

    My Requirement

    A server pc connected to 3 databases. The client use xml to communicate with the server to access the database data. For my case is to create a web application using ASP.NET and use the socket to communicate with the server. The server is a program written in Java and is already there. I don't need to create the server, only the client is needed.

    My Problem

    I have already create a Master page and a tree view site navigation for my webpage now is only the connection part and data access part.

    I want to connect to the server using socket but i don't know how to implement it inside my asp.net web application. Is there any other alternatives since you have pointed it out to me or do you have any solution for me to my problem


  • Wasim

    You can't use SOckets in ASP.Net...... Why do you need sockets inWebpage there may be alternate i can point if you tell me your requirements.

    Best Regards,

    Rizwan aka RizwanSharp



  • eclere

    Hi, I have used the asynchronous client socket example. But I have problem receiving xml tags from server for don't know what reason for patientmain page. In patientmain page i want to display the patient name in a listbox but nothing was display. I went to debug using stepinto but only see "</packet> was receive. Why is it so For my login page I also used the same code for receiving and sending and it works it redirect me to another page when login successful. I put all the socket codes inside a controller.vb class file and used it for all pages. Can you help me with it As I don't hve any ideas where the problem lies..

    Controller.vb

    #Region "Import Libraries"

    Imports System

    Imports System.Net

    Imports System.Net.Sockets

    Imports System.Threading

    Imports System.Text

    #End Region

    ' State object for receiving data from remote device.

    Public Class StateObject

    ' Client socket.

    Public workSocket As Socket = Nothing

    ' Size of receive buffer.

    Public Const BufferSize As Integer = 256

    ' Receive buffer.

    Public buffer(BufferSize) As Byte

    ' Received data string.

    Public sb As New StringBuilder

    End Class 'StateObject

    Public Class Controller

    ' ManualResetEvent instances signal completion.

    Public Shared connectDone As New ManualResetEvent(False)

    Public Shared sendDone As New ManualResetEvent(False)

    Public Shared receiveDone As New ManualResetEvent(False)

    ' The response from the remote device.

    Public Shared mdcsResponse As String = String.Empty

    Public Shared client As Socket

    Public Shared Sub Connect()

    ' Establish the remote endpoint for the socket.

    ' For this example use local machine.

    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry("192.168.1.166")

    Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)

    Dim remoteEP As New IPEndPoint(ipAddress, 12111)

    ' Create a TCP/IP socket.

    client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' Connect to the remote endpoint.

    client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client)

    ' Wait for connect.

    connectDone.WaitOne()

    End Sub

    Private Shared Sub ConnectCallback(ByVal ar As IAsyncResult)

    ' Retrieve the socket from the state object.

    Dim client As Socket = CType(ar.AsyncState, Socket)

    ' Complete the connection.

    client.EndConnect(ar)

    Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString())

    ' Signal that the connection has been made.

    connectDone.Set()

    End Sub 'ConnectCallback

    Public Shared Sub Receive(ByVal client As Socket)

    Controller.receiveDone.Reset()

    ' Create the state object.

    Dim state As New StateObject

    'Controller.mdcsResponse = New String("")

    state.workSocket = client

    ' Begin receiving the data from the remote device.

    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)

    End Sub 'Receive

    Public Shared Sub ReceiveCallback(ByVal ar As IAsyncResult)

    ' Retrieve the state object and the client socket

    ' from the asynchronous state object.

    Dim state As StateObject = CType(ar.AsyncState, StateObject)

    Dim s As Socket = state.workSocket

    ' Read data from the remote device.

    Dim bytesRead As Integer

    Try

    bytesRead = s.EndReceive(ar)

    Catch e As Exception

    End Try

    If bytesRead > 0 Then

    ' There might be more data, so store the data received so far.

    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))

    'Console.Error.WriteLine("Recieved:" & state.sb.ToString)

    ' Get the rest of the data.

    s.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)

    If state.sb.ToString().Contains("</packet>") Then

    ' All the data has arrived; put it in response.

    If state.sb.Length > 1 Then

    mdcsResponse = state.sb.ToString()

    state.sb.Remove(0, state.sb.Length)

    End If

    ' Signal that all bytes have been received.

    receiveDone.Set()

    End If

    End If

    End Sub 'ReceiveCallback

    Public Shared Sub Send(ByVal client As Socket, ByVal data As String)

    ' Convert the string data to byte data using ASCII encoding.

    Controller.sendDone.Reset()

    'Controller.mdcsResponse = New String("")

    Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)

    If Not client.Connected Then

    Controller.Connect()

    End If

    ' Begin sending the data to the remote device.

    client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)

    End Sub 'Send

    Private Shared Sub SendCallback(ByVal ar As IAsyncResult)

    ' Retrieve the socket from the state object.

    Dim client As Socket = CType(ar.AsyncState, Socket)

    sendDone.Set()

    End Sub 'SendCallback

    Public Shared Function getResponse() As String

    Return mdcsResponse

    End Function

    End Class

    PatientMain.aspx.vb

    #Region "Import Libraries"

    Imports System

    Imports System.IO

    Imports System.Net

    Imports System.Net.Sockets

    Imports System.Threading

    Imports System.Text

    Imports System.Xml

    #End Region

    Partial Public Class PatientMain

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Controller.Send(Controller.client, "<hc type=""list""/>" & vbCrLf)

    Controller.sendDone.WaitOne()

    Controller.Receive(Controller.client)

    Controller.receiveDone.WaitOne()

    Dim response As String = String.Empty

    response = Controller.getResponse

    Dim xmlRead As New XmlTextReader(New StringReader(response))

    Try

    While xmlRead.Read()

    If xmlRead.Name = "patient" Then

    ListBoxMain.Items.Add(xmlRead.GetAttribute("firstname") & " " & xmlRead.GetAttribute("lastname") & " " & xmlRead.GetAttribute("mrn"))

    End If

    End While

    Catch ex As Exception

    End Try

    End Sub

    End Class

    Login.aspx.vb

    #Region "Import Libraries"

    Imports System

    Imports System.Net

    Imports System.Net.Sockets

    Imports System.Threading

    Imports System.Text

    Imports WebPhysician.Controller

    #End Region

    Partial Public Class Login

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub LoginBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginBtn.Click

    Controller.Connect()

    If Not txtusername.Text.Equals(String.Empty) And Not txtpwd.Text.Equals(String.Empty) Then

    Controller.Send(Controller.client, "<auth><username>" & txtusername.Text & "</username><password>" & txtpwd.Text & "</password></auth>" & vbCrLf)

    Controller.sendDone.WaitOne()

    'Receive the response from the remote device.

    Controller.Receive(Controller.client)

    Controller.receiveDone.WaitOne()

    'Write the response to the console.

    If Controller.mdcsResponse.Contains("<auth><status>0</status></auth>") Then

    Label1.Text = "Login unsucessful, Please ensure that your username and password is valid"

    Response.Redirect("~/Logout.aspx")

    ElseIf Controller.mdcsResponse.Contains("<auth><status>1</status></auth>") Then

    Response.Redirect("~/Welcome.aspx")

    End If

    End If

    End Sub

    End Class

     


  • .NET Socket implementation