Heya guys,
I just finished a class library of sockets, and I'm releasing it here. This actually has all the more useful functions of a server side, and client side. You can send messages, check if you got any messages, etc...
There is a lil' example:
Code:
Imports Sockets_Server.SocketsServer
Module SSTest
Public Client As New ClientSide("localhost", 4475)
Public Server As New ServerSide(4475)
Sub Main()
End Sub
End Module
The current functions, properties of the client side are:
SendString(String)
SendInteger(Integer, Integer)
SendBytes(Byte())
GotString(String)
GotInteger(Integer, Integer)
GotBytes(Byte())
Property: HostName
Property: Port
The current functions, properties of the server side are:
SendString(String)
SendInteger(Integer, Integer)
SendBytes(Byte())
GotString(String)
GotInteger(Integer, Integer)
GotBytes(Byte())
Property: Port
**Note: All the functions got comments, so I think it's easy to know what are you going to do.**
Virus scan:
VirusTotal - Free Online Virus, Malware and URL Scanner
Full source:
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Namespace SocketsServer
Public Class ClientSide
Private PortNum As Integer
Private Host As String
''' <summary>
''' Gets the current port number of the server which is begin used by this client
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property Port As Integer
Get
Return PortNum
End Get
End Property
''' <summary>
''' Gets the current ip of the server which is begin used by this client
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property HostName As String
Get
Return Host
End Get
End Property
Public Sub New(ByVal hostname As String, ByVal port As Integer)
PortNum = port
hostname = Host
End Sub
Private TCPC As New TcpClient(Host, PortNum)
''' <summary>
''' Sends a string value via a socket to the remote host. ACE ROCKS!!!
''' </summary>
''' <param name="str"></param>
''' <remarks></remarks>
Public Sub SendString(ByVal str As String)
Dim s As Stream = TCPC.GetStream
If Not TCPC.Connected Then
Try
TCPC.Connect(Host, PortNum)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
s.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length)
End If
End Sub
''' <summary>
''' Sends a integer value via a socket to the remote host. ACE ROCKS!!!
''' </summary>
''' <param name="int"></param>
''' <remarks></remarks>
Public Sub SendInteger(ByVal int As Integer, ByVal count As Integer)
Dim s As Stream = TCPC.GetStream
If Not TCPC.Connected Then
Try
TCPC.Connect(Host, PortNum)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
s.Write(BitConverter.GetBytes(int), 0, count)
End If
End Sub
''' <summary>
''' Sends a array of bytes via a socket to the remote host. ACE ROCKS!!!
''' </summary>
''' <param name="b"></param>
''' <remarks></remarks>
Public Sub SendBytes(ByVal b() As Byte)
Dim s As Stream = TCPC.GetStream
If Not TCPC.Connected Then
Try
TCPC.Connect(Host, PortNum)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
s.Write(b, 0, b.Length)
End If
End Sub
''' <summary>
''' Gets if the client got the message (string) from the server or not. ACE ROCKS?
''' </summary>
''' <param name="str"></param>
''' <remarks></remarks>
Public Function GotString(ByVal str As String) As Boolean
Dim s As Stream = TCPC.GetStream
If s.Read(System.Text.ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length) Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' Gets if the client got a message (integer) from the server. ACE ROCKS?
''' </summary>
''' <param name="int"></param>
''' <param name="len"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GotInteger(ByVal int As Integer, ByVal len As Integer) As Boolean
Dim s As Stream = TCPC.GetStream
If s.Read(BitConverter.GetBytes(int), 0, len) Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' Gets if the client got a array of bytes from the server. ACE ROCKS?
''' </summary>
''' <param name="b"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GotBytes(ByVal b() As Byte) As Boolean
Dim s As Stream = TCPC.GetStream
If s.Read(b, 0, b.Length) Then
Return True
Else
Return False
End If
End Function
End Class
Public Class ServerSide
Private pn As Integer
Private TCPS As New TcpListener(pn)
''' <summary>
''' Gets the current port number of the server
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property Port As Integer
Get
Return pn
End Get
End Property
Public Sub New(ByVal port As Integer)
pn = port
TCPS.Start()
End Sub
''' <summary>
''' Sends a string message to all the clients
''' </summary>
''' <param name="str"></param>
''' <remarks></remarks>
Public Sub SendString(ByVal str As String)
Dim s As Socket = TCPS.AcceptSocket
Try
s.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(str))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
''' <summary>
''' Sends a integer value to all the clients
''' </summary>
''' <param name="int"></param>
''' <remarks></remarks>
Public Sub SendInteger(ByVal int As Integer)
Dim s As Socket = TCPS.AcceptSocket
Try
s.Send(BitConverter.GetBytes(int))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
''' <summary>
''' Sends a array of bytes to all the clients
''' </summary>
''' <param name="b"></param>
''' <remarks></remarks>
Public Sub SendBytes(ByVal b() As Byte)
Dim s As Socket = TCPS.AcceptSocket
Try
s.Send(b)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
''' <summary>
''' Gets if the server got or not a string message from the client(s)
''' </summary>
''' <param name="msg"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GotString(ByVal msg As String) As Boolean
Dim s As Socket = TCPS.AcceptSocket
If (s.Receive(System.Text.ASCIIEncoding.ASCII.GetBytes(msg))) Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' Gets if the server got or not a integer value from the client(s)
''' </summary>
''' <param name="int"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GotInt(ByVal int As Integer) As Boolean
Dim s As Socket = TCPS.AcceptSocket
If (s.Receive(BitConverter.GetBytes(int))) Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' Gets if the server got or not a array of bytes from the client(s)
''' </summary>
''' <param name="b"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GotBytes(ByVal b() As Byte) As Boolean
Dim s As Socket = TCPS.AcceptSocket
If (s.Receive(b)) Then
Return True
Else
Return False
End If
End Function
End Class
End Namespace
Thanks, and enjoy =)
P.S.
Report here any kind of bugs that you found, also I will add a log function later, cause now it's kinda late....
