B4J Question UDPsocket.close

HansDieter

Member
Licensed User
Longtime User
Hello,

i'm initializing an UDP.Socket in APPSTART. On a config-form i give the user the option to change the port for listening. To do so i try to close the socket with the old port and initialize a new one with the new port. Executeing the socket.close (i tried in debug mode executing step by step) statement throws some JAVA exeptions.

Here an excerpt from my code:

Code: Copy
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private btn_save_port As Button
    Private localport As TextField
    Private msgbox As Msgboxes
    Private UDPSocket1 As UDPSocket
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    UDPSocket1.Initialize("UDP", 5000, 8000)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("start") 'Load the layout file.
    MainForm.Show
End Sub
Sub btn_save_port_Action   
    'restart socket with new Port
    UDPSocket1.Close
    UDPSocket1.Initialize("UDP",localport.Text,8000)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msgbox.show("Message received: " & msg, "")
End Sub

And here the exeptions thrown when executeing UDPSocket1.Close:

Code: Copy
Program started.
java.net.SocketException: socket closed
    at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
    at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
    at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
    at java.net.DatagramSocket.receive(DatagramSocket.java:786)
    at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket$UDPReader.run(SocketWrapper.java:382)
    at java.lang.Thread.run(Thread.java:745)

What i'm doing wrong?

Hans
 
Back
Top