Controversial Topic of Winsck and TCP/IP Connecting
-
I've posted this up on the MSDN in the VB section and it has sat there receiving 55 views and still no answers or suggestions. I'm hoping that maybe someone here can help me understand it and help me find a solution. Now, I've gone and I've bought at least 300 dollars worth of books and tried every example I could find on the internet and tried to learn on my own, but I've hit a snag that requires some expertise. I do not claim to be a great VB programmer, but I'm hoping that my skill will increase with time. It took a friend of mine 5 years to master C programming, and after he mastered it, it only took him 3 months to master C++ and C#. So, I have hope that I'll master VB faster than he mastered C. I have read - recently - many topics concerning the connection of TCP/IP connecting versus using the old winsck connecting method. Currently, I'm in the middle of a project and I'm completely lost as to which direction to go because I've tried both directions (winsck & TCP/IP) without any sort of avail. Currently, I am attempting to create my own mud client (as a reference, you could look at Nerdy Mud Client @ SourceForge - I'm trying to recreate the VB6 project using VB 2008 EE). Thus far, I've been able to appropriately get my forms together and working the way I wanted them. I've got the main form, with a window where the text output should be, I've got a text input bar and set it to AcceptsReturn True so people can type a message into the bar and hit enter to submit it, but for some reason, I can't get it to connect at all. I'd like it to actually connect to "legendsofkrynn.wolfpaw.net:6100." I keep finding tutorials on creating P2P chat clients and FTP clients, but that's not what I want. I want my client to talk to the server and receive information from the server, as well as send user input commands to the server. Currently, this is the code that I've got for my connection method.
'Connects to legendsofkrynn.wolfpaw.net:6100
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ConnectToolStripMenuItem.Click
winsock1.Connect("legendsofkrynn.wolfpaw.net", 6100)
End SubUnfortunately, if this connects, it won't display the output into my mudViewer form (which is just a regular textbox set to multiline = true). Unfortunately as well, if this doesn't connect, I have no way to figure it out. Now, I've also tried....
-
I've posted this up on the MSDN in the VB section and it has sat there receiving 55 views and still no answers or suggestions. I'm hoping that maybe someone here can help me understand it and help me find a solution. Now, I've gone and I've bought at least 300 dollars worth of books and tried every example I could find on the internet and tried to learn on my own, but I've hit a snag that requires some expertise. I do not claim to be a great VB programmer, but I'm hoping that my skill will increase with time. It took a friend of mine 5 years to master C programming, and after he mastered it, it only took him 3 months to master C++ and C#. So, I have hope that I'll master VB faster than he mastered C. I have read - recently - many topics concerning the connection of TCP/IP connecting versus using the old winsck connecting method. Currently, I'm in the middle of a project and I'm completely lost as to which direction to go because I've tried both directions (winsck & TCP/IP) without any sort of avail. Currently, I am attempting to create my own mud client (as a reference, you could look at Nerdy Mud Client @ SourceForge - I'm trying to recreate the VB6 project using VB 2008 EE). Thus far, I've been able to appropriately get my forms together and working the way I wanted them. I've got the main form, with a window where the text output should be, I've got a text input bar and set it to AcceptsReturn True so people can type a message into the bar and hit enter to submit it, but for some reason, I can't get it to connect at all. I'd like it to actually connect to "legendsofkrynn.wolfpaw.net:6100." I keep finding tutorials on creating P2P chat clients and FTP clients, but that's not what I want. I want my client to talk to the server and receive information from the server, as well as send user input commands to the server. Currently, this is the code that I've got for my connection method.
'Connects to legendsofkrynn.wolfpaw.net:6100
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ConnectToolStripMenuItem.Click
winsock1.Connect("legendsofkrynn.wolfpaw.net", 6100)
End SubUnfortunately, if this connects, it won't display the output into my mudViewer form (which is just a regular textbox set to multiline = true). Unfortunately as well, if this doesn't connect, I have no way to figure it out. Now, I've also tried....
Having no actual experience myself, with what you are trying to do, here is a couple of thoughts that may help: - Attempt the actual connection in a Try Catch block with
msgbox(ex.tostring)
to first find out if it really is not connecting, or if there is simply some error in the code. - Does the.Connect
method in your winsock code example return a value? If so, the return value may tell you why the connection fails / is refused. You can step through the code, or perhaps you can show the return value (if there is one) in a messagebox as well. - Almost all remote connections require some sort of identification (username / password). Have you checked if maybe the connection simply wants you to supply such information?My advice is free, and you may get what you paid for.
-
I've posted this up on the MSDN in the VB section and it has sat there receiving 55 views and still no answers or suggestions. I'm hoping that maybe someone here can help me understand it and help me find a solution. Now, I've gone and I've bought at least 300 dollars worth of books and tried every example I could find on the internet and tried to learn on my own, but I've hit a snag that requires some expertise. I do not claim to be a great VB programmer, but I'm hoping that my skill will increase with time. It took a friend of mine 5 years to master C programming, and after he mastered it, it only took him 3 months to master C++ and C#. So, I have hope that I'll master VB faster than he mastered C. I have read - recently - many topics concerning the connection of TCP/IP connecting versus using the old winsck connecting method. Currently, I'm in the middle of a project and I'm completely lost as to which direction to go because I've tried both directions (winsck & TCP/IP) without any sort of avail. Currently, I am attempting to create my own mud client (as a reference, you could look at Nerdy Mud Client @ SourceForge - I'm trying to recreate the VB6 project using VB 2008 EE). Thus far, I've been able to appropriately get my forms together and working the way I wanted them. I've got the main form, with a window where the text output should be, I've got a text input bar and set it to AcceptsReturn True so people can type a message into the bar and hit enter to submit it, but for some reason, I can't get it to connect at all. I'd like it to actually connect to "legendsofkrynn.wolfpaw.net:6100." I keep finding tutorials on creating P2P chat clients and FTP clients, but that's not what I want. I want my client to talk to the server and receive information from the server, as well as send user input commands to the server. Currently, this is the code that I've got for my connection method.
'Connects to legendsofkrynn.wolfpaw.net:6100
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ConnectToolStripMenuItem.Click
winsock1.Connect("legendsofkrynn.wolfpaw.net", 6100)
End SubUnfortunately, if this connects, it won't display the output into my mudViewer form (which is just a regular textbox set to multiline = true). Unfortunately as well, if this doesn't connect, I have no way to figure it out. Now, I've also tried....
Johan made some good suggestions in the other post, but more can't hurt... You posted the connection code, which may or may not be working, but where's the code that does the actual I/O? The code that sends the user-entered text, and the code that displays the incoming data? If that code has errors, it would explain why you aren't seeing any output. Socket programming can be a little complicated for beginners, mainly because you generally use a second thread to watch for output, and you have to parse out individual messages from the network stream.
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)
-
I've posted this up on the MSDN in the VB section and it has sat there receiving 55 views and still no answers or suggestions. I'm hoping that maybe someone here can help me understand it and help me find a solution. Now, I've gone and I've bought at least 300 dollars worth of books and tried every example I could find on the internet and tried to learn on my own, but I've hit a snag that requires some expertise. I do not claim to be a great VB programmer, but I'm hoping that my skill will increase with time. It took a friend of mine 5 years to master C programming, and after he mastered it, it only took him 3 months to master C++ and C#. So, I have hope that I'll master VB faster than he mastered C. I have read - recently - many topics concerning the connection of TCP/IP connecting versus using the old winsck connecting method. Currently, I'm in the middle of a project and I'm completely lost as to which direction to go because I've tried both directions (winsck & TCP/IP) without any sort of avail. Currently, I am attempting to create my own mud client (as a reference, you could look at Nerdy Mud Client @ SourceForge - I'm trying to recreate the VB6 project using VB 2008 EE). Thus far, I've been able to appropriately get my forms together and working the way I wanted them. I've got the main form, with a window where the text output should be, I've got a text input bar and set it to AcceptsReturn True so people can type a message into the bar and hit enter to submit it, but for some reason, I can't get it to connect at all. I'd like it to actually connect to "legendsofkrynn.wolfpaw.net:6100." I keep finding tutorials on creating P2P chat clients and FTP clients, but that's not what I want. I want my client to talk to the server and receive information from the server, as well as send user input commands to the server. Currently, this is the code that I've got for my connection method.
'Connects to legendsofkrynn.wolfpaw.net:6100
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ConnectToolStripMenuItem.Click
winsock1.Connect("legendsofkrynn.wolfpaw.net", 6100)
End SubUnfortunately, if this connects, it won't display the output into my mudViewer form (which is just a regular textbox set to multiline = true). Unfortunately as well, if this doesn't connect, I have no way to figure it out. Now, I've also tried....
You're showing the code for the form , but you're not showing ANY of the TCP handling code at all. If all you have is a call to Connect, and nothing else, then it's not surprising you're not seeing anything come back. You don't have any code handing any of the received data and putting in in the textbox.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
I've posted this up on the MSDN in the VB section and it has sat there receiving 55 views and still no answers or suggestions. I'm hoping that maybe someone here can help me understand it and help me find a solution. Now, I've gone and I've bought at least 300 dollars worth of books and tried every example I could find on the internet and tried to learn on my own, but I've hit a snag that requires some expertise. I do not claim to be a great VB programmer, but I'm hoping that my skill will increase with time. It took a friend of mine 5 years to master C programming, and after he mastered it, it only took him 3 months to master C++ and C#. So, I have hope that I'll master VB faster than he mastered C. I have read - recently - many topics concerning the connection of TCP/IP connecting versus using the old winsck connecting method. Currently, I'm in the middle of a project and I'm completely lost as to which direction to go because I've tried both directions (winsck & TCP/IP) without any sort of avail. Currently, I am attempting to create my own mud client (as a reference, you could look at Nerdy Mud Client @ SourceForge - I'm trying to recreate the VB6 project using VB 2008 EE). Thus far, I've been able to appropriately get my forms together and working the way I wanted them. I've got the main form, with a window where the text output should be, I've got a text input bar and set it to AcceptsReturn True so people can type a message into the bar and hit enter to submit it, but for some reason, I can't get it to connect at all. I'd like it to actually connect to "legendsofkrynn.wolfpaw.net:6100." I keep finding tutorials on creating P2P chat clients and FTP clients, but that's not what I want. I want my client to talk to the server and receive information from the server, as well as send user input commands to the server. Currently, this is the code that I've got for my connection method.
'Connects to legendsofkrynn.wolfpaw.net:6100
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ConnectToolStripMenuItem.Click
winsock1.Connect("legendsofkrynn.wolfpaw.net", 6100)
End SubUnfortunately, if this connects, it won't display the output into my mudViewer form (which is just a regular textbox set to multiline = true). Unfortunately as well, if this doesn't connect, I have no way to figure it out. Now, I've also tried....
Everyone, Thank you so much for your suggestions! Johan, I actually did not think of that. I don't have any block for winsck that would show whether or not the connection was connected or not. Ian and Dave, I did not think of having code to handle the I/O. The basic concept is that I want this one client to connect to the remove server (legendsofkrynn.wolfpaw.net) on the specified port (6100). So it would make sense that there would need to be something that needed to handle the I/O. Would you recommend I use a module or a class for the I/O handling? Also, which would actually be more prudent to use in my current quest? Winsck or TCP/IP? Many people say that with the development of VB 2008, there's no longer a need to use winsck, would anyone agree?
-
Everyone, Thank you so much for your suggestions! Johan, I actually did not think of that. I don't have any block for winsck that would show whether or not the connection was connected or not. Ian and Dave, I did not think of having code to handle the I/O. The basic concept is that I want this one client to connect to the remove server (legendsofkrynn.wolfpaw.net) on the specified port (6100). So it would make sense that there would need to be something that needed to handle the I/O. Would you recommend I use a module or a class for the I/O handling? Also, which would actually be more prudent to use in my current quest? Winsck or TCP/IP? Many people say that with the development of VB 2008, there's no longer a need to use winsck, would anyone agree?
The general rule is that when you're programming a .NET language, try to stay within the .NET framework and .NET-based libraries (aka "Managed code") unless you really need to do otherwise. Hence, don't use winsock directly. Use a TcpClient. The winsock DLLs are pretty much obsolete anyway, I believe. I do suggest, though, that if this is your first foray into programming, you pick a simpler project. Network clients aren't incredibly complicated, but they pull in several concepts that are a little above the beginner level. Here's the brief summary of what you need to do: 1) Get the network stream from the GetStream() function. This is an abstraction of the "pipe" you use to read and write data from the network. 2) To simplify access to this stream, create a System.IO.StreamReader. This does all the work of buffering the incoming data, splitting it into discrete messages, and letting you read one line at a time. 3) Create a background thread to watch the streamreader and read the incoming data. Look up System.Threading.ThreadPool.QueueUserWorkItem for a "safe" way to do it. If you're not familiar with the concept, having an extra thread is basically like running two parts of the program at the same time (Sort of). The background thread will keep watching the network for incoming data. 4) Have the background thread loop on the streamreader's ReadLine() function, to get the actual data. 5) When it gets a line, Invoke to the GUI thread (This is needed, because background threads can't touch GUI controls) and add the line to your text box. Those five steps will let you see the data coming in from the network... Sending data is actually a lot easier... 1) Create a StreamWriter on the same stream in the first step above. 2) When the user presses enter in the input box, grab the text from that box and use the StreamWriter's WriteLine() method, followed by Flush() (Flush makes sure the data actually gets sent, instead of waiting in a buffer). 3) Also add that text to the output window, since MUD clients expect you to echo the data you send. I've bolded some of the keywords, so you know what to look up in your references... But like I said, this is a bit above beginner level, so take your time and keep google handy.
Proud to have finally moved to the A-Ark. Which one are you in
-
The general rule is that when you're programming a .NET language, try to stay within the .NET framework and .NET-based libraries (aka "Managed code") unless you really need to do otherwise. Hence, don't use winsock directly. Use a TcpClient. The winsock DLLs are pretty much obsolete anyway, I believe. I do suggest, though, that if this is your first foray into programming, you pick a simpler project. Network clients aren't incredibly complicated, but they pull in several concepts that are a little above the beginner level. Here's the brief summary of what you need to do: 1) Get the network stream from the GetStream() function. This is an abstraction of the "pipe" you use to read and write data from the network. 2) To simplify access to this stream, create a System.IO.StreamReader. This does all the work of buffering the incoming data, splitting it into discrete messages, and letting you read one line at a time. 3) Create a background thread to watch the streamreader and read the incoming data. Look up System.Threading.ThreadPool.QueueUserWorkItem for a "safe" way to do it. If you're not familiar with the concept, having an extra thread is basically like running two parts of the program at the same time (Sort of). The background thread will keep watching the network for incoming data. 4) Have the background thread loop on the streamreader's ReadLine() function, to get the actual data. 5) When it gets a line, Invoke to the GUI thread (This is needed, because background threads can't touch GUI controls) and add the line to your text box. Those five steps will let you see the data coming in from the network... Sending data is actually a lot easier... 1) Create a StreamWriter on the same stream in the first step above. 2) When the user presses enter in the input box, grab the text from that box and use the StreamWriter's WriteLine() method, followed by Flush() (Flush makes sure the data actually gets sent, instead of waiting in a buffer). 3) Also add that text to the output window, since MUD clients expect you to echo the data you send. I've bolded some of the keywords, so you know what to look up in your references... But like I said, this is a bit above beginner level, so take your time and keep google handy.
Proud to have finally moved to the A-Ark. Which one are you in
Ian, This is some really great advice! Thanks so much for writing it for me. So far, I'm shocked and surprised that I get more help here than I do on the MSDN official VB forums with Microsoft, heh. I'm definitely checking into these things as of right this moment. Now, I do have a question, do you think it would be more beneficial for me to modulate the your suggestions, or should I privatize them and make them classes? Also, I'm including code below - upgraded I/O module - from Nerdy Mud Clients source code. Should I attempt to just copy/paste and credit that portion of the code to Nerdy, or are your suggestions above suggestions for I/O? I know that sounds like a stupid question and I apologize if it makes me look even more noobish. I know this project is definitely above my level, but I'm fully committed to seeing it through. To me, I feel that it's better to tackle something difficult and learn as much from it as possible than to keep with the smaller projects and learn little by little. Backwards logic, I know.
Option Strict Off
Option Explicit On
Module mdlDataIO
Dim cmdhistRow As Short
Dim cmdhistNewPos As Short
Dim cmdHistTmp As String
Public arraycmdhist(30) As String
Public Sub multisend(ByRef txt2send As String)
frmMain.Winsock1.SendData(txt2send & vbCr)
End Sub
Public Sub File2Combo(ByRef sfile As String, _
ByRef oList As System.Windows.Forms.ComboBox) _
'this has been relocated from frmmain to help modularize and clean up code
'it should be stable and complete
Dim fnum As Short
Dim sTemp As String
fnum = FreeFile()
oList.Items.Clear()
FileOpen(fnum, sfile, OpenMode.Input)
While Not EOF(fnum)
sTemp = LineInput(fnum)
oList.Items.Add(sTemp)
End While
FileClose(fnum)
End SubPublic Sub cmdHistScrollup() 'stable/finalized If CDbl(frmMain.lblSBH.Text) = 30 Then frmMain.lblSBH.Text = CStr(0) If arraycmdhist(CInt(frmMain.lblSBH.Text)) = "" Then Do While arraycmdhist(CInt(frmMain.lblSBH.Text)) = "" \_ And CDbl(frmMain.lblSBH.Text) < 30 frmMain.lblSBH.Text = CStr(CDbl(frmMain.lblSBH.Text) + 1) Loop End If frmMain.txtInput.Text = arraycmdhist(CInt(frmMain.lblSBH.Text)) If CDbl(frmMain.lblSBH.Text) <= 29 Then frmMain.lblSBH.Text = \_ CStr(CDbl(frmMain.lblSBH.Text) + 1) End Sub Public Sub cmdhistscrollDown()
-
Ian, This is some really great advice! Thanks so much for writing it for me. So far, I'm shocked and surprised that I get more help here than I do on the MSDN official VB forums with Microsoft, heh. I'm definitely checking into these things as of right this moment. Now, I do have a question, do you think it would be more beneficial for me to modulate the your suggestions, or should I privatize them and make them classes? Also, I'm including code below - upgraded I/O module - from Nerdy Mud Clients source code. Should I attempt to just copy/paste and credit that portion of the code to Nerdy, or are your suggestions above suggestions for I/O? I know that sounds like a stupid question and I apologize if it makes me look even more noobish. I know this project is definitely above my level, but I'm fully committed to seeing it through. To me, I feel that it's better to tackle something difficult and learn as much from it as possible than to keep with the smaller projects and learn little by little. Backwards logic, I know.
Option Strict Off
Option Explicit On
Module mdlDataIO
Dim cmdhistRow As Short
Dim cmdhistNewPos As Short
Dim cmdHistTmp As String
Public arraycmdhist(30) As String
Public Sub multisend(ByRef txt2send As String)
frmMain.Winsock1.SendData(txt2send & vbCr)
End Sub
Public Sub File2Combo(ByRef sfile As String, _
ByRef oList As System.Windows.Forms.ComboBox) _
'this has been relocated from frmmain to help modularize and clean up code
'it should be stable and complete
Dim fnum As Short
Dim sTemp As String
fnum = FreeFile()
oList.Items.Clear()
FileOpen(fnum, sfile, OpenMode.Input)
While Not EOF(fnum)
sTemp = LineInput(fnum)
oList.Items.Add(sTemp)
End While
FileClose(fnum)
End SubPublic Sub cmdHistScrollup() 'stable/finalized If CDbl(frmMain.lblSBH.Text) = 30 Then frmMain.lblSBH.Text = CStr(0) If arraycmdhist(CInt(frmMain.lblSBH.Text)) = "" Then Do While arraycmdhist(CInt(frmMain.lblSBH.Text)) = "" \_ And CDbl(frmMain.lblSBH.Text) < 30 frmMain.lblSBH.Text = CStr(CDbl(frmMain.lblSBH.Text) + 1) Loop End If frmMain.txtInput.Text = arraycmdhist(CInt(frmMain.lblSBH.Text)) If CDbl(frmMain.lblSBH.Text) <= 29 Then frmMain.lblSBH.Text = \_ CStr(CDbl(frmMain.lblSBH.Text) + 1) End Sub Public Sub cmdhistscrollDown()
Ok, one thing at a time... 1) Forget modules. That's a VB6 thing. The closest parallel is a "static class", which for something like this, you don't need. 2) The code you posted is something else entirely... You don't need that at all. Basically, here's the interface I would suggest (Not spoon-feeding the code - Just setting up a layout):
Public Class ServerConnection
Public Property IsConnected as Boolean
Get
Private Set
End PropertyPublic Sub Connect(ByVal address as String, ByVal port as Long)
'connect to the server and start a new thread to listen for incoming data
End Sub
Public Sub Disconnect(ByVal address as String, ByVal port as Long)End Sub
Public Sub SendMessage(ByVal message as String)
End Sub
Public Event MessageReceived(ByVal sender as Object, ByVal message as String)
End ClassThis is just the interface... It's up to you to implement it. The idea is that only this class actually knows how the data is going back and forth, so everything not shown here is set as Private. The rest of your application can just send and receive messages simply, without having to know the details. The term for this is "encapsulation", and it's one of the staples of object-oriented programming. Granted, you could put all of the code in the same file as your form, but if you start out with a good design structure, it makes it easier to add to it later, and much easier to read. As for putting the results in a textbox... Well, that part is a lot simpler.
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)