How to send text throught pc's - vb.net

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.

Related

Freezing the main thread after the TCPListener start

Currently i am creating a simple chat app which using TCP my problem is when i click on the button for start the TCP the window is freezing and i cannot do nothing (and it remain freez even if i connected with a TCP client) I tried to put the TCP-server into another thread but somehow it this case it is not working and i cannot even connect with TCP-client
Here is my code
Sub TCPServerStart()
TCPListenerz = New TcpListener(IPAddress.Any, 1000)
TCPListenerz.Start()
TCPServer = TCPListenerz.AcceptSocket
Timer1.Start()
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Dim adddress As IPAddress = CType(TCPServer.RemoteEndPoint, IPEndPoint).Address
ConnectedTo.Text = adddress.ToString
End Sub
Private Sub TCP_connction_type_Click(sender As Object, e As EventArgs) Handles TCP_connction_type.Click
TCPServerStart
End Sub
Sub TCPServcerSub()
Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TCPTextbox.Text)
TCPServer.Send(sendbytes)
'' TCPServer.SendFile("")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim rcvbytes(TCPServer.ReceiveBufferSize) As Byte
TCPServer.Receive(rcvbytes)
TextBox4.Text = System.Text.Encoding.ASCII.GetString(rcvbytes)
End Sub ''TCP Recive
Thanks for any kind of help

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

visualbasic tcp how to write line by line

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

Wireless data recieving and sending using vb.net

I have 3 different computers in 3 different locations ,and i need to build a software that can control these 3 computers using vb.net,my basic need is to play a video on my server system ,and i need these 3 computers to play the same video at the same time , how can i send and recieve wireless data using vb.net
You can upload the video to any video hosting site and then use this code to send a message to the computers (using there public IP adress where stated):
Public Class MessageReciever
Dim Listener As New TcpListener(5534) 'or any unused port
Dim Client As TcpClient
Private Sub MessageReciever_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Listener.Stop()
End Sub
Private Sub MessageReciever_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
Listener.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim message As String
Dim nStart As Integer
Dim nLast As Integer
If Listener.Pending = True Then
message = ""
Client = Listener.AcceptTcpClient()
Dim reader As New StreamReader(Client.GetStream())
While reader.Peek > -1
message &= Convert.ToChar(reader.Read()).ToString
End While
If message.Contains("</>") Then
nStart = InStr(message, "</>") + 4
nLast = InStr(message, "<\>")
message = Mid(message, nStart, nLast - nStart)
End If
End If
If message = "playVideo" then
Process.start("http://youtube.com/watch?yourvideo") 'opens website in default browser
End If
End Sub
End Class
Import all the stuff it tells you to.
If you put this software on the computers you would like to play the video on and then put the following code on the main computer you would like to control the others on:
Option Explicit On
Public Class MessageSender
Dim client As TcpClient
Private Sub PlayVidButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayVidButton.Click
Try
client = New TcpClient(computer ur controllings public ip, 5534) 'you can get your public ip # portforward.com and make sure the port here is the same as the one entered for the listener in the other form
Dim writer As New StreamWriter(client.GetStream())
writer.Write("playVideo")
writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
I hope this helps your problem.

MultiThreading and Sockets/TCP [VB.NET]

I started learning about TCP/Sockets yesterday and decided to make a chatbox for a friend and I.
Unfortunately, i am having some difficulties with MultiThreading.
Whenever i am using it, i can no longer receive messages from my friend.
But, if i disable it then, everything works perfectly.
I don't know what's going on here, could somebody help?
Imports System.Net.Sockets
Imports System.Net
Public Class ServerClient
Dim _TCPServer As Socket
Dim _TCPListener As TcpListener
Dim _ListenerThread As System.Threading.Thread
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'When Submit is pressed, send some text to the client
Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
txtBox.AppendText(vbCrLf & "Server: " & txtInput.Text)
txtInput.Clear()
_TCPServer.Send(bytes)
End Sub
Private Sub TCPListen()
'If somebody calls port 2424, accept it, unblock the socket and start the timer
_TCPListener = New TcpListener(IPAddress.Any, 2424)
_TCPListener.Start()
_TCPServer = _TCPListener.AcceptSocket()
btnSend.Enabled = True
txtBox.AppendText("Connection Established" & vbCrLf)
_TCPServer.Blocking = False
_Timer.Enabled = True
End Sub
Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick
'If data has been sent, receive it
Try
Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
_TCPServer.Receive(rcvdbytes)
txtBox.AppendText(vbCrLf & "Client: " & System.Text.Encoding.ASCII.GetString(rcvdbytes) & vbCrLf)
Catch ex As Exception
End Try
End Sub
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Link the new thread to TCPListen(), allow access to all threads and wait for a call
_ListenerThread = New Threading.Thread(AddressOf TCPListen)
Control.CheckForIllegalCrossThreadCalls = False
txtBox.AppendText("Waiting for connection.." & vbCrLf)
btnSend.Enabled = False
_ListenerThread.Start()
End Sub
End Class
This example project contains four classes - TcpCommServer, TcpCommClient, clsAsyncUnbuffWriter and CpuMonitor. With these classes, you will not only be able to instantly add TCP/IP functionality to your VB.NET applications, but also has most of the bells and whistles we're all looking for. With these classes, you will be able to connect multiple clients to the server on the same port. You will be able to easily: throttle bandwidth to the clients, and send and receive files and data (text?) along 250 provided channels simultaneously on a single connection.
http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class
Well, i learned BackgroundWorkers could do the exact same thing and now it all works.
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Wait for a call
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
TCPListen()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
_Timer.Enabled = True
End Sub