|
-
Jul 26th, 2004, 01:17 PM
#1
Thread Starter
Member
[Resolved] Problem with code please help (System.Net.Sockets) Related
Ok, I have the following code working except my form locks up until someone connects, then after they connect it locks up until data is sent once data is received and a msg box pops up the form locks up again... Can someone tell me what I am doing wrong? This does exactly what I need it to do but what I dont need it to do is lock up the form.
VB Code:
Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
Dim port As String = "9909"
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
Dim Listen As New TcpListener(ipadd, port)
Listen.Start()
Dim client As TcpClient = Listen.AcceptTcpClient()
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
MsgBox([String].Format("Received: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
cmdListen.Enabled = False
End Sub
Basically the code is setup to listen on an IP/Port. When someone connects it will wait for that persons input then display a msg box when they type something. I am wanting to grab what they type and depending on what they type do a specific thing. I already know how to do this as long as the form doesnt lock up!
Thanks for any help.
P.S. Hope I am not confusing
Last edited by dsc_chris; Jul 26th, 2004 at 03:04 PM.
-
Jul 26th, 2004, 02:01 PM
#2
Frenzied Member
The reason you are locking up is because you are using methods that block, that is, they block the thread until that method completes. I assume all your code is in one thread, so when you call a blocking method, it will hang the GUI.
Both the TcpListener.Start and the NetworkStream.Read methods are blocking methods.
I've taken a different route with my socket programming, using multiple threads and asynchronous methods. But, looking at MSDN, you can avoid blocking with TcpListener.Start by using the Pending method, and avoid blocking with NetworkStream.Read by using the DataAvailable property.
HTH,
Mike
-
Jul 26th, 2004, 02:03 PM
#3
Thread Starter
Member
Thats what I was thinking it was doing, Would you be able to maybe show me some example code?
-
Jul 26th, 2004, 02:15 PM
#4
Thread Starter
Member
Ignore my last request, I think I have found exactly what I needed when you refered to the MSDN. Thank You I will let you know if it works.
-
Jul 26th, 2004, 02:20 PM
#5
Thread Starter
Member
Ok, That has stopped the lockup issue. But it isnt processing anything after Else. Like on an incoming connection... (I am a n00b to .NET it is totally different from vb6 in terms of sockets.)
New Code:
VB Code:
Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
Dim port As String = "9909"
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
Dim Listen As New TcpListener(ipadd, port)
Listen.Start()
If Not Listen.Pending Then
MsgBox("No incoming connections")
Else
Dim client As TcpClient = Listen.AcceptTcpClient()
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
MsgBox([String].Format("Received: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
cmdListen.Enabled = False
End If
End Sub
-
Jul 26th, 2004, 02:20 PM
#6
Frenzied Member
Can try - but example code for what? For the Pending and DataAvailable stuff? I've actually never used that, so I don't have any, although I'd search MSDN for an example.
If you're interested in asynchronous socket programming, look up in MSDN the topics Using an Asynchronous Server Socket, Using an Asynchronous Client Socket, Asynchronous Server Socket Example and Asynchronous Client Socket Example.
If you have played with it yet, asynchronous programming can take a little while to get your head around - although it's pretty cool. Depending on your application, you may not even need it. I've read, but don't know for sure myself, that you can handle up to 2000 connections without having to go asynchronous.
Of course, your mileage may vary
-
Jul 26th, 2004, 02:25 PM
#7
Frenzied Member
Originally posted by dsc_chris
Ignore my last request...
Too late I did not try to run your code, but where exactly does the code block? Looks like you handled the TCPListener with the Pending method, but how about your NetworkStream.Read? It will block there. Aren't you going to try the DataAvailable property?
-
Jul 26th, 2004, 02:25 PM
#8
Thread Starter
Member
What I am looking for is a solution to listen on a specific ip/port and on any incoming data to that connection it process's the data and depending on what the data is it does certain things (things i would specify in the code itself) I dont need to handle 2000 connections :P just a single connection hehe
-
Jul 26th, 2004, 02:31 PM
#9
Thread Starter
Member
This is the new code. Still no go, Am I putting the code in the right place? Right now I have it so when you click the Listen button it process's the code.
VB Code:
Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
Dim port As String = "9909"
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
Dim Listen As New TcpListener(ipadd, port)
Listen.Start()
If Not Listen.Pending Then
MsgBox("No incoming connections")
Else
Dim client As TcpClient = Listen.AcceptTcpClient()
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
If stream.CanRead Then
Do
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
MsgBox([String].Format("Received: {0}", data))
Loop While stream.DataAvailable
cmdListen.Enabled = False
End If
End If
End Sub
-
Jul 26th, 2004, 02:43 PM
#10
Thread Starter
Member
Ok, I have fixed it simply by adding a loop. BUT when it gets to actually processing the incoming data it locks up...
VB Code:
Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
Dim port As String = "9909"
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
Dim Listen As New TcpListener(ipadd, port)
Listen.Start()
MsgBox(Listen.Pending)
Do
If Not Listen.Pending Then
MsgBox("No incoming connections")
lblSocket.Text = "No Incoming Connections"
Else
Dim client As TcpClient = Listen.AcceptTcpClient()
lblSocket.Text = "Client Connected"
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
If stream.CanRead Then
Do
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
'MsgBox([String].Format("Received: {0}", data))
lblSocket.Text = ([String].Format("Received: {0}", data))
Loop While stream.DataAvailable
cmdListen.Enabled = False
End If
End If
Loop
End Sub
-
Jul 26th, 2004, 02:48 PM
#11
Frenzied Member
Try changing your NetworkStream.CanRead to NetworkSteam.DataAvailable. CanRead just tells you that the stream supports reading. I think you're still blocking on the NetworkStream.Read.
-
Jul 26th, 2004, 02:52 PM
#12
Thread Starter
Member
I tried that. Actually what was hogging the form down was that stupid loop. I threw it all into a timer and moved the Dim's to the Pubilc Declarations area and just simply put in the command button
VB Code:
Listen.Start()
tmrSockets.Interval = 1
tmrSockets.Enabled = True
This seems to have fixed the small issue i had. Now if I can figure out how to prevent it from disconnecting right after it receives data i am good to go :P
-
Jul 26th, 2004, 02:58 PM
#13
Thread Starter
Member
Sweet. I did a little bit of testing and it works exactly how I needed it to... Thanks a WHOLE BUNCH Mike. You were an awsome help! I hope this thread is useful to others looking at simple ways around the winsock controls :P
Regards
Chris Childers
DarkStar Communications
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|