CLR: .Net / Mono / Boo / Otros CLR > VB .NET

 Cliente Servidor Sockets???

(1/1)

Blizknight:
Hola a todos
Queria hacer mi primera aplicacion cliente servidor pero no encontre un ejemplo basico que me oriente.
alguien me podria orientar ?
saludos

lencho:
aqui hay unos ejemplos uqe puedes descargar.

http://msdn2.microsoft.com/en-us/vs2005/aa718334.aspx


BYTE

Blizknight:
muchas gracias lencho.
Encontre un codigo simple que me ayudo a comprender la sintaxis
en la cual manejaba un server en consola, queria pasar este server a un form para ponerle mas opciones sin embargo no me salia ><
aqui les pego el codigo a ver si me dan una mano

les dejo el proyecto aca y las lineas abajo por si no lo quieren descargar
http://rapidshare.de/files/38737533/Socket.rar.html

tan solo quiero pasarlo a un form que no sea consola pero al ejecutar esta linea
el form se raya O.o


--- Citar ---  While True
            Dim user As New BServer(listener.AcceptTcpClient)
        End While
--- Fin de la cita ---

ojala me puedan ayudar
gracias de antemano =D  :smartass:




FORM1

--- Código: Text ---  Imports System.Net.SocketsImports System.NetPublic Class Form1    Const portN As Integer = 7000    Dim localAdd As IPAddress = IPAddress.Parse(&#34;127.0.0.1&#34;)    Dim listener As New TcpListener(localAdd, portN)     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        TextBox1.AppendText(&#34;Ready...&#34;)        listener.Start()        While True            Dim user As New BServer(listener.AcceptTcpClient) // ERROR AQUI T-T        End While    End SubEnd Class   

CLASE BServer.vb

--- Citar ---
Imports System.Net.Sockets

Public Class BServer
    '---contains a list of all the clients
    Public Shared AllClients As New Hashtable

    '---information about the client
    Private _client As TcpClient
    Private _clientIP As String
    Private _ClientNick As String

    '---used for sending/receiving data
    Private data() As Byte

    '---is the nick name being sent?
    Private ReceiveNick As Boolean = True

    Public Sub New(ByVal client As TcpClient)
        _client = client

        '---get the client IP address
        _clientIP = client.Client.RemoteEndPoint.ToString

        '---add the current client to the hash table
        AllClients.Add(_clientIP, Me)

        '---start reading data from the client in a separate thread
        ReDim data(_client.ReceiveBufferSize)
        _client.GetStream.BeginRead(data, 0, CInt(_client.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
        Console.Read()
    End Sub

    Public Sub SendMessage(ByVal message As String)
        Try
            '---send the text
            Dim ns As System.Net.Sockets.NetworkStream
            SyncLock _client.GetStream
                ns = _client.GetStream
            End SyncLock
            Dim bytesToSend As Byte() = _
                System.Text.Encoding.ASCII.GetBytes(message)
            ns.Write(bytesToSend, 0, bytesToSend.Length)
            ns.Flush()
        Catch ex As Exception
            'Console.WriteLine(ex.ToString)
            Form1.TextBox1.AppendText(ex.ToString)
        End Try
    End Sub

    Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
        '---read from client---
        Dim bytesRead As Integer
        Try
            SyncLock _client.GetStream
                bytesRead = _client.GetStream.EndRead(ar)
            End SyncLock
            '---client has disconnected
            If bytesRead < 1 Then
                AllClients.Remove(_clientIP)
                Broadcast(_ClientNick & " has left the chat.")
                Exit Sub
            Else
                '---get the message sent
                Dim messageReceived As String = _
                    System.Text.Encoding.ASCII. _
                    GetString(data, 0, bytesRead)
                '---client is sending its nickname
                If ReceiveNick Then
                    _ClientNick = messageReceived

                    '---tell everyone client has entered the chat
                    Broadcast(_ClientNick & " has joined the chat.")
                    ReceiveNick = False
                Else
                    '---broadcast the message to everyone
                    Broadcast(_ClientNick & ">" & messageReceived)
                End If
            End If
            '---continue reading from client
            SyncLock _client.GetStream
                _client.GetStream.BeginRead(data, 0, _
                   CInt(_client.ReceiveBufferSize), _
                   AddressOf ReceiveMessage, Nothing)
            End SyncLock
        Catch ex As Exception
            AllClients.Remove(_clientIP)
            Broadcast(_ClientNick & " has left the chat.")
        End Try
    End Sub

    Public Sub Broadcast(ByVal message As String)
        '---log it locally
        Form1.TextBox1.AppendText(message)
        'Console.WriteLine(message)
        Dim c As DictionaryEntry
        For Each c In AllClients
            '---broadcast message to all users
            CType(c.Value, BServer).SendMessage(message & vbCrLf)
        Next
    End Sub
End Class


--- Fin de la cita ---

Navegación

[0] Índice de Mensajes

Ir a la versión completa