-
Chat....
Hi ,
How to create a simple chat program in Vb using winsock.
I am having a listener socket at the server so
i am able to send messages from the client to the server.
But i am not able to send any acknowledgement messsage from the server to the client.
Kindly help.
regards,
-Bhasker G
-
i am also currently working on a chat program called "DIGI Chat". I guess if you have a look at my source code u'll understand everything! However i need a week or so to get it finish since it's got many functions like file transfer and white board and much more!
if u can wait for a week or so just tell me ur email and i'll mail the code to you when i finish/! or i can post the code on PSC for other people to have a look!
happy???
-
No...i cannot wait until a week...
Here is my server side code....
Option Explicit
Private Sub Command1_Click()
Winsock2.RemoteHost = "aaa.bbb...." 'SERVER IP ADDRESS
Winsock2.RemotePort = 1111 'The Listening port at the server
Winsock2.Connect
Winsock2.SendData txtToClient 'PROBLEM HERE
End Sub
Private Sub Form_Load()
Winsock1.LocalPort = 1001
Winsock1.Listen
End Sub
Private Sub Winsock1_Close()
Close #1
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strIncoming As String
Winsock1.GetData strIncoming, vbString
txtReceive = strIncoming
End Sub
-
Well ...
I am slightly confused by the code.
Are you putting two Winsock controls on the same form in the server application? Why?
Can you explain how you have written the code?
.
-
Yes you are right honeybee...i have 2 winsock controls since i assumed that 1 control for listening and 1 control for sending message to the client...correct me if i am wrong....and the comment is wrong in this line
Winsock2.RemoteHost = "aaa.bbb...." 'SERVER IP ADDRESS
it should be...
Winsock2.RemoteHost = "aaa.bbb...." 'Requesting Client IP adress
-
Well ...
If you go to my website http://anandk.freehosting.net/custom.html you will find a link to another thread on Winsock where this type of thing has been discussed.
Mainly, the thing to note is that you need to employ a number of winsock controls for such an exercise, either by way of a control array or by way of a collection of Winsock objects.
The first winsock control acts as a listener, which only does the job of listening to any requests coming through on a certain port. Once a request arrives, it then searches the rest of the collection or array to locate a winsock that's available for connection. If such a control is available, the request is passed on to it, and thereafter the listening winsock forgets about the request.
If there is no winsock available for communication, meaning all are already occupied, you can either create a new instance or load another control in the array and proceed or send back an error message to the client telling the connections are full.
.
-
1 Attachment(s)
Well ...
Here is some code. It might not be finished, I left it before completing it in all respects. But that will give you a fair idea of the server side part.
.
-
1 Attachment(s)
Honeybee...can u see my code and tell where i am wrong?
-
Well ...
First off, for testing purposes, I changed the RemoteIPs in both projects to 127.0.0.1 to work on the same computer.
You should move the Winsock2.RemoteIP and port setting statements to the Form_Load in the server part.
Then in the cmdSend_Click event, use a loop similar to the one used in the client:
Do while Winsock2.State <>sckConnected
Winsock2.Connect
If Winsock2.State = sckError Then
'Error Handler
End If
Loop
Winsock2.SendData "Received"
Also in the client form add another textbox and use it to display the Winsock2_DataArrival from the server.
Another piece of advice, whenever developing such programs, test them with simple text messages first, instead of going for the database stuff. There could be confusion while debugging any errors. First make the code perfect with text messages and then customize it to suit your specific needs.
In order for the client to connect to the server, it is necessary that the server has already put Winsock1 in Listen mode. Also in order for the server to communicate to the client through its Winsock2, it is necessary that the client's Winsock2 is already in the listen mode. So it's best to start the listener in the form_load, and connect code in the Command Click event.
.