visualbasic tcp how to write line by line - vb.net

I want to make a program where another user can send data to a pc.
I need a username and password, but the problem is I don't find code to accept 2 tcp data stuff.
I have the variables password and username, but when I start all the text is read into the username variable. I need it to be split.
Here is my code.
Imports System.Net.Sockets
Imports System.Net
Imports MySql.Data.MySqlClient
Public Class Form1
Dim mysyqlcon As MySqlConnection
Dim COMMAND As MySqlCommand
Dim TCPServer As Socket
Dim TCPListenerz As TcpListener
Dim username As String
Dim password As String
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim rcvbytes(TCPServer.ReceiveBufferSize) As Byte
TCPServer.Receive(rcvbytes)
username = System.Text.Encoding.ASCII.GetString(rcvbytes)
Catch ex As Exception
End Try
System.Threading.Thread.Sleep(3000)
Try
Dim rcvbytes2(TCPServer.ReceiveBufferSize) As Byte
TCPServer.Receive(rcvbytes2)
password = System.Text.Encoding.ASCII.GetString(rcvbytes2)
Catch ex As Exception
End Try
Label1.Text = username
Label2.Text = password
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TCPListenerz = New TcpListener(IPAddress.Any, 1000)
TCPListenerz.Start()
TCPServer = TCPListenerz.AcceptSocket()
TCPServer.Blocking = False
Timer1.Enabled = True
End Sub
End Class

Related

Save text on my.settings saved multiple times vb.net

i have start a new application to encrypt strings and save them on my settings the code its work fine but wen it save it save multiple times the same.
How can i do for save all the content from a listbox to my settings with out to repeat.
This is my code
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
Well i just solved my self the problem its because wen i start the timer its is there the code to retrieved all the content from my settings to the listbox.
I just removed and solved.

How do get only available ssid with visual basic 2010

Sorry for bad english
I want the available ssid list in listbox
I am use api and its work but It stopped working after a while.And not see current wifi list.
My code:
Imports deneme2.NativeWifi
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub scan()
On Error Resume Next
Dim client As New WlanClient
ListView1.Items.Clear()
For Each wlanIface As WlanClient.WlanInterface In client.Interfaces
Dim networks() As Wlan.WlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0)
For Each network As Wlan.WlanAvailableNetwork In networks
Dim ssid As Wlan.Dot11Ssid = network.dot11Ssid
Dim networkName As String = Encoding.ASCII.GetString(ssid.SSID, 0, CType(ssid.SSIDLength, Integer))
Dim item As ListViewItem = New ListViewItem(networkName)
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString())
item.SubItems.Add(network.wlanSignalQuality.ToString + "%")
ListView1.Items.Add(item)
Next
Next
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
scan()
End Sub
End Class

VB.NET - Working with Multiple Forms

I'm trying to make a login window and a main window. The problem is when I close the login form and show the main window, the whole program stops.
The login form:
Imports System.IO
Imports System.Text
Imports System.Net
Public Class frmLogin
Dim address As String = "http://puu.sh/jKJ**Zq/d613de****29.txt"
Dim client As WebClient = New WebClient()
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'frmMain.Close()
End Sub
Private Sub frmLogin_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
frmMain.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If attemptLogin() Then
MsgBox("Login Successful")
Me.Close()
'frmMain.Show()
Else
MsgBox("Username or password is incorrect")
End If
End Sub
Private Function attemptLogin()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim line As String
Dim username As String
Dim password As String
line = reader.ReadLine()
Do While Not line Is Nothing
username = line.Split(":")(0)
password = line.Split(":")(1)
If (username = TextBox1.Text And password = TextBox2.Text) Then
Return True
End If
line = reader.ReadLine()
Loop
reader.Close()
client.Dispose()
Return False
End Function
Private Function Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim line As String
line = reader.ReadLine
Do While Not line Is Nothing
line = reader.ReadLine()
Loop
reader.Close()
Dim writer As StreamWriter = New StreamWriter(client.OpenRead(address))
writer.Write(TextBox1.Text & ":" & TextBox2.Text)
writer.Close()
client.Dispose()
Return False
End Function
End Class
The Main Window:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
'frmMain.Close()
End Sub
Private Sub frmMain_closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
End Sub
End Class
I am fairly new to VB.NET so sorry about the lack of knowledge/ugly coding. I'm used to programming GUIs in Java.
You need to set your 'Shutdown mode' to 'When last form closes'.
You can find this setting by going to My project and then clicking on Application (the first tab).
You can then find the setting at the bottom.

VB.NET Remove Item in ComboBox after placing in ListBox

How can I remove item in comboBox after
I choose it and put into listBox.
Here's my code. Please help me.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class frmAdvancePayment
Private Sub frmAdvancePayment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstBillNum.Items.Clear()
Dim connection_string As String = "Data Source=.\sqlexpress;Initial Catalog=CreditAndCollection;Integrated Security=True"
Dim connection As New SqlConnection(connection_string)
connection.Open()
Dim sql As String = "select BillNum from tblBillingSched where Status ='Unpaid'"
Dim da As New SqlDataAdapter(sql, connection_string)
Dim dt As New DataTable
da.Fill(dt)
cmbBillNum.DataSource = dt
cmbBillNum.DisplayMember = "BillNum"
cmbBillNum.ValueMember = "BillNum"
connection.Close()
End Sub
Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
lstBillNum.Items.Add(cmbBillNum.SelectedValue)
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstBillNum.Items.Clear()
End Sub
End Class
it's what you expect ?
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Dim index As Integer = ComboBox1.SelectedIndex
ListBox1.Items.Add(ComboBox1.Items(index))
ComboBox1.Items.RemoveAt(index)
End Sub

How to send text throught pc's

Is there any way to send text throught PC's without a client and a server program? Just simply send text from a program to another.
Even though you don't want to use clients and servers, this is the easiest way to go.
The server is a server that runs in the command prompt, but runs in the background of your program. The server and client won't be visible in any way.
A simple answer with few lines of codes are TCP Communication. This uses the ip addresses of the both computers and establish a server/client connection.
Every communication needs something that hosts it, to achieve this you code the program to contain the following:
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
msgbox("This is the data recieved: " & Data)
End If
End If
End Sub
End Class
This will open a "TCPListener" on the localhost port 8000. Whenever a client sends data to the listener, the text of the textbox Textbox1 to the data sent.
To send data to the server, use the following code:
Option Explicit On
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim Client As TcpClient
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//Ip to the local or remote, forwarded server. 127.0.0.1 is localhost - the same machine.
Client = New TcpClient("127.0.0.1", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write("Hello World!")
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
This will, when Button1 is pressed, try to send the data/string "Hello World!" to the server.
This could be combined into one by having the application set up as following:
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
'Change the string
End If
TextBox1.Text = Data
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//This has to be the address to the remote
Client = New TcpClient("xx.xx.xx.xx", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(TextBox2.Text)
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
To extend this and make it useable in a real application, use a backgroundworker to simply make the server and client run on another thread.
If you don't want to use socket or pipes, i can only think of files, which is more pc to pc than program to program.