|
-
Feb 12th, 2006, 08:56 PM
#1
Thread Starter
Hyperactive Member
Winsock newbie
I wanna dig into winsock, so i hear its the best networking code for vb6. I would be using it most likely for games. So im looking for some really basic stuff to get started with, something easy so i could just trasfer variables from 2 computers. Could someone give me a push in the door or lead me to some nice tutorials. Thanks,
___
Dan
-
Feb 13th, 2006, 08:51 AM
#2
Re: Winsock newbie
Try searching google but be sure to stay away from winsock API to start with, just look for some tutorials using the normal winsock control for vb6.
Chris
-
Feb 13th, 2006, 09:20 AM
#3
Re: Winsock newbie
Here is a winsock example, there is two projects, the first project is the Server, which will listen and accept connections, the other will be the client, which will make the connection to the server.
The server listens for a connection on port 8989
The client makes a connection to the server on port 8989
The server and client can then send data to each other using the textbox on form
To add a winsock to the form...go to Project>Componants>select Microsoft Winsock Control>Drag it from the toolbar to the form, rename it to sckServer or sckClient.
In the server project there is 4 controls:
sckServer - This is the winsock control
lblStatus - This label displays the connection status
txtSend - This textbox will be used to input data to send to the client
cmdSend - This button will be used to send data from txtSend to the client
This is the entire code for the server:
VB Code:
Private Sub Form_Load()
'this section initializes the sckServer winsock to listen for connections
sckServer.Close 'Close the socket
sckServer.LocalPort = 8989 'Set the socket port to 8989
sckServer.Listen 'Listen for connection attempts on the port
lblStatus.Caption = "Listening for connections"
End Sub
Private Sub sckServer_Close()
'When the remote computer ends the connection, this event is fired
lblStatus.Caption = "Connection closed"
sckServer.Close
End Sub
Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)
'When a connection attempt is made to the socket, this event is fired...
sckServer.Close 'Close the socket
sckServer.Accept requestID 'Accept the connection
End Sub
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
'When some data is received on the socket, this event is fired
Dim strData As String
sckServer.GetData strData, vbString 'Grab the data received and store it in strData variable
MsgBox "Data Arrived From Client: " & strData 'Display the data received in a message box
End Sub
Private Sub sckServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
'This event is fired when there is an error in the socket
'this event is used more in the client socket when making connections
'we will just put a message box which will display the error
MsgBox "Error: " & Number & ", " & Description, vbCritical, "Server Error"
End Sub
in the client project there are 5 controls...
sckClient - This is the winsock for the client
lblStatus - Same as server
cmdConnect - When this button is pushed, the program makes a connection attempt to the server
txtSend - Same as server
cmdSend - Same as server
Here is the entire code for the client project...
VB Code:
Private Sub cmdConnect_Click()
'Sub for the connect button
sckClient.Close 'Close the socket
sckClient.Connect "127.0.0.1", 8989 'Make a connection to 127.0.0.1 on port 8989
'The 127.0.0.1 IP will loop back to your own computer
'You could replace this with a server IP
End Sub
Private Sub cmdSend_Click()
'Send button sub
sckClient.SendData txtSend.Text
End Sub
Private Sub sckClient_Close()
'This event is fired when the remote side ends the connection
lblStatus.Caption = "Connection closed"
End Sub
Private Sub sckClient_Connect()
'This event is fired when the connection attempt has been made and established
lblStatus.Caption = "Connected"
sckClient.SendData "HELLO" 'Send some data to the server saying HELLO
End Sub
Private Sub sckClient_DataArrival(ByVal bytesTotal As Long)
'When some data is received on the socket, this event is fired
Dim strData As String
sckClient.GetData strData, vbString 'Grab the data received and store it in strData variable
MsgBox "Data Arrived From Server: " & strData 'Display the data received in a message box
End Sub
Private Sub sckClient_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
'This event is fired when there is an error in the socket
'this event is used more in the client socket when making connections
'we will just put a message box which will display the error
MsgBox "Error: " & Number & ", " & Description, vbCritical, "Client Error"
End Sub
You could also add a button to end the connection, you would put this codeL
Be sure to change the name as required^
You could play around with this project and learn winsock, but I would certainly recommend reading some winsock tutorials.
One more thing to know about winsock: in the Winsock_Close event, dont ever put a message box there to tell the user that the connection is closed, always use a label to do this, because there is some strange bug where it constantly repeats a messagebox sometimes, and this will make you unable to continue your app, you would have to close vb in task manager.
Hope this helps and good luck!
Chris
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
|