I'm creating a client-server style program.

The two connect fine and I can send data from the client to the server, The server gets the information correctly

However when I try and send data from the server to the client, the client receives empty packets. Upon closer inspection I've noticed that the clients local port is random and not what I set it too (usually around the 1200. When connecting I have set it to use port 1982.

TCPIP.connect "127.0.0.1", 1982

Does anyone have any ideas, suggestions on this one?

I'm doing this on the same computer, (which I've never had trouble with before).
Do you think
a.) That a fire wall could be interfaring?
b.) It is confused, as there is already a program taking that port, and maybe I should try it on two different computers?

Note
------------
I've made three programs,
* Client,
* Server,
* Management tool.

The server and client play nice with each other. So I know that it's not an code issue with the server side. I've copied the code from the server code to put in the management tool. but with same effect

If the port is open and being used by another application at a particular time, it could interfere yes.
I would not say it is a firewall issue if you are able to use the client/server combination OK, its only when the management tool is involved.

Perhaps the server has a mind of its own and doesn't want to be managed!! ;)

Apologies if my questions are a bit basic as it were, but I'm in the process of learning it all myself, but have been fortunate enough for mine to be working well it seems :)

I've been starting at Winsock coding for a while now and it is driving me around the banana Tree. I created a temp server/client and the same thing has happened.

Since I have been staring at the code for a while, I wonder if fresh eyes can spot anything wrong.

Have I done something wrong in the coding?

Server side

'temp project to test winsock

'-----------------------------------------------
Private Sub Command2_Click() ' send data to client
If Winsock1.State = sckConnected Then
    Winsock1.SendData (txt2Send)
Else
    Text1.Text = Text1.Text + vbCrLf & "no send - Not connected"
End If
End Sub
'-----------------------------------------------
Private Sub Form_Load()
    Winsock1.Close
    Winsock1.LocalPort = txtPort
    Winsock1.Listen
     Text1.Text = Text1.Text + vbCrLf & "listening on port " & Winsock1.LocalPort
End Sub
'-----------------------------------------------
Private Sub Winsock1_Connect()
    Text1.Text = Text1.Text + vbCrLf & " Connected"
End Sub
'-----------------------------------------------
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    Text1.Text = Text1.Text + vbCrLf & " Connection Request"
    Winsock1.Close
    Winsock1.Accept requestID
End Sub
'-----------------------------------------------
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim temp As String
    
    Winsock1.GetData temp
    Text1.Text = Text1.Text + vbCrLf & temp
End Sub

Client Side

'temp project to test winsock

Private Sub Command1_Click() ' send text
    If Winsock.State = 7 Then
        Winsock.SendData txtSend
        Call output("Sending " & txtSend)
    Else
        Call output("Not connected, can't send")
    End If
End Sub
'-----------------------------------------------
Private Sub connect_Click()
    Call output("trying to connect")
    Winsock.Close
    Winsock.connect txtAddress, txtPort
End Sub
'-----------------------------------------------

Private Sub output(ByVal data As String)
    Text4 = Text4 + vbCrLf & data
    Text4.SelStart = Len(Text4)
End Sub
'-----------------------------------------------

Private Sub Form_Load()
Call output("Form loaded")
End Sub
'-----------------------------------------------

Private Sub Winsock_Connect()
    Call output("Connected")
    Call output("Local port " & Winsock.LocalPort)
End Sub
'-----------------------------------------------

Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim strnewdata As String
    Winsock.GetData (strnewdata)
    Call output("data received " & strnewdata)
End Sub

Hi,

I've just replicated your app using the code you have supplied and it seems to be working fine for me.
I am using the default Windows firewall (I'm behind a router hence I don't need anything more secure than that :)) and VB6.exe is set to have access through it if needed.

I've attached a screenshot to show it working...

One thing I have found (and have had to include a fair bit of error handling and winsock.state checking on my app) is that if you close the client side while the connection is active, but you dont close the server side, when you re-load the client, it will not connect to the server.

I would say double check your firewall (if it is a software one on your machine) that VB6.exe is allowed access through (VB6 needs access if you are just running the project without compiling to a standalone .exe - incase you weren't sure why it needed access). Try again once you are sure it will be OK with the firewall. If still a problem, let me know and I'll strip out the winsock code from the app I am working on and post it up (I have added a load of nonsense to get the server to run commands and send back the results etc which you may not need - I could include this if preferred though).

Hope that helps anyway...

OK, 5 days, and many hours trying everything I can think of. I got it worked out

Thanks to Jonifen, I worked out that it was the client side that was having troubles.

I changed one line, which in the tcpip_receive()

from
Winsock1.GetData temp

TO

call winsock.getdata (temp, vbstring)

and dance the hoola it now works, This is really weird as I've never done this before, and even more so since it from code worked for jonifen.

Thanks Jonifen!!, you rock!
(if I could, I'd buy you a beer!)

I'm glad you got it working in the end :)

Although I'm genuinely unsure how much I helped though, as I didn't even spot the line of code which you changed to resolve the problem. I'm still learning a lot of things in VB and I have to admit, its helped me aswell on my own app by reading through other code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.