[RELEASE]Socket Server Class

Posts 1–11 of 11 · Page 1 of 1
[RELEASE]Socket Server Class
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....
Sockets Server.rarattachment deleted · 7 downloads before removal
thank you, Would you beable to release the source so i can convert it to c#?

but thanks!
Quote Originally Posted by cosconub View Post
thank you, Would you beable to release the source so i can convert it to c#?

but thanks!
Visual Basic and C# both compile to MSIL, and can be used together.
well you send me the link for this topic on msn but what does it do?
Clean And Approved.
It's a class, so easy to use.

Really this should be in Open source, I want to build a repository. So collecting anything Open Source would be great.
oh didnt know they Can be used together, this jus made my life 1 millon time easyier
Good release, helpful!
http://www.mpgh.net/forum/33-visual-...ient-only.html <--- open source Client Only TCP Socket :P Just Thought it might help also if you kno more about sockets :P

Also this is to help the OP Owner... AcE.bu50t Check that thread to learn how to use events instead of functions to recieve Data and shit
thx guys, btw: added source

Quote Originally Posted by Deto View Post
http://www.mpgh.net/forum/33-visual-...ient-only.html <--- open source Client Only TCP Socket :P Just Thought it might help also if you kno more about sockets :P

Also this is to help the OP Owner... AcE.bu50t Check that thread to learn how to use events instead of functions to recieve Data and shit
nice but, I luv using functions
events are better though for events because then as soon as the client recieves data from the server you event will raise with the data from the server instead of coding a loop to keep checkin the server for data :P
Quote Originally Posted by Deto View Post
events are better though for events because then as soon as the client recieves data from the server you event will raise with the data from the server instead of coding a loop to keep checkin the server for data :P
I still luv the functions.
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us