|
-
Jul 13th, 2000, 12:06 AM
#9
Addicted Member
yep you can send messages to your self by using the 127.0.0.1 ip address, its the way I usually test any client and server program I make.
make the server listen on a certain port, and make the client project connect to 127.0.0.1 on that port. to get the data recieved use the data arrival event in the winsock control and in that event put something like
Dim IncomingData as string
winsock1.getdata IncomingData
Msgbox IncomingData.
if you are still looking for a winsock example here is something extremely simple a one on one client server program
[CODE]'Start a new project make it the server
'the server code.
Private Sub Form_Load()
Winsock1.LocalPort = 5555 'sets the local port
Winsock1.Listen 'tells the winsock control to listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckConnected Then Winsock1.Close 'if the socket isn't already connected then close the winsock to accept the request
Winsock1.Accept requestID 'accept the connection request
Winsock1.SendData "Welcome to the server" 'sends some testdata
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim ServerData As String
Winsock1.GetData ServerData 'Gets the data
MsgBox ServerData 'Displays it
End Sub
'Now load another instance of vb, in which the client code will go.
'Client code
Private Sub Command1_Click()
Winsock1.Connect "127.0.0.1", 5555 'instruct it to connect
End Sub
Private Sub Winsock1_Connect()
MsgBox "Connected" 'When a connection is established
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim ClientData As String
Winsock1.GetData ClientData 'gets the data sent
MsgBox ClientData 'displays the data
End Sub
'Now run the server, then run the client and try it out
'If you want to make server that supports multiple connections that also pretty easy to do.
'Set your servers winsock control index property to 0
'then in the general declaration section place this code:
'and replace all your server code with this:
Dim AmntConnect As Integer
Private Sub Form_Load()
Winsock1(Index).LocalPort = 5555 'sets the local port
Winsock1(Index).Listen 'tells the winsock control to listen
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
AmntConnect = AmntConnect + 1
Load Winsock1(AmntConnect) 'loads the winsock control
Winsock1(AmntConnect).LocalPort = 0 'sets the port to a port that isn'g being used
Winsock1(AmntConnect).Accept requestID 'accept the request
Winsock1(AmntConnect).SendData "Welcome to the server" 'just sends some data
End If
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim ServerData As String
Winsock1(Index).GetData ServerData 'Gets the data
MsgBox ServerData 'Displays it
End Sub
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
|