[RESOLVED] Send Data / Winsock
Hi guys...
After a long day of coding, I have come up with my last coding task of the day...
I'd like to have it so that a Winsock control receives data, looks at it, and based on what it gets does one of two things...
Lets say the program receives the letter "r" it will perform MsgBox "R"
Lets say if the program receives anything else, it will apply it to a text box, and then save it to a text file.
Program 1 Action: Sends Letter "R"
Program 2 Action: MsgBox "R"
Program 1 Action: Sends multilined contents of TextBox
Program 2 Action: Receives content and stores it as is (including lines) in a txt file...
Is this do-able? If so, how would I go about coding it? My brian is totally fried after a whole 14 hours of working on code!
Thanks fellas!
Re: [RESOLVED] Send Data / Winsock
Then don't use the Form_Load to do the listening. Use another Sub for this. It is bad practice to use the Form_Load event to have your server socket listen. Not good programming technique to use a timer for what you are using it for.
NOTE:
You can lead a horse to water but you cant make him drink it
Re: [RESOLVED] Send Data / Winsock
I didnt use it. I used a timer that runs once on disconnect to listen again. :P
Re: [RESOLVED] Send Data / Winsock
Whatever works for you then do it even if it is poor programming practice.
Re: [RESOLVED] Send Data / Winsock
How is it bad programing?!
I simply said I couldn't go back to the form_load event becasue there are other things in there that cant be triggered.
I then said that my solution was that when the connection closed, it fired a timer that tells winsock to listen again... then disables the timer...
How would you do it since my way is bad practice?! I'm new, gimme a break.
Re: [RESOLVED] Send Data / Winsock
How would you do it since my way is bad practice?! I'm new, gimme a break.
I have been telling you the correct way all along; you just haven't paid any attention to it.
First, I would use a socket array instead of a single socket. Socket(0) will be the socket that the server listens on. Socket(1) will be the socket that the server loads and uses as the connection request socket thus becoming the communication socket. When the client closes his socket you need only to close Socket(1) and Socket(0) remains in the Listen state therefore eliminating the need to return to the server socket at the Socket.Listen.
Second, if I was to use a single server socket I would put a call statement in the Form_Load event which calls the server socket listen sub. Then on client closing I would put another call in the server socket close event to call that same sub thereby elimating the need for a timer event (which, by the way, you are the first programmer I have ever seen in all my years of socket programming to use a timer to trigger the listening state). Like this:
Code:
'
'
Private Sub Form_Load()
'
' Do whatever needs to be done here
'
ServerListen
End Sub
Private Sub ServerListen()
Socket.Close
Socket.LocalPort = 15151
Socket.Listen
End Sub
Private Sub Socket_ConnectionRequest(ByVal requestID As Long)
'
' Do same thing as you are already doing
'
End Sub
Private Sub Socket_Close()
ServerListen
End Sub
'
'
'
'
Above is for a single server socket. Using two sockets eliminates the call to the ServerListen sub in the Socket_Close() event.
Also, you should always have a Socket_Error(.....) event to trap any unexpected error(s) that might occur.
The above is the correct way. Maybe not the very best since I have seen much better socket programming but for a simple application like yours the above is the way to go. Using a timer is pointless and your boss would probably make you re-write your code. If this is only for you and you alone then you can do it anyway that siutes your fancy but I am just trying to help you get it done right from the start.
1 Attachment(s)
Re: [RESOLVED] Send Data / Winsock
Here is what I've been able to develop, using the code here, as well as the link that was provided. I will leave you with this one note though, I hope you aren't planning any malicious works with this. I only say this, because I know how easily it would be to modify a few lines of the code Ill post, and this code can become classified as trojan material. Admin: if you feel the need, remove my attachment.
wxmancanada, I don't normally do this, but if you do use some of my code, I'd like to see (via PM if nessacary) your full code. Just out of curiosity.
Code not 100% mine, as I basically copied and modified to suit my needs, but info was given on a free to use basis. Nor is it 100% free from errors. Code is not commented on, as it is fairly straight forward... exception being that the client relies on the server to update the clients screen (alowing for custom messages to be used, depending on the data entered, after server processing)
JmsRickland, could you show me how such an array would work? Would it be possible (or needed) to use a dynamic to support any number of connections?
Re: [RESOLVED] Send Data / Winsock
.....show me how such an array would work? Would it be possible (or needed) to use a dynamic to support any number of connections?
Using a single socket server (as in the OP's approach) allows for only one client to connect. Once the server has established a connection it is locked from acepting any other requests because the socket is no longer in the Listening state.
In order to get around this problem it becomes necessary for the server to have a way to do both listening (all the time) for incomming requests and also carry on a normal communications with one or more clients.
Answer. The server must use more than one socket. At the minimum, two sockets are required, one socket is used throughout the entire life of the server application and remains in a Listening state all the time. The other socket(s) will be used to carry out the communications between the server and one or more clients connected at the same time.
To make the server in it's simplest form is to put one Winsock control on the Form and give it an Index of 0 (zero). This is the base socket of the array and it will be used as the Listening socket. So, here's how it works....
Code:
Dim Clients As Integer
'
'
Private Sub Form_Load()
'
'
'
ServerListen
End Sub
Private Sub ServerListen()
Socket(0).Close
Socket(0).LocalPort = 15151
Socket(0).Listen
End Sub
Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Clients = Clients + 1
Load Socket(Clients)
Socket(Clients).Close
Socket(Clients).Accept requestID
End Sub
Private Sub Socket_Close(Index As Integer)
Socket(Index).Close
End Sub
Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim s As String
Socket(Index).GetData s
'
'
'
End Sub
I'm not trying to implement a full-blown featured server but only to show you the very basics of how to get a multi-client server application on the way. The above code is by no means meant to be complete in all that is necessary nor am I implying that other programming techniques would not be better that what I am showing. There are many many example of excellent multi-client chat programs around here and I do not need to extend on that since you can find all you need to know by searching on the key word multchat, multiclient, etc.
Re: [RESOLVED] Send Data / Winsock
EDIT: I'll actually make a new thread instead of dirtying this one up... :P