Jump to content
Welcome, Guest
Existing user? Sign In

Sign In



Sign Up
The MatriX
  • Welcome To Ghbsys
  • CS GO Streaming Version is released. Have fun streaming while cheating!
  • Have a Payment Issue? Send us a Support ticket.
  • Make a thread if you need support or join our discord for live support.
  • Have Suggestions? Make a thread and you'll earn Ghbsys Points for implemented suggestions.
  • Join our discord to stay well connected! Don't forget to integrate your discord to the site
  • Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum

    Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum, like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be apart of [GHB] - GAmEhAcKbAsTaRdS Forum by signing in or creating an account.
    • Start new topics and reply to others
    • Subscribe to topics and forums to get email updates
    • Get your own profile page and make new friends
    • Send personal messages to other members.

    [Hilfe] Vb.Net Managed TCP Server & Client


    ~Beefy~
     Share

    Recommended Posts

    Hehe, ich habe aber auch ein Problem..

    Der Fehler sagt: "Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden"

     

    (Obwohl ich nur ungerne meine Source Poste) Hier ist sie:

    Imports System.Net.Sockets
    Imports System.IO
    Imports System.Net
    
    Imports System.Text
    Imports System.Threading
    
    
    '(C) Copyright 2010 - Felix Faust
    'Dieser Teil und alles darunter ist 100% von Felix Faust
    'Der Code darf nicht geklaut werden. Man darf ihn sich nur als Beispiel nehmen.
    '(C) Copyright 2010 - Felix Faust
    
    Public Class ManagedServer
    
    Public Structure Connection
    
    	Private _aKnownName As String
    	Private nStream As NetworkStream
    	Private wStream As StreamWriter
    	Private rStream As StreamReader
    
    	Public ReadOnly Property NetworkStream As NetworkStream
    		Get
    			Return nStream
    		End Get
    	End Property
    	Public ReadOnly Property WriterStream As StreamWriter
    		Get
    			Return wStream
    		End Get
    	End Property
    	Public ReadOnly Property ReaderStream As StreamReader
    		Get
    			Return rStream
    		End Get
    	End Property
    	Public Property KnownName As String
    		Get
    			Return _aKnownName
    		End Get
    		Set(ByVal value As String)
    			_aKnownName = value
    		End Set
    	End Property
    
    	Public Sub New(ByVal NetStream As NetworkStream, ByVal ConnectionName As String)
    		nStream = NetStream
    		wStream = New StreamWriter(nStream)
    		rStream = New StreamReader(nStream)
    		wStream.AutoFlush = True
    	End Sub
    
    	Public Sub Send(ByVal Data As String)
    		wStream.Write(Data)
    	End Sub
    End Structure
    
    Private nStream As NetworkStream
    Private wStream As StreamWriter
    Private rStream As StreamReader
    Private Server As TcpListener
    Private ServerIP As String
    Private ServerPort As Integer
    Private EndPoint As IPEndPoint
    
    Private Connected As Boolean
    Private mainthread As Threading.Thread
    Private cList As List(Of Connection)
    
    Private AllowAcceptingClients As Boolean = False
    Public ReadOnly Property Clients As List(Of Connection)
    	Get
    		Return cList
    	End Get
    End Property
    
    Public ReadOnly Property IsStarted As Boolean
    	Get
    		Return Connected
    	End Get
    End Property
    
    Public Sub New(ByVal IpAddress As String, ByVal Port As Integer)
    	cList = New List(Of Connection)
    	Try
    		ServerIP = IpAddress
    		ServerPort = Port
    		EndPoint = New IPEndPoint(Net.IPAddress.Parse(IpAddress), Port)
    	Catch ex As Exception
    		Throw New Exception("Es ist ein innerer Fehler bei der neudeklaration des Servers aufgetreten. Weitere Informationen in der" & _
    							   "InnerException.", New Exception(ex.Message))
    	End Try
    	Connected = False
    End Sub
    
    Public Event DataRecieved(ByVal Con As Connection, ByVal Data As String)
    Public Event ClientJoined(ByVal Con As Connection, ByVal ClientName As String)
    Public Event ClientLost(ByVal Con As Connection, ByVal ClientName As String)
    Public Event ConnectionLost(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    Public Sub SendToAllClients(ByVal Data As String)
    	For Each cl As Connection In cList
    		cl.Send(Data)
    	Next
    End Sub
    Public Sub Send(ByVal Con As Connection, ByVal Data As String)
    	Con.Send(Data)
    End Sub
    Public Sub StartServer()
    	Server = New TcpListener(EndPoint)
    	Server.Start()
    	If IsStarted = True Then
    		Throw New Exception("Server already started! You cannot start it , you have to stop it before you restart it.")
    		Exit Sub
    	End If
    	Connected = True
    End Sub
    Public Sub StopServer()
    	If IsStarted = False Then
    		Throw New Exception("Server is not started yet! You cannot stop the server before you had not started it.")
    		Exit Sub
    	End If
    	Server.Stop()
    	Connected = False
    End Sub
    
    Public Sub BeginAcceptingClients()
    	AllowAcceptingClients = True
    	Server.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAccept), Server)
    End Sub
    Public Sub EndAcceptingClients()
    	AllowAcceptingClients = False
    End Sub
    
    Private Delegate Sub EventHandlerDelegate(ByVal Con As Connection, ByVal Data As String)
    
    Private Sub DoAccept(ByVal ar As IAsyncResult)
    	' Try
    	If AllowAcceptingClients = False Then Exit Sub
    	With CType(ar.AsyncState, TcpListener).EndAcceptTcpClient(ar)
    		Dim NewCl As New Connection(.GetStream, "")
    		NewCl.KnownName = NewCl.ReaderStream.ReadLine
    		cList.Add(NewCl)
    		RaiseEvent ClientJoined(NewCl, NewCl.KnownName)
    
    		Dim Listener As New Threading.Thread(AddressOf ListenToConnection)
    		Listener.Start(NewCl)
    
    	End With
    	If AllowAcceptingClients = False Then Exit Sub
    	Server.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAccept), Server)
    	' Catch ex As Exception
    	'	 Throw New Exception("Beim abhören der Verbindung ist ein Fehler aufgetreten. Mehr in der InnerException.", ex)
    	' End Try
    
    End Sub
    Private Sub OnDatareceived(ByVal state1 As Object, ByVal state2 As Object)
    	RaiseEvent DataRecieved(state1, state2)
    End Sub
    Private Sub OnClientLost(ByVal state1 As Object, ByVal state2 As Object)
    	RaiseEvent ClientLost(state1, state1.KnownName)
    End Sub
    
    Private Sub ListenToConnection(ByVal con As Connection)
    	Do
    		Try
    			Dim Tmp As String = con.ReaderStream.ReadLine
    			Dim EHD As New EventHandlerDelegate(AddressOf OnDatareceived)
    			EHD.Invoke(con, Tmp)
    		Catch
    			Dim EHD As New EventHandlerDelegate(AddressOf OnClientLost)
    			EHD.Invoke(con, con.KnownName)
    			cList.Remove(con)
    		End Try
    	Loop
    End Sub
    
    End Class
    
    
    
    
    Public Class ManagedClient
    
    Private _aKnownName As String
    Private nStream As NetworkStream
    Private wStream As StreamWriter
    Private rStream As StreamReader
    Private _aClient As TcpClient
    Private ServerIP As String
    Private ServerPort As String
    Private Thread As Threading.Thread
    Private EndPoint As IPEndPoint
    
    Public Event DataRecieved(ByVal sender As System.Object, ByVal e As String)
    Public Event ConnectionLost(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    Public ReadOnly Property IsConnected As Boolean
    	Get
    		Return _aClient.Connected
    	End Get
    End Property
    
    Public ReadOnly Property NetworkStream As NetworkStream
    	Get
    		If _aClient.Connected = False Then
    			Return Nothing
    			Exit Property
    		End If
    		Return nStream
    	End Get
    End Property
    Public ReadOnly Property WriterStream As StreamWriter
    	Get
    		If _aClient.Connected = False Then
    			Return Nothing
    			Exit Property
    		End If
    		Return wStream
    	End Get
    End Property
    Public ReadOnly Property ReaderStream As StreamReader
    	Get
    		If _aClient.Connected = False Then
    			Return Nothing
    			Exit Property
    		End If
    		Return rStream
    	End Get
    End Property
    Public ReadOnly Property KnownName As String
    	Get
    		Return _aKnownName
    	End Get
    End Property
    
    Public Sub Send(ByVal Data As String)
    	If _aClient.Connected = False Then Exit Sub
    	wStream.Write(Data)
    End Sub
    
    Public Sub Connect()
    	_aClient.Connect(ServerIP, ServerPort)
    	nStream = _aClient.GetStream
    	rStream = New StreamReader(nStream)
    	wStream = New StreamWriter(nStream)
    	Send(_aKnownName)
    	wStream.AutoFlush = True
    	Thread = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf Listen))
    	Thread.Start()
    End Sub
    Public Sub Disconnect()
    	Thread.Abort()
    	_aClient.Close()
    End Sub
    
    Public Sub New(ByVal IP As String, ByVal Port As Integer)
    	_aKnownName = "Default Client"
    	If Port >= 65535 Then Throw New Exception("The port number is to high! Maximum Port number is 65535.", _
    	New Exception("New declaration of ManagedClient throwd an exception with the message: ""Port number is to high.""!"))
    
    	ServerIP = IP
    	ServerPort = Port
    
    	EndPoint = New IPEndPoint(IPAddress.Parse(IP), Port)
    
    	'Try
    	_aClient = New TcpClient(EndPoint)
    
    
    	'Catch ex As Exception
    	'Throw New Exception("An error occured while trying to connect to the server. More in the inner exception.", ex)
    	'End Try
    
    	'If _aClient.Connected = False Then
    	'Throw New Exception("An error occured while trying to connect to the server.", New Exception("Unknown Error"))
    	'End If
    
    
    End Sub
    Public Sub New(ByVal IP As String, ByVal Port As Integer, ByVal ClientName As String)
    	_aKnownName = "Default Client"
    	If ClientName = "" = False Then _aKnownName = ClientName
    	If Port >= 65535 Then Throw New Exception("The port number is to high! Maximum Port number is 65535.", _
    	New Exception("New declaration of ManagedClient throwd an exception with the message: ""Port number is to high.""!"))
    
    	ServerIP = IP
    	ServerPort = Port
    
    	Try
    		_aClient = New TcpClient(IP.ToString, Port)
    		nStream = _aClient.GetStream
    		rStream = New StreamReader(nStream)
    		wStream = New StreamWriter(nStream)
    		Send(_aKnownName)
    	Catch ex As Exception
    		Throw New Exception("An error occured while trying to connect to the server. More in the inner exception.", ex)
    	End Try
    
    	If _aClient.Connected = False Then
    		Throw New Exception("An error occured while trying to connect to the server.", New Exception("Unknown Error"))
    	End If
    
    	wStream.AutoFlush = True
    
    	Thread.Start()
    End Sub
    
    Private Sub Listen()
    	While _aClient.Connected
    		Dim Tmp As String = rStream.ReadLine
    		RaiseEvent DataRecieved(Me, Tmp)
    	End While
    	If _aClient.Connected = False Then RaiseEvent ConnectionLost(Me, New System.EventArgs())
    End Sub
    End Class
    '(C) Copyright 2010 - Felix Faust
    'Dieser Teil und alles darüber ist 100% von Felix Faust
    'Der Code darf nicht geklaut werden. Man darf ihn sich nur als Beispiel nehmen.
    '(C) Copyright 2010 - Felix Faust

    Edited by ~Beefy~
    Link to comment
    Share on other sites

    Guest
    This topic is now closed to further replies.
     Share

    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...