ho realizzato un programmino che mi permette di inviare messaggi da smartphone wm a pc tramite comunicazione socket (lo smartphone è connesso a pc tramite activesync con connessione bluetooth). il problema è che appna provo ad inviare il messaggio al pc lo smartphone lo invia correttamente, mentre il software lato pc (da me creato) non lo riceve. come mai secondo voi?
ecco il software lato pc:
codice:
Dim host As IPAddress
Dim mySock As Socket
Dim ip As IPHostEntry
Dim ep As IPEndPoint
Dim listener As TcpListener
Private Sub SocketListen()
Try
'Viene preso l’IP della macchina locale
ip = Dns.GetHostEntry(Dns.GetHostName())
host = IPAddress.Parse(ip.AddressList(0).ToString())
MsgBox("ip:" & host.ToString, MsgBoxStyle.OkOnly)
'Creato l’EndPoint sulla porta 8200 (Porta di ascolto)
ep = New IPEndPoint(host, CInt(TextBox1.Text))
'Dichiarato il Socket di tipo TCP
mySock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'Dichiarata variabile di tipo TCPListener per far partire l’ascolto
listener = New TcpListener(ep)
listener.Start()
While True
'Aspetto una connessione
mySock = listener.AcceptSocket()
'Quando arriva la connessione chiamo la funzione Elabora Messaggio passando il socket
ElaboraMessaggio()
End While
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Private Sub ElaboraMessaggio()
Dim buffer(1024) As Byte
Dim ByteDaLeggere As Integer
Try
'Controllo se il socket è ancora connesso
If mySock.Connected Then
'Ricavo la lunghezza dei byte da leggere. Qua viene caricato anche il buffer
ByteDaLeggere = mySock.Receive(buffer)
'Faccio l’encoding del buffer per ricavare la stringa
Dim ritorno As String = Encoding.ASCII.GetString(buffer)
'Scrivo il messaggio letto
Console.WriteLine(ritorno.TrimEnd())
MsgBox(ritorno.TrimEnd(), MsgBoxStyle.OkOnly)
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
GroupBox1.Enabled = False
SocketListen()
End Sub