Poblema con socket

sabato 26 febbraio 2005 - 18.30

Filabula Profilo | Newbie

Salve a tutti
Sto realizzando una chat in vb.net, il problema sta nell'invio di dati da parte del server Il realizata la connessione il client invia dei dati al server che li dovrebbe rigirare nuovamente al client, ma appena appena chiamo il metodo endsend non succede nulla sul client a meno che sul server dopo aver chiamato il metodo endsend chiamo il metodo Shutdown che svuota i buffer e resetta la connessione.
Da questo ho dedotto che il prob è che il metodo endsend incanala sul buffer i dati e non li invia.

Sapete come posso risolvere il problema??

Filabula Profilo | Newbie

ecco il codice Server:
''/// ==============================================================
''/// Socket Server Asincrono
''/// ==============================================================
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Windows.Forms


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Codice generato da Progettazione Windows Form "

Public Sub New()
MyBase.New()

'Chiamata richiesta da Progettazione Windows Form.
InitializeComponent()

'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()

End Sub

'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Richiesto da Progettazione Windows Form
Private components As System.ComponentModel.IContainer

'NOTA: la procedura che segue è richiesta da Progettazione Windows Form.
'Può essere modificata in Progettazione Windows Form.
'Non modificarla nell'editor del codice.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

' Entrata dei dati dal client.
Public Shared data As String = Nothing

' Inizializza il segnale di attesa per il thread su non segnalato
Public Shared allDone As New ManualResetEvent(False)

Public Shared Sub StartListening()
' Buffer di ingresso per i dati.
Dim bytes() As Byte = New [Byte](1024) {}

' Stabilisce l'endpoint locale per ilsocket.
' Il DNS name per il computer
' Avvia l'ascolto.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 11000)

' Intializes a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)

' Bind the socket to the local endpoint and listen for incoming
' connections.
Try
listener.Bind(localEndPoint)
listener.Listen(100)

While True
' Set the event to nonsignaled state.
allDone.Reset()

' Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...")
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
listener)

' Wait until a connection is made before continuing.
allDone.WaitOne()
End While

Catch e As Exception
Console.WriteLine(e.ToString())
End Try

Console.WriteLine(ControlChars.Cr + "Press ENTER to continue...")
Console.Read()
End Sub 'StartListening
Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
' Signal the main thread to continue.
allDone.Set()

' Get the socket that handles the client request.
Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)
Console.WriteLine("Socket connected to {0}", _
handler.RemoteEndPoint.ToString())

' Create the state object.
Dim state As New StateObject
stat

Filabula Profilo | Newbie

e questo è il client:
''/// ==============================================================
''/// Socket Client Asincrono
''/// ==============================================================
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Codice generato da Progettazione Windows Form "

Public Sub New()
MyBase.New()

'Chiamata richiesta da Progettazione Windows Form.
InitializeComponent()

'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()

End Sub

'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Richiesto da Progettazione Windows Form
Private components As System.ComponentModel.IContainer

'NOTA: la procedura che segue è richiesta da Progettazione Windows Form.
'Può essere modificata in Progettazione Windows Form.
'Non modificarla nell'editor del codice.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(16, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(160, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(200, 32)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

' The port number for the remote device.
Private Shared port As Integer = 11000

' ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)

' The response from the remote device.
Private Shared response As [String] = [String].Empty

Dim client As Socket
Private Sub StartClient()
' Connect to a remote device.
Try
' Establish the remote endpoint for the socket.
' The name of the
' remote device is "host.contoso.com".
Dim ipHostInfo As IPHostEntry = Dns.Resolve("110.111.112.115")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, port)

' Create a TCP/IP socket.
client = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)

' Connect to the remote endpoint.
client.BeginConnect(remoteEP, AddressOf ConnectCallback, client)
connectDone.WaitOne()

'' Send test data to the remote device.
'Send(client, "This is a test<EOF>")
'sendDone.WaitOne()

freeteo Profilo | Guru

ciao,
per realizzare applicazioni di questo tipo io ti consiglio di usare 1po di .net remoting che gestisci molto meglio anche se non è semplicissimo...
pero ti do 1paio di link che ti possono aiutare con esempi:
http://www.codeproject.com/dotnet/dotnetchatapplication.asp
http://www.c-sharpcorner.com/Internet/remoting_chat_server2.asp

se non ricordo male cera anche 1o di microsoft stessa ma adesso non lo trovo piu :-(

ciao

Filabula Profilo | Newbie

Ok grazie me li guardo subito
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5