tedhead2
March 8th, 2011, 11:36
Sockets are fun, so this is a basic way to use them.
Client code:
You need 1 button , 1 label, and 1 multi-lined textbox.
Imports System.Net.Sockets
Imports System.Text
Public Partial Class MainForm
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Public Sub New()
Me.InitializeComponent()
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
msg("Client Started")
clientSocket.Connect("127.0.0.1", 5555)
label1.Text = "Client Socket Program - Server Connected ..."
End Sub
Sub msg(ByVal mesg As String)
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg
End Sub
End Class
Server(This is a windows CONSOLE application):
Imports System.Net.Sockets
Imports System.Text
Imports System.Console
Module Program
Sub Main()
Dim serverSocket As New TcpListener(5555)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
msg("Server started")
clientSocket = serverSocket.AcceptTcpClient()
msg("Accepted connection from client")
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = _
System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("Data from client - " + dataFromClient)
Dim serverResponse As String = _
"Server response " + Convert.ToString(requestCount)
Dim sendBytes As [Byte]() = _
Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
End Module
Run the SERVER then the CLIENT, and click the button on the client to send data.
You could have a java server and a vb.net client, or a vb.net server with a c++ client, it works no matter what as long as it uses sockets.
Client code:
You need 1 button , 1 label, and 1 multi-lined textbox.
Imports System.Net.Sockets
Imports System.Text
Public Partial Class MainForm
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Public Sub New()
Me.InitializeComponent()
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
msg("Client Started")
clientSocket.Connect("127.0.0.1", 5555)
label1.Text = "Client Socket Program - Server Connected ..."
End Sub
Sub msg(ByVal mesg As String)
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg
End Sub
End Class
Server(This is a windows CONSOLE application):
Imports System.Net.Sockets
Imports System.Text
Imports System.Console
Module Program
Sub Main()
Dim serverSocket As New TcpListener(5555)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
msg("Server started")
clientSocket = serverSocket.AcceptTcpClient()
msg("Accepted connection from client")
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = _
System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("Data from client - " + dataFromClient)
Dim serverResponse As String = _
"Server response " + Convert.ToString(requestCount)
Dim sendBytes As [Byte]() = _
Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
End Module
Run the SERVER then the CLIENT, and click the button on the client to send data.
You could have a java server and a vb.net client, or a vb.net server with a c++ client, it works no matter what as long as it uses sockets.