Tengo este código maldito con dos formularios de un servidor y un cliente. Ayudadme por favor. Solo envía una sola vez desde el ultimo cliente que arranco.
Servidor:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text.Encoding
Public Class Form2
Inherits System.Windows.Forms.Form
Structure cliente
Dim hilo As Thread
Dim osck As Socket
End Structure
Private osck As Socket
Private escuchadortcp As TcpListener
Private hiloconexion As Thread
Private ht As New Hashtable
Dim ocliente As cliente
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
escuchadortcp = New TcpListener(IPAddress.Any, 15000)
escuchadortcp.Start()
hiloconexion = New Thread(AddressOf Escucharconexiones)
hiloconexion.Start()
End Sub
Sub Escucharconexiones()
While True
osck = escuchadortcp.AcceptSocket
ocliente = New cliente
ocliente.osck = osck
ocliente.hilo = New Thread(AddressOf escucharclientes)
SyncLock ht
ht.Add(osck.RemoteEndPoint, ocliente)
End SyncLock
ocliente.hilo.Start()
End While
End Sub
Sub escucharclientes()
While True
Dim adatos(1000) As Byte
ocliente.osck.Receive(adatos, adatos.Length, SocketFlags.None)
TextBox1.AppendText(ASCII.GetString(adatos))
enviardatos(adatos)
End While
End Sub
Sub enviardatos(ByVal adatos() As Byte)
Dim oide As IDictionaryEnumerator
oide = ht.GetEnumerator
While oide.MoveNext
CType(oide.Value, cliente).osck.Send(adatos, adatos.Length, SocketFlags.None)
End While
End Sub
End Class
Cliente:
Imports System.io
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text.Encoding
Public Class Form3
Inherits System.Windows.Forms.Form
Private clientetcp As TcpClient
Private hiloescucha As Thread
Private flujodatos As Stream
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
clientetcp = New TcpClient("compaq", 15000)
hiloescucha = New Thread(AddressOf Escuchaservidor)
hiloescucha.Start()
End Sub
Sub Escuchaservidor()
While True
Dim adatos(500) As Byte
clientetcp.GetStream.Read(adatos, 0, adatos.Length)
TextBox1.AppendText(ASCII.GetString(adatos))
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adatos() As Byte
adatos = ASCII.GetBytes(TextBox2.Text + ControlChars.NewLine)
clientetcp.GetStream.Write(adatos, 0, adatos.Length)
End Sub
End Class