sockets

(1/4) > >>

flashnet:
Hola buenas, tengo un pequeño problema.
Quiero hacer un cliente al cual conecte a distintos servers simultaneamente y el comando que envie se envie a todos.
Hasta hay bien, el problema esta en que no consigo hacer varias conexiones a la ver solo una.
A ver si me podeis ayudar.

Gracias de antemano

aqui el codigo

Código:


Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading


Public Class Form1

    Private oTCPStream As Net.Sockets.NetworkStream
    Public Shared oTCP As New Net.Sockets.TcpClient()
    Private bytWriting As [Byte]()
    Private bytReading As Byte()

    Private oHebras As Thread


    Public Delegate Sub ParameterizedThreadStart(ByVal obj As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtNameFile.ReadOnly = True
        OpenFileDialog1.Filter = "File Txt |*.txt"
        OpenFileDialog1.Title = "Open File"
        OpenFileDialog1.Multiselect = False

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArchivo.Click

        ComboBox1.Items.Clear()

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtNameFile.Text = OpenFileDialog1.FileName

        End If
        If Not Trim(txtNameFile.Text) = "" Then
            Dim objReader As New StreamReader(txtNameFile.Text)
            Dim sLine As String = ""

            While sLine IsNot Nothing
                sLine = objReader.ReadLine()
                If sLine IsNot Nothing Then
                    ComboBox1.Items.Add(sLine)
                End If
            End While

            objReader.Close()

        End If

    End Sub

    Private Function ConnServ(ByRef VarIP) As Object

        If Not VarIP = "" Then
            Try
                System.Threading.Thread.Sleep(1500)

                oTCP.SendTimeout = 1500
                oTCP.Connect(Trim(VarIP), "8000")

                oTCPStream = oTCP.GetStream

                WriteData(Trim(txtUser.Text) & vbCrLf)
                System.Threading.Thread.Sleep(500)

                WriteData(Trim(txtPassword.Text) & vbCrLf)
                System.Threading.Thread.Sleep(500)

                WriteData("Hello Serv" & vbCrLf)
                System.Threading.Thread.Sleep(500)

                '  oTCPStream.Close()
                '  oTCP.Close()

            Catch ex As Exception
                MsgBox(ex.Message)

            End Try

        End If

        Return Nothing

    End Function

    Private Sub WriteData(ByVal sData As String)

        bytWriting = System.Text.Encoding.ASCII.GetBytes(sData)

        oTCPStream.Write(bytWriting, 0, bytWriting.Length)

    End Sub


    Private Sub btnConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConn.Click

        Dim I As Integer
        Dim NewIP As String


        For I = 0 To ComboBox1.Items.Count - 1
            NewIP = (ComboBox1.Items(I).ToString)

            Dim t As New Thread(DirectCast(Function() ConnServ(NewIP), ThreadStart))
            t.Start()


        Next
    End Sub
End Class


raul338:
No se puede conectar un socket cliente a varios servidores. Debes hacerte un array de sockets (o tcpClients) que conecten uno a cada servidor. Luego para mandarle los datos. Recorres ese arreglo enviando uno por uno

[D4N93R]:
Exacto, un socket por conexión, jjejej Raul deja el empeño con los sockets, FlashNet usa TcpClient :P es lo mismo pero más arreglado.. :D

Claro, amenos de que desees controlar mejor la conexión, pero no creo que sea tu caso.

Un saludo!

raul338:
Soy socket-Fan! JAJA.... es que desde que aprendi sockets (en vb6) lo uso de esa manera y me parece mas "comoda"

[D4N93R]:
:P A mi también me gusta hacer las cosas así. xD es mas divertido. mas crudo :D

Navegación

[0] Índice de Mensajes

[#] Página Siguiente