João Simões 0 Posted April 9, 2019 Report Share Posted April 9, 2019 (edited) Bom dia , estou a trabalhar num projeto no qual preciso de receber informação de diferentes ESP8266 e através de uma aplicação trabalhar esses dados . Estou a pensar em fazer utilizando Sockets ,ja procurei bastante e tenho um código fucnional , mas que apenas recebe informação de um cliente.E queria saber se alguem me pode ajudar a saber que alteração posso fazer a este código de forma a receber dados de mais de uma ESP. O código base da comunicação é o seguinte: Imports System.Net Imports System.Net.Sockets Imports System.IO Imports System.Text Imports System.Threading Public Class Form1 Dim TCPServer As Socket Dim TCPListener As TcpListener Dim ip As String 'Public ipserver As IPAddress = IPAddress.Parse("127.0.0.1") Public IsListening As Boolean = True Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load TCPListener = New TcpListener(IPAddress.Any, 80) 'esp TCPListener.Start() TCPServer = TCPListener.AcceptSocket TCPServer.Blocking = False Timer1.Enabled = True End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Try Dim Rcvbytes(TCPServer.ReceiveBufferSize) As Byte TCPServer.Receive(Rcvbytes) TextBox2.Text = (System.Text.Encoding.ASCII.GetString(Rcvbytes)) Catch ex As Exception End Try End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim sendbytes() As Byte = (System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)) TCPServer.Send(sendbytes) TextBox1.Clear() End Sub End Class Obrigado;) Edited April 10, 2019 by João Simões Formatação de código. Link to post Share on other sites
M6 149 Posted April 10, 2019 Report Share Posted April 10, 2019 Vê lá se isto ajuda: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener.beginacceptsocket?view=netframework-4.7.2 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to post Share on other sites
João Simões 0 Posted April 10, 2019 Author Report Share Posted April 10, 2019 Obrigado pela ajuda, mas deixa ver se percebi o que aquilo faz ... o Código abre e fecha a comunicação com o cliente? Como se desligasse e ligasse o servidor? Link to post Share on other sites
M6 149 Posted April 10, 2019 Report Share Posted April 10, 2019 Isso define uma callback para ser executada de forma assíncrona. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now