• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Connecting 2 applications through internet by using winsock 6.0 SP5

Hi,
  I am developing 2 applications that connects with each other through the internet by using internet IP. I have tried that out with winsock 6.0 (SP5) but it don't seems to be working. As far as my past experience, connecting applications through the internet by using winsock is possible and can be easily achieved. I have tried searching for updates/news regarding to the changes microsoft made to the winsock component when they release the SP5 for Visual Studio but can't get a clue. Can anyone tell me what is going on?? Thanks
0
JohnWoo
Asked:
JohnWoo
  • 10
  • 9
  • 2
  • +1
1 Solution
 
abithCommented:
Hai John

winsock control is used only for socket connection and it always uses port to connect to the internet. You may get more details about the winsock in the below mentioned address.

WinsockVB.com
socks5.com

regards
Abith
0
 
RejojohnyCommented:
could u post the code that u have tried and what is the problem that u r facing ... if u want to connect from A to B, then B has to listen at a particular port to accept connections and A and to connect to that same port ..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi JohnWoo,

Please explain what your finished application should do and whether or not you are getting any errors.  Also, as Rejojohny requested, please post your code and perhaps we can find out what is wrong with it.

Regards,

Idle_Mind
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
JohnWooAuthor Commented:
Sorry.......guess I was in a rush. Anyway, here is my code snippets:

Server code:

Private Sub Form_Load()
 winServer(0).Close
 winServer(0).LocalPort = 100
 winServer(0).Listen
 
 connectionNum = 10 'max number of connection allowed
 
 For countConnection = winServer.UBound + 1 To connectionNum - 1
  Load winServer(countConnection)
 Next
End Sub

Private Sub winServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
 winServer(Index).Close
 winServer(Index).Accept requestID
 Code(Index) = ""
 initial(Index) = ""
 UsrID(Index) = ""

MakeListen
End Sub

Public Sub MakeListen()
 Dim i As Integer
   
 For i = 0 To connectionNum - 1
  If winServer(i).State = sckClosed Then
   winServer(i).Listen
   Exit Sub
  End If
 Next i
End Sub
-------------------------------------------------------------------------------------------------------------------
Client code:

 If frmUpload.WskInternet.State = sckConnected Then
        Else
         frmWait.Show
         frmUpload.WskInternet.Connect ServerIp, ServerPort
         
         Do While frmUpload.WskInternet.State <> sckConnected

          frmUpload.WskInternet.Close
          frmUpload.WskInternet.Connect ServerIp, ServerPort
       
          DoEvents
       
          LoopConn = LoopConn + 1
       
          If LoopConn = 5171 Then
            If MsgBox("The server is busy. Do you still want to connect?",
                            vbYesNo, "Upload/Download") = vbNo Then End
            LoopConn = 0
          End If
         Loop
       
         If frmUpload.WskInternet.State = sckConnected Then
       
          frmUpload.WskInternet.SendData "SiteCode:" & VeriProCode
       
          DoEvents
          DoEvents
          DoEvents
   
         Else
          MsgBox "The connection session is disconnect.Please restart the application.",
                      vbCritical, "Upload/Download"
         End If
        End If
-------------------------------------------------------------------------------------------------------------------

I am trying to perform data synchronization between 2 different places through the internet. With this purpose, I have developed 2 applications (the server and client). The client application is able to upoad/download data to/from the server program through the internet. I am using FTP for the download and upload process though but I need the winsock conncetion so that the server could verify the client that is trying to connect to it. but the client failed to get response from the server with the connection time out message. As I debug through both the programs the server did receive a connection request from the client but it returns a connection time out error message and teh server just failed to process the connection without any reasons that I have know which will make the program behaves such a manner.  
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Server Side:
You only need to have Index 0 loaded in the form.  Then, you should only be listening on Index 0, and accept any incoming connections on the higher indices as shown below.  You don't need your MakeListen() sub.

Private Sub winServer_ConnectionRequest(index As Integer, ByVal requestID As Long)
    Dim acceptIndex As Integer
    Dim curindex As Integer
   
    acceptIndex = -1
    If index = 0 Then
        For curindex = 1 To winServer.UBound
            If winServer(curindex).State = sckClosed Then
                acceptIndex = curindex
                Exit For
            End If
        Next curindex
    End If
   
    If acceptIndex = -1 Then
        acceptIndex = winServer.Count
        Load winServer(acceptIndex)
    End If
   
    winServer(acceptIndex).LocalPort = 0
    winServer(acceptIndex).Accept requestID
End Sub

