2017-09-13 10 views
0

J'ai le code qui envoie la chaîne du client au serveur avec la connexion TCP. Mais je ne sais pas comment le convertir en TCP NonBlocking State Connection. J'ai essayé de mettre socket.Blocking = FALSE, mais ce code me donne une erreur.VB.net État TCP non bloquant

Socket Code d'erreur: 10035

Exception Titre: Une opération de socket non bloquant ne peut pas être effectuée immédiatement

Heres mon code pour envoyer la chaîne du client au serveur.

Code Serveur:

Imports System.Net 
Imports System.Net.Sockets 
Imports System.Text 

Module Module1 
    Dim soket As Socket 
    Sub Main() 
     Try 
      Dim alamatIP As IPAddress = IPAddress.Parse("127.0.0.1") 
      Dim tcpListener As New TcpListener(alamatIP, 8000) 
      'soket.Blocking = False - THIS WILL GIVE ME AN ERROR 
      tcpListener.Start() 

      System.Console.WriteLine("Server port 8000...") 

      Dim myendpoint As IPEndPoint 
      myendpoint = CType(tcpListener.LocalEndpoint, IPEndPoint) 

      System.Console.WriteLine("IP : " + myendpoint.Address.ToString + " and port is " + myendpoint.Port.ToString) 
      System.Console.WriteLine("Waiting for connection !") 

      soket = tcpListener.AcceptSocket() 
      myendpoint = CType(soket.RemoteEndPoint(), IPEndPoint) 

      System.Console.WriteLine("Receiving from " + myendpoint.Address.ToString()) 

      Dim bitData(100) As Byte 
      Dim newString As String = "" 
      Do 
       Try 
        Dim size As Integer = soket.Receive(bitData) 
        newString = Encoding.ASCII.GetString(bitData) 
        Console.WriteLine(newString) 
        Array.Clear(bitData, 0, bitData.Length) 
       Catch ex As Exception 
        Console.Write(ex.ToString()) 
       End Try 
      Loop While newString <> "exit" 
      soket.Close() 
      tcpListener.Stop() 
     Catch ex As Exception 
      Console.WriteLine("Error ..." + ex.ToString()) 
     End Try 
     Console.ReadLine() 
    End Sub 

End Module 

Code client:

Imports System.Net 
Imports System.Net.Sockets 
Imports System.Text 
Imports System.IO 
Imports System.Threading 
Module Module1 

    Sub Main() 
     Try 
      Dim tcpClient As New TcpClient 'creating the client 
      Console.WriteLine("Connecting....") 
      tcpClient.Connect("127.0.0.1", 8000) 'connecting the client the server 
      'port is same as in the server 
      Console.WriteLine("Connected") 
      Console.WriteLine("Enter the string to be transmitted : ") 

      Dim strMessage As String 
      Dim stm As Stream 
      Dim counter As Integer = 0 

      Do 

       stm = tcpClient.GetStream() 'getting the stream of the client 
       Dim ascenc As New ASCIIEncoding 
       strMessage = Console.ReadLine() 
       Dim byteData() As Byte = ascenc.GetBytes(strMessage) 
       Console.WriteLine("Transmitted ") 
       stm.Write(byteData, 0, byteData.Length()) 

      Loop While strMessage <> "exit" 

        tcpClient.Close() 'closing the connection 
     Catch ex As Exception 
      Console.WriteLine("Error..." + ex.StackTrace.ToString()) 'writing the exception into the console 
     End Try 
    End Sub 

End Module 

Merci pour toute aide.

+0

Avez-vous fatigué l'adresse IP locale du serveur? –

+0

Pourquoi voudriez-vous le rendre non bloquant? Cela briserait votre code, car il est conçu pour attendre l'information. Avez-vous entendu parler de sockets asynchrones? ([** client **] (https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-client-socket-example) et [** serveur **] (https: //docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example) exemple) Sachez que vous devez toujours placer un 'wait' dans la méthode 'Main' afin qu'il ne quitte pas l'application jusqu'à ce que vous disiez qu'il peut le faire. –

+0

est-il difficile de faire une prise non bloquante? existe-t-il des références pour rendre le serveur TCP non bloquant. –

Répondre

1

Lorsque vous essayez de lire à partir d'un socket non bloquant ou d'y écrire et que l'opération ne peut pas être terminée maintenant, le socket est supposé signaler une erreur. En VB, cela signifie qu'il est censé déclencher une exception. Par exemple, cela se produit lorsque votre machine n'a pas reçu de données mais que vous essayez de Read quelque chose.

Avant d'essayer une opération sur un socket non bloquant, appelez Select ou Poll afin de vous assurer qu'il est prêt pour l'opération.