Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
Visual Studio .NET
VS.NET General Discussion
Convert a simple client/server app from sync to async
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Bokka" data-source="post: 183499" data-attributes="member: 67918"><p>Hello!</p><p></p><p></p><p>I ask you if someone could help me out. I wrote a couple of programs (a client and a server) that communicate with each other. Something really basic. The problem, however, is that being a synchronous communication, the server starts only after it has received the package from the client and ends the communication. I would therefore like to make this program (server) asynchronous, to be able to make the best use of it. Here is the code: </p><p></p><p>SERVER:</p><p></p><p>[CODE=vbnet]Imports System.Net</p><p>Imports System.IO</p><p>Imports System.Net.Sockets</p><p>Public Class Form1</p><p></p><p> Dim aa As SocketAsyncOperation</p><p> Dim ip As IPAddress = IPAddress.Parse(“127.0.0.1”)</p><p> Dim tl As TcpListener = New TcpListener(ip, 1234)</p><p> Dim ns As NetworkStream</p><p> Dim br As BinaryReader</p><p> Dim bw As BinaryWriter</p><p> Dim soc As Socket</p><p></p><p> Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load</p><p></p><p></p><p> Label1.Text = "ok"</p><p> tl.Start()</p><p> Label1.Text = "Server started"</p><p> soc = tl.AcceptSocket</p><p> ns = New NetworkStream(soc)</p><p> bw = New BinaryWriter(ns)</p><p> br = New BinaryReader(ns)</p><p> Dim s As String = br.ReadString</p><p> bw.Write(s)</p><p></p><p> End Sub</p><p>End Class[/CODE]</p><p></p><p>CLIENT:</p><p></p><p>[CODE=vbnet]Imports System.Net.Sockets</p><p>Imports System.IO</p><p></p><p>Public Class Form1</p><p></p><p> Dim tc As TcpClient = New TcpClient()</p><p> Dim ns As NetworkStream</p><p> Dim br As BinaryReader</p><p> Dim bw As BinaryWriter</p><p></p><p> Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load</p><p></p><p></p><p></p><p> End Sub</p><p></p><p> Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click</p><p></p><p> tc.Connect(“127.0.0.1”, 1234)</p><p> ns = tc.GetStream</p><p> br = New BinaryReader(ns)</p><p> bw = New BinaryWriter(ns)</p><p> bw.Write(TextBox1.Text)</p><p> TextBox2.Text = br.ReadString</p><p></p><p> End Sub</p><p>End Class[/CODE]</p><p></p><p></p><p></p><p>I saw that in vb.net there is the function called "SocketAsync" but I didn't find any example to follow ... </p><p></p><p>Please, someone could help me?</p><p></p><p>Thanks in advance!!</p></blockquote><p></p>
[QUOTE="Bokka, post: 183499, member: 67918"] Hello! I ask you if someone could help me out. I wrote a couple of programs (a client and a server) that communicate with each other. Something really basic. The problem, however, is that being a synchronous communication, the server starts only after it has received the package from the client and ends the communication. I would therefore like to make this program (server) asynchronous, to be able to make the best use of it. Here is the code: SERVER: [CODE=vbnet]Imports System.Net Imports System.IO Imports System.Net.Sockets Public Class Form1 Dim aa As SocketAsyncOperation Dim ip As IPAddress = IPAddress.Parse(“127.0.0.1”) Dim tl As TcpListener = New TcpListener(ip, 1234) Dim ns As NetworkStream Dim br As BinaryReader Dim bw As BinaryWriter Dim soc As Socket Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Text = "ok" tl.Start() Label1.Text = "Server started" soc = tl.AcceptSocket ns = New NetworkStream(soc) bw = New BinaryWriter(ns) br = New BinaryReader(ns) Dim s As String = br.ReadString bw.Write(s) End Sub End Class[/CODE] CLIENT: [CODE=vbnet]Imports System.Net.Sockets Imports System.IO Public Class Form1 Dim tc As TcpClient = New TcpClient() Dim ns As NetworkStream Dim br As BinaryReader Dim bw As BinaryWriter Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click tc.Connect(“127.0.0.1”, 1234) ns = tc.GetStream br = New BinaryReader(ns) bw = New BinaryWriter(ns) bw.Write(TextBox1.Text) TextBox2.Text = br.ReadString End Sub End Class[/CODE] I saw that in vb.net there is the function called "SocketAsync" but I didn't find any example to follow ... Please, someone could help me? Thanks in advance!! [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
Visual Studio .NET
VS.NET General Discussion
Convert a simple client/server app from sync to async
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom