|
-
Jan 5th, 2023, 11:16 AM
#1
Thread Starter
Junior Member
Need help with socket listener to stay active
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.
Code:
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
Last edited by Shaggy Hiker; Jan 5th, 2023 at 04:36 PM.
Reason: Added CODE tags.
-
Jan 5th, 2023, 05:46 PM
#2
Thread Starter
Junior Member
Re: Need help with socket listener to stay active
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
-
Jan 5th, 2023, 08:49 PM
#3
Lively Member
Re: Need help with socket listener to stay active
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) ?
-
Jan 5th, 2023, 11:01 PM
#4
Thread Starter
Junior Member
Re: Need help with socket listener to stay active
I was thinking of using non blocking Sockets or Async mode but have no clue as to how to implement it!
Thanks
-
Jan 6th, 2023, 12:45 AM
#5
Re: Need help with socket listener to stay active
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:
Code:
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
-
Jan 6th, 2023, 05:27 AM
#6
Re: Need help with socket listener to stay active
This line looks very sus to me
Code:
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>
-
Jan 6th, 2023, 01:05 PM
#7
Thread Starter
Junior Member
Re: Need help with socket listener to stay active
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|