I am very new to working with sockets so I have no idea if anything I am doing is correct.
I am working on a program that has a thread pool to deal with when something connects to a socket. I run into the problem of not being able to tell when the client has disconnected unless I intentionally create an error that occurs by trying to send something. I am not allowed to close the socket myself, the socket must be kept open so that more then a single packet can be sent. There is no set amount of packets either. I must keep the thread alive until the one who requested the socket disconnects.
Here is my code that is revalent. If you need something I failed to provide just tell me what and I will post it as soon as possible.
I am working on a program that has a thread pool to deal with when something connects to a socket. I run into the problem of not being able to tell when the client has disconnected unless I intentionally create an error that occurs by trying to send something. I am not allowed to close the socket myself, the socket must be kept open so that more then a single packet can be sent. There is no set amount of packets either. I must keep the thread alive until the one who requested the socket disconnects.
Here is my code that is revalent. If you need something I failed to provide just tell me what and I will post it as soon as possible.
VB.NET:
Imports System.Net.Sockets
Imports System.net
Imports System.Text
Imports System.Threading
Dim portnumber As Integer = xxxx 'Incoming Port from Units
Dim MainSocketThread As New System.Threading.Thread(AddressOf SocketThread)
Dim sendport As Integer = xxxx
Dim something As New SocketException
Dim something2 As New SocketError
Dim asdf As Socket
Public Sub SocketThread()
Try
Dim sockettcpListener As New TcpListener(CType(Dns.GetHostEntry("xxx.xxx.xxx.xxx").AddressList(0), IPAddress), portnumber)
sockettcpListener.Start()
Try
Dim i As Integer = 0
While i < 1
Dim sockettcpClient As TcpClient = sockettcpListener.AcceptTcpClient()
ThreadPool.QueueUserWorkItem(AddressOf listenthread, sockettcpClient)
End While
Catch ex As Exception
MessageBox.Show("Thread stopped running at " & System.DateTime.Now & vbNewLine & ex.Message)
End Try
sockettcpListener.Stop()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub listenthread(ByVal sockettcpClient)
Debug.WriteLine("start Socket thread id: " & System.Threading.Thread.CurrentThread.ManagedThreadId)
Try
Do
Dim networkStream As NetworkStream = sockettcpClient.GetStream()
Dim bytes(sockettcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(sockettcpClient.ReceiveBufferSize))
parsed(bytes)
'networkStream.WriteByte(142) 'hex code "8E"
'This line is the inserted error line.
Loop While (socket is open)
Catch ex As Sockets.SocketException
Debug.WriteLine(ex.ErrorCode.ToString)
Catch ex As IO.IOException
Debug.WriteLine("io error " & ex.Message)
Debug.WriteLine(something.ErrorCode.ToString)
Select Case something.ErrorCode
Case 0
Debug.WriteLine("success")
Case Sockets.SocketError.Success
Case Else
Debug.WriteLine("diff message: " & something.ErrorCode)
End Select
Catch ex As Exception
Debug.WriteLine(System.DateTime.Now & " " & "Socket listen error: " & ex.ToString)
Debug.WriteLine("something's text " & something.ErrorCode.ToString())
End Try
Debug.WriteLine("end Socket thread id: " & System.Threading.Thread.CurrentThread.ManagedThreadId)
End Sub
Public Sub parsed(ByVal bytes)
Debug.WriteLine("Extracted: " & byterun(bytes, 0, bytes.ToString.Length))
End Sub
Last edited by a moderator: