Solved

Connecting 2 applications through internet by using winsock 6.0 SP5

Posted on 2004-09-17
24
257 Views
Last Modified: 2013-11-13
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
Comment
Question by:JohnWoo
  • 10
  • 9
  • 2
  • +1
24 Comments
 
LVL 5

Expert Comment

by:abith
ID: 12082918
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
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12083458
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12084027
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12086008
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12086407
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12086470
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12089579
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12089607
>> 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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12096234
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12096285
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12096310
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Author Comment

by:JohnWoo
ID: 12096312
yup...........I only applied one winsock on the server program
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12096501
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12096516
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12097707
Yup...that's the CSocket I am using
0
 
LVL 2

Author Comment

by:JohnWoo
ID: 12098099
oops.........I have double check my code........I am not using CSocket actually.......I am using Winsock API
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12098347
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12099740
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 12101822
That shouldn't matter, you just reference the control by prepending it with the form name:

    Form1.Winsock1.Connect IP, Port

Idle_Mind
0
 
LVL 2

Author Comment

by:JohnWoo
ID: 12723448
I wish to delete this question and have my points refund......Thanks
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12723478
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
 
LVL 2

Author Comment

by:JohnWoo
ID: 12732215
seems fair enough base on what you said...ok....I'll award those points to him then
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now