Sono arrivato a questo punto:
ho fatto un Server e un Client che comunicano tra loro e si scambiano i messaggi, solo che i messaggi di ciascuno non partono fino a che l'altro non ne invia uno a sua volta , come mai?
io vorrei arrivare al punto che creo una chat vera e propria, ma sensa nessuna funzione particolare solo per parlare.
Potete dare un occhiata o meglio copiare i progetti ed eseguirli per vedere dove sbaglio???
GRAZIE MILLE !!!
Server
codice:
Imports System.Net.Sockets
Imports System.Text
Public Class TCPSrv
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
Friend WithEvents btnInvia As System.Windows.Forms.Button
Friend WithEvents txtMessaggio As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
Friend WithEvents btnDisconnect As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnDisconnect = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.btnInvia = New System.Windows.Forms.Button()
Me.txtDisplay = New System.Windows.Forms.TextBox()
Me.txtMessaggio = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'btnDisconnect
'
Me.btnDisconnect.Location = New System.Drawing.Point(8, 96)
Me.btnDisconnect.Name = "btnDisconnect"
Me.btnDisconnect.Size = New System.Drawing.Size(80, 24)
Me.btnDisconnect.TabIndex = 7
Me.btnDisconnect.Text = "Disconnetti"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(8, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(59, 13)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Messaggio"
'
'btnInvia
'
Me.btnInvia.Location = New System.Drawing.Point(240, 56)
Me.btnInvia.Name = "btnInvia"
Me.btnInvia.Size = New System.Drawing.Size(40, 24)
Me.btnInvia.TabIndex = 0
Me.btnInvia.Text = "Invia"
'
'txtDisplay
'
Me.txtDisplay.Location = New System.Drawing.Point(64, 8)
Me.txtDisplay.Multiline = True
Me.txtDisplay.Name = "txtDisplay"
Me.txtDisplay.Size = New System.Drawing.Size(168, 40)
Me.txtDisplay.TabIndex = 5
Me.txtDisplay.Text = ""
'
'txtMessaggio
'
Me.txtMessaggio.Location = New System.Drawing.Point(64, 56)
Me.txtMessaggio.Name = "txtMessaggio"
Me.txtMessaggio.Size = New System.Drawing.Size(168, 20)
Me.txtMessaggio.TabIndex = 3
Me.txtMessaggio.Text = ""
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(24, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(41, 13)
Me.Label1.TabIndex = 8
Me.Label1.Text = "Display"
'
'TCPSrv
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 125)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtDisplay, Me.Label1, Me.btnDisconnect, Me.txtMessaggio, Me.Label2, Me.btnInvia})
Me.Location = New System.Drawing.Point(10, 10)
Me.Name = "TCPSrv"
Me.Text = "Server"
Me.ResumeLayout(False)
End Sub
#End Region
'Must listen on correct port- must be same as port client wants to connect on.
Const portNumber As Integer = 8000
Dim tcpListener As New tcpListener(portNumber)
Private Sub TCPSrv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tcpListener.Start()
End Sub
Private Sub btnInvia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvia.Click
Dim responseString As String = txtMessaggio.Text
'Accetta la connessione in corso del Client e restituisce TcpClient inizializzaro per la comunicazione.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
'Invia il flusso
Dim networkStream As NetworkStream = tcpClient.GetStream()
'legge il flusso nell'array "byte"
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
'visualizza i dati trasmessi dal Client
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
txtDisplay.Text = clientdata
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
txtMessaggio.Text = Nothing
'Chiude TcpClient.
tcpClient.Close()
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
'Chiude TcpListener
tcpListener.Stop()
MsgBox("Connessione interrotta!")
End Sub
End Class
Client
codice:
Imports System.Net.Sockets
Imports System.Text
Public Class TCPCli
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
Friend WithEvents btnInvia As System.Windows.Forms.Button
Friend WithEvents txtMessaggio As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtIP As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtDisplay = New System.Windows.Forms.TextBox()
Me.btnInvia = New System.Windows.Forms.Button()
Me.txtMessaggio = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.txtIP = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(8, 104)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(59, 13)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Messaggio"
'
'txtDisplay
'
Me.txtDisplay.Location = New System.Drawing.Point(64, 48)
Me.txtDisplay.Multiline = True
Me.txtDisplay.Name = "txtDisplay"
Me.txtDisplay.Size = New System.Drawing.Size(168, 48)
Me.txtDisplay.TabIndex = 5
Me.txtDisplay.Text = ""
'
'btnInvia
'
Me.btnInvia.Location = New System.Drawing.Point(240, 104)
Me.btnInvia.Name = "btnInvia"
Me.btnInvia.Size = New System.Drawing.Size(40, 24)
Me.btnInvia.TabIndex = 0
Me.btnInvia.Text = "Invia"
'
'txtMessaggio
'
Me.txtMessaggio.Location = New System.Drawing.Point(64, 104)
Me.txtMessaggio.Name = "txtMessaggio"
Me.txtMessaggio.Size = New System.Drawing.Size(168, 20)
Me.txtMessaggio.TabIndex = 3
Me.txtMessaggio.Text = ""
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(48, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(15, 13)
Me.Label1.TabIndex = 6
Me.Label1.Text = "IP"
'
'txtIP
'
Me.txtIP.Location = New System.Drawing.Point(64, 8)
Me.txtIP.Name = "txtIP"
Me.txtIP.Size = New System.Drawing.Size(168, 20)
Me.txtIP.TabIndex = 7
Me.txtIP.Text = ""
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(24, 48)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(41, 13)
Me.Label3.TabIndex = 8
Me.Label3.Text = "Display"
'
'TCPCli
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 141)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtIP, Me.txtDisplay, Me.Label3, Me.Label1, Me.txtMessaggio, Me.Label2, Me.btnInvia})
Me.Name = "TCPCli"
Me.Text = "Client"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnInvia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvia.Click
Dim messaggio As String = txtMessaggio.Text
Dim IP As String = txtIP.Text
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect(IP, 8000)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
'Faccio una semplice scrittura.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(messaggio)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'lettura del NetworkStream nel byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
'visualizzazione dei dati inviati dal Server
Dim returndata As String = Encoding.ASCII.GetString(bytes)
txtDisplay.Text = returndata
txtMessaggio.Text = Nothing
Else
If Not networkStream.CanRead Then
MsgBox("Non posso leggere i dati da questo flusso")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
MsgBox("Non posso leggere i dati da questo flusso")
tcpClient.Close()
End If
End If
End If
End Sub
End Class