-
[RESOLVED] How To Send Command1.Caption over Winsock1?
I am trying to write a chat room program and I am going to use Command Buttons to display the number of chat rooms open for the user to chat into.
I wish to send the Caption of Command1 to Command10 over Winsock1, via a modem TCP/IP Internet or LAN ENTERnet cable. Also I wish to only use the port "1234" and nothing else. I wish to send this from one project and then recieve this from another project, that isn't actually the same, but works on the same protocols and ports of TCP/IP etc.
Also the Command Captions must be encripted in some way, to only make it impossible for a person to read it, but not a simple 32-Bit machine.
-
Re: How To Send Command1.Caption over Winsock1?
To use the TCP/IP protocol you will need to use mswinsck.ocx. The winsock control allows you to creat TCP connections or use UDP to communicate between programs over a network or the internet.
You could also use API calls, but I have found that it is a lot of mucking around. It's generally easier to start with the winsock control when learning.
look up VB6 Winsock Tutorial, there is tons of info out there and really it is very simple to use.
-
Re: How To Send Command1.Caption over Winsock1?
So I can't do this, right?
Open Winsock1
Print Winsock1, Command1.Caption .... Command10.Caption
Close Winsock1
Hangup Winsock1
-
Re: How To Send Command1.Caption over Winsock1?
nope, can't do that.
1) right-click the toolbox>Components
2) Scroll down until you find Microsoft Winsock Control 6.0 and tick it.
3) "Draw"/place a Winsock control on your form.
4) In the application waiting for a connection (the server app) set the index to 0 and place this code in Form_Load
Code:
Winsock1(0).localport=1234
Winsock1(0).listen
Place this code inside your Winsock ConnectionRequest sub
Code:
load Winsock1(winsock1.ubound+1)
winsock1(winsock1.ubound).accept requestid
Place this code inside your Winsock DataArrival sub
Code:
dim theData as string
winsock1(index).getdata theData
'theData contains the data received.
To send data from the server app to the client app use winsock1(x).senddata "Text to be sent"
the x stands for the sockt number to send the data on as this server app can have multiple connections. The sockets are in a control array, so treat it as any ordinary array.
5) In the application making the connection put this is the form_load sub
Code:
winsock1.remoteport=1234
winsock1.remotehost="127.0.0.1" 'change this to the IP/computername/domain of the computer running the server app 127.0.0.1 is the loopback for the local machine
In the winsock1 DataArrival sub place this code
Code:
dim theData as string
winsock1(index).getdata theData
'theData contains the data received.
To send data use the following command winsock1.senddata command1.caption ' change comman1.caption to whatever you require to be sent. The client app only supports 1 connection at a time, this of course could be changed by making Winsock1 a control array. to close a connection use winsock1.close
-
Re: How To Send Command1.Caption over Winsock1?
I am getting a run-time error and it says that the address is in use and the error code is: 10,048.
I am not sure what that means, because never came up against it before. Everything else is what you had coded and haven't change anything of my project, which was in deed good for me.
-
Re: How To Send Command1.Caption over Winsock1?
Okay I have changed my source code to look better and function better. That error that was stated above in post #5 is now working. But when I try and establish the connection it crashes the program.
The run-time error is: '40,006' and the error description is: Wrong protocol or connection state for the requested transaction or request.
-
Re: How To Send Command1.Caption over Winsock1?
you cant send data until the TCP handshake is complete, what I generally do is this
Code:
winsock1.connect
tmrWaitForConnection.interval=1000 'this may need to be increased if you are connecting across a slow network or long distances over the net
tmrWaitForConnection.enabled=true
place a timer and in the sub tmrWaitForConnection_Timer place this code:
Code:
if winsock1.state=sckconnected then
'do something here to notify the app that the connection is ready, eg set a public boolean var to true
else
'connection not established, perhaps we should wait longer or alert the user that the connection timed out
end if
-
Re: How To Send Command1.Caption over Winsock1?
With post #7; Do I place this code into the reciever project or the sender project? You were clear where to put the code into the project.
-
Re: How To Send Command1.Caption over Winsock1?
This is the Server App, that is sending the data to the Client App. The zip file is called: Doom Network 2008 - 1.zip and the Client App, zip file is called: Doom Client 2008 - 1.zip.
Please note: That the attached files have been removed by the powers of be. (Just kidding).
-
Re: How To Send Command1.Caption over Winsock1?
What is the syntax of Winsock1.SendData ......?????......
What is the syntax of Winsock1.RecieveData .......??????.......
-
Re: How To Send Command1.Caption over Winsock1?
This is how the winsock is supposed to be used:
Although there is a protocol named UDP, but it is a bit complex for the beginners, we shall be using the other one named TCP/IP because it is much simpler for the newbies.
In TCP/IP, there are two types of programs (applications). One of them will open a port (a door, to understand it easily) and wait for someone to connect to it. This is known as the SERVER. The other program will connect to the server from another machine. This one is known as a CLIENT.
This understood, we now see how both these parts work. First we shall examine the server part.
Code:
Option Explicit
Private Sub Form_Load()
Winsock1.LocalPort = 1234 'this is the port number (or the door number, you could say)
Winsock1.Listen 'this code will tell the winsock to wait for someone to connect
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close 'first we stop waiting anymore because someone is already there
Winsock1.Accept requestID 'this is how we allow someone to connect to us
Winsock1.SendData "mConnected successfully!" 'only sending a connection message, you could ignore this
End Sub
Now we see, how the client is supposed to connect to the server. Note that this code is for
another program and NOT to be used with the same code as that of above
Code:
Option Explicit
Private Sub Command1_Click()
'first we save the IP address of the server computer
Winsock1.RemoteHost = "127.0.0.1" 'this is if both programs are running on the same computer
Winsock1.RemotePort = 1234 'must be the same port that the server is using
Winsock1.Connect 'now that we know the IP and the port, try connecting to it
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'dataarrival occurs when some data is sent from the other corner
Dim data As String
Winsock1.GetData data 'save the arrived data in a string
If Left(data,1) = "m" Then 'if a message has arrived
MsgBox Mid(data,2) 'show a message box
End If
End Sub
Do you understand it now? If questions persist, don't hesitate asking me again.
-
Re: How To Send Command1.Caption over Winsock1?
Also one more thing. What is the command to send data in a specific form to the client using the server project.
-
Re: How To Send Command1.Caption over Winsock1?
What do you mean?
You simply use this command:
Winsock1.SendData your_data
Where your_data is a string. But remember, you can only use this command when your winsock is in connected state.
-
Re: How To Send Command1.Caption over Winsock1?
Beep.
This post can be removed by the adminstrators.
-
Re: How To Send Command1.Caption over Winsock1?
I have got the Server working, well it compiled the file without any errors in it. Then with the Client project, I cannot get it to read the data at all.
-
Re: How To Send Command1.Caption over Winsock1?
The error that I am getting from the compiled Client project is: That the connection cannot be established in this current state. Which means that we are nearly getting this thread resolved.
Thanks to you, my friend.
-
1 Attachment(s)
Re: How To Send Command1.Caption over Winsock1?
Quote:
Originally Posted by lone_REBEL
Code:
Option Explicit
Private Sub Form_Load()
Winsock1.LocalPort = 1234 'this is the port number (or the door number, you could say)
Winsock1.Listen 'this code will tell the winsock to wait for someone to connect
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close 'first we stop waiting anymore because someone is already there
Winsock1.Accept requestID 'this is how we allow someone to connect to us
Winsock1.SendData "mConnected successfully!" 'only sending a connection message, you could ignore this
End Sub
There's lots of ways to use the winsock control.
The problem with that code is it only allows one connection to the server at a time, this may be ok for his use, but in other circumstances it wont be appropriate, also, you dont have to reset everything if the connection drops out.
This example contains a simple ping command, and a method to distinguish between different commands that could be received by the server
-
Re: How To Send Command1.Caption over Winsock1?
Actually there is something wrong with my machine. I cannot get it to work properly, I am getting the same error all the time with the demo code you gave me.
-
Re: How To Send Command1.Caption over Winsock1?
Breaking News:
This news is just in, now...
I just found a multi user winsock activex control that can run in just about any lingo. I got the VB6Pro one, even with documentation.
-
Re: How To Send Command1.Caption over Winsock1?
Quote:
Originally Posted by ThEiMp
Actually there is something wrong with my machine. I cannot get it to work properly, I am getting the same error all the time with the demo code you gave me.
what exactly is it doing that it shouldnt be?
is the multi-user winsock activeX working for you?
-
Re: How To Send Command1.Caption over Winsock1?
I just wanted to add that there is no reason to run a loop to wait for winsock to connect. That's what the Connect event is for.
-
Re: How To Send Command1.Caption over Winsock1?
Lol What loop to wait for the connection. Oh yeah, you mean the 1000 ms wait state for the connection. Or something else, right?
-
Re: How To Send Command1.Caption over Winsock1?
Well actually the multiuser activex control that I downloaded from a freeware site, will expire in twenty-nine days or there about.