Sockets (TCP/IP) en VB.NET (avanzados)
Hola amigos miren lo que quiero es que estas dos aplicaciones se conecten para poder enviarse datos pero cuando lo corro no funciona, el servidor muestra el mensaje de esperando conexión y allí se queda, luego ejecuto el cliente y dice que el servidor no ha respondido. Ayúdenme por favor
Tengo es este código en vb.net 2008
Servidor:
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Must listen on correct port- must be same as port client wants to connect on.
Dim port As Int32 = 8000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Dim tcpListener As New TcpListener(localAddr, port)
tcpListener.Start()
MsgBox("Waiting for connection...")
Try
'Accept the pending client connection and return 'a TcpClient initialized for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString))
'Any communication with the remote client using the TcpClient can go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
tcpListener.Stop()
Console.WriteLine("exit")
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.ToString())
Console.ReadLine()
End Try
End Sub
End Class
Cliente:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("190.95.221.211", 8000)
Dim networkStream As Net.Sockets.NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
' Do a simple write.
Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes("Is anybody there")
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Host returned: " + returndata))
Else
If Not networkStream.CanRead Then
Console.WriteLine("cannot not write data to this stream")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Console.WriteLine("cannot read data from this stream")
tcpClient.Close()
End If
End If
End If
' pause so user can view the console output
Console.ReadLine()
End Sub
End Class