Thanks for that reply.
Now that I have added some error handling in the server code it does not work on my local machine either. I will post the code, hopefully some one can tell me what I am doing wrong.
The code for the server is
Option Explicit
Private Sub cmdSend_Click()
Winsock.SendData txtChat.Text
DoEvents
txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text
txtChat.Text = ""
End Sub
Private Sub Form_Load()
cmdSend.Enabled = False 'Added this to deal with error 40006
Winsock.Protocol = sckTCPProtocol
Winsock.LocalPort = 10101
Winsock.Listen
If Winsock.State = sckConnected Then
MsgBox "the client is connected" 'Tried to check if the
'server will return a message when connected, but it does not even
'when my client returns a Connected message
End If
End Sub
Private Sub Winsock_ConnectionRequest(ByVal RequestID As Long)
Winsock.Close
Winsock.Accept RequestID
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock.GetData strData
txtMain.Text = txtMain.Text & vbCrLf & strData
txtMain.SelStart = Len(txtMain.Text)
End Sub
Private Sub winsock_connect()
cmdSend.Enabled = True 'I do not get this message even when I get a
' Connected msg from my client
MsgBox "The client is connected"
End Sub
The code for the client is
Option Explicit
Private Sub cmdConnect_Click()
Winsock.Connect txtIP.Text, "10101"
End Sub
Private Sub cmdLocalHost_Click()
MsgBox "The local host is:" & Winsock.LocalIP
End Sub
Private Sub cmdSend_Click()
Winsock.SendData txtChat.Text
DoEvents
txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text
txtChat.Text = ""
End Sub
Private Sub Winsock_Connect()
MsgBox "Connected"
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock.GetData strData
txtMain.Text = txtMain.Text & vbCrLf & strData
txtMain.SelStart = Len(txtMain.Text)
End Sub
Private Sub Winsock_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)
MsgBox "Error: " & Description
End Sub
Now when I run the client/server on my local machine I am able to send messages to the server, but the problem is that the send button on the server is disabled since the following event does not seem to occur
Private Sub winsock_connect()
cmdSend.Enabled = True
MsgBox "The client is connected"
End Sub
But then how is this possible since only if the server/client are connected will the data be sent to the server right?
I am totally lost any help would be greatly appreciated
: If you call the SendData method in the WinSock control before the socket has been connected, you will receive the Microsoft Visual Basic Error:
:
: Run-time error: 40006
:
: "Wrong protocol or connection state for the requested transaction or request."
:
: CAUSE
: The code is trying to call the SendData method before the port has actually been connected. The Connect method in Visual Basic is asynchronous and is more like a request to connect to the Winsock port.
:
: RESOLUTION
: The code must wait until the Connect event is fired before attempting to call the SendData or GetData methods. The Connect event is a signal that the connect request has been accepted and the connection is established.
:
: STATUS
: This behavior is by design.
:
: http://support.microsoft.com/default.aspx?scid=kb;en-us;183987
:
: ------
: : I had a small doubt about Winsock! I created a small chat program using WinSock. The program works fine when I test it on my local machine but when I try to connect to a remote machine I end up with a run time error 40006.
: : Why does this happen? Could it be because of a firewall? I so how do i make it ask for the users permission to grant access for the required port? Or is there a better way of doing this
: :
: : Thanks
: :
:
:
Good luck!
:
Hackman
:
: