For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > February 2005 > Hi to all









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Hi to all
Παππας Κωνσταντίνος

2005-02-09, 8:59 am

Hi to all

I would a bit of help here. I have a server client simpler application but
seems could not connected. I do not know why. I am not an expert in visual
basic so I would like to understand what happening here. I try to transmit a
simple text file from server to client with 2 separated socket on for data
transmission and one for confirmations that the data arrived. I would like
to inform me if there is a bug in my program or if I made a mistake
somewhere. I count on your help. Thanks in advance.



Server





Option Explicit



Dim clientnum As Integer

Dim data As String

Dim help As String



Private Sub Command1_Click()

Dim file As Integer

file = FreeFile

Open "c:\text.txt" For Input As #file

Input #file, data

While (data <> "EOF") And (ws2.State = sckConnected)

ws2.SendData data

If help <> "confirm" Then

ws2.SendData data

End If

Wend

data = "EOF"

ws2.SendData data

Close #file

Text1.Text = "transmition completed"

End Sub





Private Sub Form_Load()

clientnum = 0

Text1.Text = ""

ws1.Close

ws1.Protocol = sckTCPProtocol

ws1.LocalPort = 550

ws1.Listen

Text1.Text = "Waiting for clients to connect on me........."

ws2.Close

ws2.Protocol = sckTCPProtocol

ws2.RemoteHost = "127.0.0.1"

ws2.RemotePort = 560

ws2.Connect

If ws2.State <> sckConnected Then

Text1.Text = "Error"

clientnum = MsgBox("Error", vbCritical, "Error in connection")

End If

Unload Me

End Sub



Private Sub ws1_ConnectionRequest(ByVal requestID As Long)

If ws1.State <> sckConnected Then

ws1.Close

End If

ws1.Accept requestID

Text1.Text = "client" + CStr(clientnum + 1) + " connected"

End Sub



Private Sub ws1_DataArrival(ByVal bytesTotal As Long)

ws1.GetData help, vbString

If help <> "confirm" Then

ws2.SendData data

End Sub



Client



Option Explicit

Dim data, help As String

Dim file, msg As Integer



Private Sub Form_Load()

file = FreeFile

Open "c:\text1.txt" For Output As #file

Text1.Text = ""

Win1.Close

Win1.LocalPort = 560

Win1.Listen

Win2.Close

Win2.RemotePort = 550

Win2.RemoteHost = "127.0.0.1"

Win2.Connect

If Win2.State <> sckConnected Then

msg = MsgBox("unable to connect", vbApplicationModal, "Error on TCP/IP")

End If

End Sub

Private Sub Win1_ConnectionRequest(ByVal requestID As Long)

If Win1.State <> sckConnected Then

Win1.Close

Win1.Accept requestID

End If

Text1.Text = "connected with the server"

End Sub

Private Sub Win1_DataArrival(ByVal bytesTotal As Long)

help = "confirm"

Win1.GetData data, vbString

If Win2.State <> sckConnected Then

msg = MsgBox("unable to connect", vbApplicationModal, "Error on TCP/IP")

GoTo err

End If

Win2.SendData help

data = data + vbString + vbCtrlf

Print #file, data

If data = "EOF" Or "vbEOF" Then

Close #file

End If

err:

End Sub


Παππας Κωνσταντίνος

2005-02-09, 4:01 pm

new version same problem

client

Option Explicit
Dim data, help As String
Dim file, msg As Integer

Private Sub Form_Load()
file = FreeFile
Open "c:\text1.txt" For Output As #file
Text1.Text = ""
Win2.Close
Win2.LocalPort = 560
Win2.Listen
Win1.Close
Win1.RemotePort = 550
Win1.RemoteHost = "127.0.0.1"
Win1.Connect
If Win1.State <> sckConnected Then
msg = MsgBox("unable to connect", vbApplicationModal, "Error on TCP/IP")
Unload Me
End If

End Sub
Private Sub Win1_ConnectionRequest(ByVal requestID As Long)
If Win1.State <> sckConnected Then
Win1.Close
Win1.Accept requestID
End If
Text1.Text = "connected with the server"
End Sub
Private Sub Win1_DataArrival(ByVal bytesTotal As Long)
help = "confirm"
Win1.GetData data, vbString
Win2.SendData help
data = data + vbString + vbCtrlf
Print #file, data
If data = "EOF" Or "vbEOF" Then
Close #file
End If
End Sub

