Heya guys!
Today I started to learn socket programing and I found it really interesting.
Now I will show some of you guys something about the sockets. First of all we need to know what are the sockets and how they work.
1: What are sockets?
2: Sockets were development on UNIX for the first time, they are used to send a data between 2 or more machines.
1: How they work?
2: Well, let's take for example 2 phones, 1 is the phone that will call, and the other one is that one that will get the call. In sockets:
1st Phone -- Client Machine
2nd Phone -- Listener Machine
When the call begins and the second phone accept the call the voice will be transfer by packet data. In our case the packet data is the socket. If you want to call the 2nd phone you need the phone number and the prefix. In socket programing the number is the IP of the machine and the prefix is the port.
Microsoft Visual Basic has a namespace dedicated to Windows Sockets (WinSock).
Code:
Imports System.Net.Sockets
Now let's start...
Make 2 Console applications (1: Listener, 2: Client).
First we have to create our listener by putting in:
Code:
Dim x As New TcpListener(458) '458 is the current port.
Now the listener is ready... Before creating our socket we need to make sure that our listener is listening by putting the command
Start(). ( x.Start() )
Well now the listener is ready, and we need the socket.
Code:
Dim s As Socket = x.AcceptSocket
The data that the socket will transfer is a array of bytes, but we want to send a message (String), so we have to convert the string into a array of bytes.
Code:
Dim ace As String = "Hello server!"
If s.Receive(System.Text.ASCIIEncoding.ASCII.GetBytes(ace)) Then
Dim r As String = "Heya!"
Console.WriteLine("Got a message from the client! (" & ace & ")")
s.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(r))
End If
This will pretty much wait until the client sends the message and then the server will respond with "Heya!".
Server App:
Code:
Imports System.Net.Sockets
Module Module1
Sub Main()
Dim x As New TcpListener(458)
Console.WriteLine("Waiting for the message.....")
x.Start()
Dim s As Socket = x.AcceptSocket
Dim ace As String = "Hello server!"
If s.Receive(System.Text.ASCIIEncoding.ASCII.GetBytes(ace)) Then
Dim r As String = "Heya!"
Console.WriteLine("Got a message from the client! (" & ace & ")")
s.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(r))
End If
Console.ReadKey()
End Sub
End Module
Now that we have our server we need the client. We are going to work into machines that are connected at the same network, so into the hostname we have to put
localhost, and for the port the same port as in the listener.
Code:
Dim x As New TcpClient("localhost", 458)
The client will automatically connect to the server and got the sockets. To read/write the socket(s) we need the function
GetStream().
Code:
Dim s As System.IO.Stream = x.GetStream
Well, since we want to send the first socket from the client to the server we need to call the
Write() function of the stream.
Code:
Dim ace As String = "Hello server!"
*****ndBufferSize = ace.Length
If *****nnected Then
s.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(ace), 0, ace.Length)
End If
After that the stream will write into the socket our message (
Hello server!), and the client will directly send the socket to the server.
Remember? At our server code we did put...
Code:
If s.Receive(System.Text.ASCIIEncoding.ASCII.GetBytes(ace)) Then
Dim r As String = "Heya!"
Console.WriteLine("Got a message from the client! (" & ace & ")")
s.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(r))
End If
So now that the client did send the socket with the specific value (String), the listener will reply with "Heya!".
So we need to put in our client code...
Code:
If s.Read(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length) Then
Console.WriteLine("Server says: " & str)
End If
Client Code:
Code:
Imports System.Net.Sockets
Module Module1
Sub Main()
Dim x As New TcpClient("localhost", 458)
Dim s As System.IO.Stream = x.GetStream
Dim ace As String = "Hello server!"
*****ndBufferSize = ace.Length
If *****nnected Then
s.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(ace), 0, ace.Length)
End If
Dim str As String = "Heya!"
If s.Read(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length) Then
Console.WriteLine("Server says: " & str)
End If
Console.ReadKey()
End Sub
End Module
NOTE: First run the server app, and then the client one....
Now run and test!! =)
The server will say:
Waiting for the message.....
Got a message from the client! (Hello server!)
and the client will write:
Thx for reading and I hope that you did learn something new today! =)