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.htmltan solo quiero pasarlo a un form que no sea consola pero al ejecutar esta linea
el form se raya O.o
While True
Dim user As New BServer(listener.AcceptTcpClient)
End While
ojala me puedan ayudar
gracias de antemano =D
FORM1
Imports System.Net.Sockets
Imports System.Net
Public Class Form1
Const portN As Integer = 7000
Dim localAdd As IPAddress = IPAddress.Parse("127.0.0.1")
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("Ready...")
listener.Start()
While True
Dim user As New BServer(listener.AcceptTcpClient) // ERROR AQUI T-T
End While
End Sub
End Class
CLASE BServer.vb
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