# Visual Basic > Visual Basic .NET >  Need help with socket listener to stay active

## peterg2000

I have a server used to get info from SAPGUI. Everything works fine for a first request but nothing after that. The first iteration waits for a socket event. Once the socket is read for the first time it just loops without reading the receive buffer. I am assuming I need to reset the listener but am having trouble doing so.

'first iteration it works as expected but readbyte is always 0 after that even though it loops through fine. 'what I don't get is that the first iteration it waits for data to come down the socket, but after that it just loops 'and doesn't retreive anything.


```
          Dim listenerSocket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim ipend As IPEndPoint = New IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888)
    listenerSocket.Bind(ipend)
    listenerSocket.Listen(0)
    Dim clientSocket As Socket = listenerSocket.Accept()
    Dim Buffer As Byte() = New Byte(clientSocket.SendBufferSize - 1) {}
    Dim readByte, sendByte As Integer
    Dim Sbuffer As Byte() = System.Text.Encoding.ASCII.GetBytes("Some form of SAP response!")
    Dim clientError As System.Net.Sockets.SocketError
    Console.WriteLine("Waiting for connection : ")



   For infinitecounter = 1 To 3
       infinitecounter = 1
       readByte = clientSocket.Receive(Buffer) 

        While (True)
            If readByte > 0 Then
                Dim rData As Byte() = New Byte(readByte - 1) {}
                Array.Copy(Buffer, rData, readByte)


               SAPrq = System.Text.Encoding.UTF8.GetString(rData)
                SAPpar = Left(SAPrq, 1)

                SAPpar1 = InStr(SAPrq, "%")

                If SAPpar = 1 Then
                    SAPnwk = Mid(SAPrq, 3)

                    xy = Getbam(SAPnwk)' this is a function that talks to SAP and it works on first loop

                End If



        End While
       
    Next`
```

I tried adding setting infinitecounter = 2 before the next statement and adding an if statement to reset the listener but failed.

I am not that good at VB.NET so even if there may be related posts , I am too inexperienced to hack them to fit my needs.
I've been working on a solution for a while and can't find a solution on my own. 

Pete

----------


## peterg2000

I am open to an alternative but since Ièm not used to VB.net , I'd need some help with coding that! 

Thanks

Pete

----------


## bahbahbah

It looks like your While loop is just going to go on forever. Nothing is going to make it exit. Is it meant to be *While(readByte > 0)* instead of *While(True)* ?

----------


## peterg2000

I was thinking of using non blocking Sockets  or Async mode but have no clue as to how to implement it! 

Thanks

----------


## schoemr

Hi Peter,

I don't know exactly what your code is doing but what I see is that loop inside the For loop is endless.. I mean it will go on forever. There is no way to get out of that loop...so the readByte is always 0

You must get out of the loop..

maybe:




```
While (True)
    readByte = clientSocket.Receive(Buffer)
    If readByte > 0 Then
        ' you recieved data and is doing something with it
    Else
        ' No more data so exit the loop
        Exit While
    End If
End While
```

----------


## wqweto

This line looks very sus to me



```
    Dim clientSocket As Socket = listenerSocket.Accept()
```

A server should be accepting sockets (calling Accept on listener socket to create new sockets) on new incomming client connection Events only, so that you you get separate *clientSocket* for second, third and so on clients.

Currently you get/accept a *clientSocket* for the first client, handle communication/protocol in the While loop and then exit procedure without waiting for any more incomming traffic (i.e. other consequtive connections) which is probably not what you wanted to do.

cheers,
</wqw>

----------


## peterg2000

You were correct as I added this last night and all works great now! 
           If infinitecounter = 2 Then

                listenerSocket.Accept()
                clientSocket = listenerSocket.Accept()

            End If
Peter

----------