Private Sub winServer_Close(index As Integer)
    Dim curindex As Integer
   
    winServer(index).Close    
    Do While winServer(winServer.UBound).State = sckClosed
        Unload winServer(winServer.UBound)
    Loop
End Sub



Client Side:

Here is a different way to handle the connection process:

Private Sub Command1_Click()
    WskInternet.Connect ServerIp, ServerPort
End Sub

Private Sub WskInternet_Connect()
    ' This event will fire after the connection has been made.
    ' Continue your code in here...
    WskInternet.SendData "SiteCode:" & VeriProCode
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If something goes wrong with the connection or the connection attempt failed, you will get an error event.  Check the help file for all the possible values of Number.

Private Sub WskInternet_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)
    Select Case Number
        Case sckTimedout, sckConnectAborted
   
        Case sckConnectionRefused
       
        Case Else
       
    End Select
End Sub
0
 
JohnWooAuthor Commented:
Does the way you are suggesting enable multiple client connections to the server?? Cause I might be using more than one client to connect to the server at the same time.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> Does the way you are suggesting enable multiple client connections to the server??

Absolutely!

It is all handled in the ConnectionRequest() event.  The code I gave you does not limit the number of connections however, but it could be easily modified to do so.  You only start out with one winsock control (Index 0) loaded in the array.

When a connnection request comes in, the algorithm attempts to find the index of an already loaded winsock control that is no longer being used for a connection (client disconnected already).  If it doesn't find one, then it loads a new instance of the winsock control at the end of the control array using:

    Load winServer(acceptIndex)

When a client disconnects, the winsock control servicing their connection is closed, but not necessarily immediately unloaded.  The code only unloads winsock controls from the end of the control array that are in the sckClosed state.  This prevents holes from forming in the control array.  When all clients have disconnected, the only winsock control left loaded will be Index 0, which is always in the listening state.

Regards,

Idle_Mind
0
 
JohnWooAuthor Commented:
hmm.........Have exactly what you suggested but the connection is still not working with the server giving the soocket error of "connection aborted". Its' weird though cause The server did pick up a connection request from the client but just couldn't process it. I have debug through the ".accept" method of the server socket and it past through the execution without any problem. However the execution trace was lost after the server socket finished firing the connection request event and it fires socket error event instead.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok.

Did you remove all the other Winsock controls from your form and only leave one on with its Index set to zero?

Idle_Mind
0
 
JohnWooAuthor Commented:
Actually, I have tried to use CSocket class to write the connection instead but I encountered some problems. For instance it is common that when we use the winsock component, the data arrival event tends to invoke automatically when ever the component detects any data arrival from teh other sidet. However, I can't do that with the CSocket class. It appears to me that the CSocket class was unable to detect when there is going to be a data arrival process from the other side. In order to achieve the data arrival event in the winsock component, I have tried using timer to check if there is any data arriving from the other side from time to time but it is not working either as the program will hang/freeze due to too much memory consumption (just my guessing) by repeating the checking process a lot of times. O my question is can we actually write the CSocket class to behave like teh winsock component such as knowing when there will be data arriving by itself??    
0
 
JohnWooAuthor Commented:
yup...........I only applied one winsock on the server program
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you using this CSocket?
http://www.vbip.com/winsock-api/csocket-class/csocket-class-01.asp

I've never tried it myself.

Idle_Mind
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What I would do is write a simple test client/server app and pass info from textboxes back and forth to make sure you have the algorithms correct.

I'll post a sample app later today.

Idle_Mind
0
 
JohnWooAuthor Commented:
Yup...that's the CSocket I am using
0
 
JohnWooAuthor Commented:
oops.........I have double check my code........I am not using CSocket actually.......I am using Winsock API
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I've never tried the Winsock API approach.  Can't give you much help there.  The code complexity goes up several notches.

Idle_Mind
0
 
JohnWooAuthor Commented:
hmm...........I think I might know what's the problem.....but let me confirm the issue first. Is it possible to place the winsock component on a form and to initialize the connection to the server in other form, which is any form except the form where the winsock component is place on (all forms will be in the same project)???
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That shouldn't matter, you just reference the control by prepending it with the form name:

    Form1.Winsock1.Connect IP, Port

Idle_Mind
0
 
JohnWooAuthor Commented:
I wish to delete this question and have my points refund......Thanks
0
 
RejojohnyCommented:
my opinion.... Idle_mind has really worked overtime trying to solve ur problem and the last question of urs seems to have been answered by him .. u could accept one of his comments ..
0
 
JohnWooAuthor Commented:
seems fair enough base on what you said...ok....I'll award those points to him then
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now