[ Foro de BASIC ]

Error por excepción al crear mi servidor vb.net

10-Jan-2017 20:05
OMAR omar
1 Respuestas

Hola estoy intentando hacer un simple servidor en vb.net bueno lo hice pero esque ahora me da una excepción. La excepción es la siguiente:

Unhandled Exception: System.Net.Sockets.SocketException: An attempt was made to
access a socket in a way forbidden by its access permissions
  at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress
socketAddress)
  at System.Net.Sockets.Socket.Bind(EndPoint localEP)
  at System.Net.Sockets.TcpListener.Start(Int32 backlog)
  at System.Net.Sockets.TcpListener.Start()
  at name.TCPControl..ctor() in C:\Users\never\Desktop\New folder\TCPServerClea
n\name\TCPControl.vb:line 23
  at name.Program.Main() in C:\Users\never\Desktop\New folder\TCPServerClean\na
me\Program.vb:line 12
El código de mi servidor:

Module Program
   Public Server As TCPControl
   Sub Main()
       Server = New TCPControl
       Console.Write("Press any key to continue . . . ")
       Console.ReadKey(True)
   End Sub
End Module
Código de mi clase TCP:

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class TCPControl
   Public Event MessageReceived(sender As TCPControl, Data As String)

   ' SERVER CONFIG
   Public ServerIP As IPAddress = IPAddress.Any
   Public ServerPort As Integer = 64555
   Public Server As TcpListener

   Private CommThread As Thread
   Public IsListening As Boolean = True

   ' CLIENTS
   Private Client As TcpClient
   Private ClientData As StreamReader

   Public Sub New()
       Server = New TcpListener(ServerIP, ServerPort)
       Server.Start()

       CommThread = New Thread(New ThreadStart(AddressOf Listening))
       CommThread.Start()
   End Sub

   Private Sub Listening()
       ' CREATE LISTENER LOOP
       Do Until IsListening = False
           ' ACCEPT INCOMING CONNECTIONS
           If Server.Pending = True Then
               Client = Server.AcceptTcpClient
               ClientData = New StreamReader(Client.GetStream)

           End If

           ' RAISE EVENT FOR INCOMING MESSAGES
           Try
               RaiseEvent MessageReceived(Me, ClientData.ReadLine)
           Catch ex As Exception

           End Try

           ' REDUCE CPU USAGE
           'Thread.Sleep(100)
       Loop
   End Sub
End Class


16-Jan-2017 13:49
Nacho Cabanes (+83)

No sé qué línea exacta es la que te da el problema, pero en casos como estos, yo te recomendaría acudir a la referencia oficial en MSDN, para comprobar que tus parámetros sean los correctos, así como las limitaciones de los métodos que uses. Por ejemplo, la referencia de Bind está aquí:

https://msdn.microsoft.com/es-es/library/system.net.sockets.socket.bind(v=vs.110).aspx
 






(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)