Hello, I have a TCP connection code for client and server communication. It's not meant to be malware but it collects the clients computer information and then connects to the server.
It successfully "connects" but then immediately disconnects.
Here is the code for the server:
Dim Listener As TcpListener
Dim Client As TcpClient
Dim ClientList As New List(Of ChatClient)
Dim sReader As StreamReader
Dim cClient As ChatClient
Public Sub Doit()
Listener.Start()
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Public Sub Start()
Listener = New TcpListener(IPAddress.Any, myport)
Dim th As New Thread(AddressOf Doit)
th.IsBackground = True
th.Start()
End Sub
Sub AcceptClient(ByVal ar As IAsyncResult)
cClient = New ChatClient(Listener.EndAcceptTcpClient(ar))
AddHandler (cClient.MessageRecieved), AddressOf MessageRecieved
AddHandler (cClient.ClientExited), AddressOf ClientExited
ClientList.Add(cClient)
MsgBox("New connection from " & cClient.PublicIPAddress & "! Awaiting authentication...")
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub MessageRecieved(ByVal Str As String)
Select Case Str.Split("@")(0)
Case "1"
Dim system As String = Str.Split("@")(1)
MsgBox("The following client: " & system & " has disconnected.")
End Select
End Sub
Sub ClientExited(ByVal Client As ChatClient)
ClientList.Remove(Client)
RemoveFromListview(Client.PublicIPAddress)
MsgBox("The following client: " & Client.PublicIPAddress & " has disconnected.")
End Sub
Public Class ChatClient
Public Event MessageRecieved(ByVal Str As String)
Public Event ClientExited(ByVal Client As ChatClient)
Private sWriter As StreamWriter
Private Client As TcpClient
Private PublicIP As String
Public ReadOnly Property PublicIPAddress() As String
Get
If PublicIP = String.Empty Then
Try
Dim ipend As Net.IPEndPoint = Client.Client.RemoteEndPoint
If Not ipend Is Nothing Then
PublicIP = ipend.Address.ToString
End If
Catch ex As System.ObjectDisposedException
PublicIP = String.Empty
Catch ex As SocketException
PublicIP = String.Empty
End Try
End If
Return PublicIP
End Get
End Property
Sub New(ByVal xclient As TcpClient)
Client = xclient
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
End Sub
Private Sub Read()
Try
RaiseEvent MessageRecieved(New StreamReader(Client.GetStream).ReadLine)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
Catch ex As Exception
RaiseEvent ClientExited(Me)
End Try
End Sub
Public Sub Send(ByVal Message As String)
sWriter = New StreamWriter(Client.GetStream)
sWriter.WriteLine(Message)
sWriter.Flush()
End Sub
End Class
I removed the other case statements to save room.
Here is the code for the client:
Public SS As String
Dim Server As New TcpClient
Dim Writer As StreamWriter
Private Sub Startup() Handles MyBase.Load
Dim System As Systemformation (its a sructure) = GetSystemInfo() (gets the info and puts it into a new structure)
SS = System broken in to text pieces for sending
Dim th As New Threading.Thread(AddressOf Connect)
th.IsBackground = True
th.Start()
End Sub
Private Sub Connect()
Do Until Server.Connected = True
Try
Server = New TcpClient("my remote ip", CInt("my port"))
Server.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
Catch
End Try
Loop
Send("0@" & SS)
End Sub
Public Sub Read(ByVal ar As IAsyncResult)
Try
Dim a As New StreamReader(Server.GetStream)
Dim str As String = a.ReadLine
Server.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
Catch
Dim th As New Threading.Thread(AddressOf Connect)
th.IsBackground = True
th.Start()
Exit Sub
End Try
End Sub
Private Sub Send(ByVal Str As String)
Try
Writer = New StreamWriter(Server.GetStream)
Writer.WriteLine(Str)
Writer.Flush()
Catch
End Try
End Sub
I have port forwarding all done and it works if I run the client and server on my own machine. If I run the client on another machine though It gives me a message box saying that it connected with the clients remote IP and at the same time it tells me it disconnected. The client never sends the system information. What is the problem?
Thank you in advance