• Jueves 2 de Mayo de 2024, 05:45

Autor Tema:  Cliente Servidor Sockets???  (Leído 2129 veces)

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Cliente Servidor Sockets???
« en: Lunes 3 de Marzo de 2008, 15:37 »
0
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

  • Miembro de PLATA
  • *****
  • Mensajes: 1076
    • Ver Perfil
Re: Cliente Servidor Sockets???
« Respuesta #1 en: Martes 4 de Marzo de 2008, 00:14 »
0
aqui hay unos ejemplos uqe puedes descargar.

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


BYTE
______________________________________________________________________________________
"No estoy de acuerdo con lo que dices, pero defenderé con mi vida tu derecho a expresarlo"

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Re: Cliente Servidor Sockets???
« Respuesta #2 en: Martes 4 de Marzo de 2008, 01:27 »
0
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

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




FORM1
Código: Text
  1.  
  2.  
  3. Imports System.Net.Sockets
  4. Imports System.Net
  5. Public Class Form1
  6.     Const portN As Integer = 7000
  7.     Dim localAdd As IPAddress = IPAddress.Parse(&#34;127.0.0.1&#34;)
  8.     Dim listener As New TcpListener(localAdd, portN)
  9.  
  10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.         TextBox1.AppendText(&#34;Ready...&#34;)
  12.         listener.Start()
  13.         While True
  14.             Dim user As New BServer(listener.AcceptTcpClient) // ERROR AQUI T-T
  15.         End While
  16.     End Sub
  17. End Class
  18.  
  19.  
  20.  


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