Private Sub Win2_ConnectionRequest(ByVal requestID As Long)
If Win2.State <> sckConnected Then
Win2.Close
Win2.Accept requestID
End If
Text1.Text = "server connected with Client"
End Sub

server

Option Explicit
Dim clientnum As Integer
Dim data As String
Dim help As String

Private Sub Command1_Click()
Dim file As Integer

file = FreeFile
Open "c:\text.txt" For Input As #file
Input #file, data
While (data <> "EOF") And (ws2.State = sckConnected)
ws2.SendData data
If help <> "confirm" Then
ws2.SendData data
End If
Wend
data = "EOF"
ws2.SendData data
Close #file
Text1.Text = "transmition completed"
End Sub


Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 10000
clientnum = 0
Text1.Text = ""
ws1.Close
ws1.Protocol = sckTCPProtocol
ws1.LocalPort = 550
ws1.Listen
Text1.Text = "Waiting for clients to connect on me........."

End Sub

Private Sub Timer1_Timer()
Call check
End Sub

Private Sub ws1_ConnectionRequest(ByVal requestID As Long)
If ws1.State <> sckConnected Then
ws1.Close
End If
ws1.Accept requestID
Text1.Text = "client" + CStr(clientnum + 1) + " connected"
Timer1.Enabled = True
End Sub
Private Sub ws1_DataArrival(ByVal bytesTotal As Long)
ws1.GetData help, vbString
If help <> "confirm" Then
ws2.SendData data
End Sub

