Estoy haciendo un web service que me permita enviar información entre dos aplicaciones, una es una página web en asp.net y la otra una aplicación de escritorio den vb.net
Solo sé (gracias a ejemplos en la web de microsoft) crear funciones que me retornan datos según lo que quiera hacer, por ejemplo:
<System.Web.Services.WebMethod()> _
Public Function FahrenheitToCelsius(ByVal Fahrenheit As Double) _
As Double
Return ((Fahrenheit - 32) * 5) / 9
End Function
Conversiones entre datos y esas cosas...
<System.Web.Services.WebMethod()> _
Public Function CelsiusToFahrenheit(ByVal Celsius As Double) _
As Double
Return ((Celsius * 9) / 5) + 32
End Function
Sé hacer el envío de datos mediante sockets, pero ahora quiero hacer lo mismo pero usando un web service
Por ejemplo, aquí uso sockets para recibir los datos de una consulta
Public Function RecibirSocket()
Dim flag As String = ""
' Create an instance of the TcpListener class.
Dim output As String = ""
Dim tcpListener As TcpListener = Nothing
Dim ipAddress As IPAddress = Dns.GetHostEntry("localhost").AddressList(0)
Try
' Set the listener on the local IP address.
' and specify the port.
tcpListener = New TcpListener(ipAddress, 60000)
tcpListener.Start()
output = "Waiting for a connection..."
Catch e As Exception
output = "Error: " + e.ToString()
MessageBox.Show(output)
End Try
While flag <> "Ok"
' Always use a Sleep call in a while(true) loop
' to avoid locking up your CPU.
'Thread.Sleep(10)
' Create a TCP socket.
' If you ran this server on the desktop, you could use
' Socket socket = tcpListener.AcceptSocket()
' for greater flexibility.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
' Read the data stream from the client.
Dim bytes(1800) As Byte
Dim stream As NetworkStream = tcpClient.GetStream()
stream.Read(bytes, 0, bytes.Length)
processMsg(tcpClient, stream, bytes)
tcpListener.Stop()
flag = "Ok"
End While
Return flag
End Function
El otro método que llamo
Private mscClient As TcpClient = Nothing
Private mstrMessage As Byte() = Nothing
Private mstrResponse As String = Nothing
Private bytesSent() As Byte = Nothing
Public Sub processMsg(ByVal client As TcpClient, ByVal stream As NetworkStream, ByVal bytesReceived() As Byte)
' Handle the message received and
' send a response back to the client.
Try
mstrMessage = (bytesReceived)
mscClient = client
mstrResponse = "Recibido Correctamente"
Catch ex As Exception
mstrResponse = "Recibido pero con errores"
MsgBox("Error: " & ex.Message)
End Try
bytesSent = Encoding.ASCII.GetBytes(mstrResponse)
stream.Write(bytesSent, 0, bytesSent.Length)
End Sub
Para enviar el socket en la aplicación cliente pues uso esto:
Protected Sub Send()
Dim conexion As New SqlConnection(Cadena_Conexion)
Dim a As Byte() = Nothing
Dim Result As String = ""
Dim sql As String = ""
Try
conexion.Open()
sql = "SELECT H_1 FROM Persona WHERE H_TypeDocument_ID = '1' AND H_Document_ID = '1032494911'"
Dim cmd As New SqlCommand(sql, conexion)
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.Read Then
a = reader.GetValue(0)
End If
reader.Close()
cmd.Dispose()
conexion.Dispose()
Dim output As String = ""
Dim serverIP As String = "localhost"
Try
' Create a TcpClient.
' The client requires a TcpServer that is connected
' to the same address specified by the server and port
' combination.
Dim port As Int32 = 60000
'Dim port As Int32 = 13
Dim client As New TcpClient(serverIP, port)
' Translate the passed message into ASCII and store it as a byte array.
Dim data As Byte()
data = a
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
MsgBox("Tamaño enviado: " & data.Length)
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
output = "Sent: Se ha enviado los byte correctamente"
MessageBox.Show(output)
' Buffer to store the response bytes.
data = New [Byte](1800) {}
' String to store the response ASCII representation.
Dim responseData As String = String.Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
output = "Received: " + responseData
MessageBox.Show(output)
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
output = "ArgumentNullException: " + e.ToString()
MessageBox.Show(output)
Catch e As SocketException
output = "SocketException: " + e.ToString()
MessageBox.Show(output)
End Try
Result = "Exito"
Catch ex As Exception
Result = "Error: " & ex.ToString
End Try
End Sub
¿Cómo se hace esto, cómo uso un web service asp.net para hacer traslado de información entre estas dos aplicaciones cómo lo hago con sockets?
sockets
) ni tus intentos de usarweb services
. Sin más informacion no podemos solucionar tu problema. Un saludo!web services
con tus dudas o con los errores que te salen para poder orientarte. Un saludo