Public Sub check()
If ws1.State = sckConnected Then
ws2.Close
ws2.RemoteHost = "127.0.0.1"
ws2.RemotePort = 560
ws2.Connect
Timer1.Enabled = False
If ws2.State <> sckConnected Then
clientnum = MsgBox("Error on TCP/IP connection", vbOKOnly, "Connection
error")
Unload Me
End If
End If
End Sub
"Παππας Κωνσταντίνος" <pappaskostas@ath.forthnet.gr> wrote in message
news:#I6Fi#oDFHA.4052@TK2MSFTNGP15.phx.gbl...
> Hi to all
>
> I would a bit of help here. I have a server client simpler application but
> seems could not connected. I do not know why. I am not an expert in visual
> basic so I would like to understand what happening here. I try to transmit

a
> simple text file from server to client with 2 separated socket on for data
> transmission and one for confirmations that the data arrived. I would like
> to inform me if there is a bug in my program or if I made a mistake
> somewhere. I count on your help. Thanks in advance.
>
>
>
> Server
>
>
>
>
>
> Option Explicit
>
>
>
> Dim clientnum As Integer
>
> Dim data As String
>
> Dim help As String
>
>
>
> Private Sub Command1_Click()
>
> Dim file As Integer
>
> file = FreeFile
>
> Open "c:\text.txt" For Input As #file
>
> Input #file, data
>
> While (data <> "EOF") And (ws2.State = sckConnected)
>
> ws2.SendData data
>
> If help <> "confirm" Then
>
> ws2.SendData data
>
> End If
>
> Wend
>
> data = "EOF"
>
> ws2.SendData data
>
> Close #file
>
> Text1.Text = "transmition completed"
>
> End Sub
>
>
>
>
>
> Private Sub Form_Load()
>
> clientnum = 0
>
> Text1.Text = ""
>
> ws1.Close
>
> ws1.Protocol = sckTCPProtocol
>
> ws1.LocalPort = 550
>
> ws1.Listen
>
> Text1.Text = "Waiting for clients to connect on me........."
>
> ws2.Close
>
> ws2.Protocol = sckTCPProtocol
>
> ws2.RemoteHost = "127.0.0.1"
>
> ws2.RemotePort = 560
>
> ws2.Connect
>
> If ws2.State <> sckConnected Then
>
> Text1.Text = "Error"
>
> clientnum = MsgBox("Error", vbCritical, "Error in connection")
>
> End If
>
> Unload Me
>
> End Sub
>
>
>
> Private Sub ws1_ConnectionRequest(ByVal requestID As Long)
>
> If ws1.State <> sckConnected Then
>
> ws1.Close
>
> End If
>
> ws1.Accept requestID
>
> Text1.Text = "client" + CStr(clientnum + 1) + " connected"
>
> End Sub
>
>
>
> Private Sub ws1_DataArrival(ByVal bytesTotal As Long)
>
> ws1.GetData help, vbString
>
> If help <> "confirm" Then
>
> ws2.SendData data
>
> End Sub
>
>
>
> Client
>
>
>
> Option Explicit
>
> Dim data, help As String
>
> Dim file, msg As Integer
>
>
>
> Private Sub Form_Load()
>
> file = FreeFile
>
> Open "c:\text1.txt" For Output As #file
>
> Text1.Text = ""
>
> Win1.Close
>
> Win1.LocalPort = 560
>
> Win1.Listen
>
> Win2.Close
>
> Win2.RemotePort = 550
>
> Win2.RemoteHost = "127.0.0.1"
>
> Win2.Connect
>
> If Win2.State <> sckConnected Then
>
> msg = MsgBox("unable to connect", vbApplicationModal, "Error on TCP/IP")
>
> End If
>
> End Sub
>
> Private Sub Win1_ConnectionRequest(ByVal requestID As Long)
>
> If Win1.State <> sckConnected Then
>
> Win1.Close
>
> Win1.Accept requestID
>
> End If
>
> Text1.Text = "connected with the server"
>
> End Sub
>
> Private Sub Win1_DataArrival(ByVal bytesTotal As Long)
>
> help = "confirm"
>
> Win1.GetData data, vbString
>
> If Win2.State <> sckConnected Then
>
> msg = MsgBox("unable to connect", vbApplicationModal, "Error on TCP/IP")
>
> GoTo err
>
> End If
>
> Win2.SendData help
>
> data = data + vbString + vbCtrlf
>
> Print #file, data
>
> If data = "EOF" Or "vbEOF" Then
>
> Close #file
>
> End If
>
> err:
>
> End Sub
>
>



Bob Butler

2005-02-09, 4:01 pm

"Παππας Κωνσταντίνος" <pappaskostas@ath.forthnet.gr> wrote in message
news:%23fkCdEsDFHA.1936@TK2MSFTNGP14.phx.gbl
> Win1.Connect
> If Win1.State <> sckConnected Then


Connection requests take time and run asynchronously so you can't just test
immediately after trying to connect. You either have to use a loop that
checks the State property repeatedly or wait for the Connected or Error
event to trigger.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

?appa? ???sta?t????

2005-02-10, 4:00 am


Thanks Bob. I'll try
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:OlIuNHsDFHA.3928@TK2MSFTNGP15.phx.gbl...
> "Παππας Κωνσταντίνος" <pappaskostas@ath.forthnet.gr> wrote in message
> news:%23fkCdEsDFHA.1936@TK2MSFTNGP14.phx.gbl
>
> Connection requests take time and run asynchronously so you can't just

test
> immediately after trying to connect. You either have to use a loop that
> checks the State property repeatedly or wait for the Connected or Error
> event to trigger.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>



?appa? ???sta?t????

2005-02-10, 4:00 am

Dear Bob,

At the client program the connection establish but at the server point the
state of socket stuck on connecting state (6) and never goes at
sckConnected. Could you explain me why that could happened? I run both in my
machines so all socket takes the local IP address. I do not know if that
causes troubles and I need a second machine to test the programs. Could I
run server and client application in the same machine (with total 4 socket
opens at the same time ?)



Thanks in advance



"Bob Butler" <tiredofit@nospam.com> wrote in message
news:OlIuNHsDFHA.3928@TK2MSFTNGP15.phx.gbl...
> "Παππας Κωνσταντίνος" <pappaskostas@ath.forthnet.gr> wrote in message
> news:%23fkCdEsDFHA.1936@TK2MSFTNGP14.phx.gbl
>
> Connection requests take time and run asynchronously so you can't just

test
> immediately after trying to connect. You either have to use a loop that
> checks the State property repeatedly or wait for the Connected or Error
> event to trigger.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com