# Solved: SendData using WinSock Control in Vb6



## deepmadan (May 20, 2011)

I am developing an application in VB6 which will communicate using UDP Connection to a hardware which recieves and sends data using UDP Connection. My Hardware works fine. It has already been tested with an application written in VisualC using UDP connection only. In VisualC, I had working on UDP Connection without Winsock.
Hardware's Remote IP and Port is been set to my computer's IP and Port 4040.

My code for connection and sending data is as below :


```
If Winsock1.State = sckClosed Then
   Winsock1.Protocol = sckUDPProtocol
   Winsock1.LocalPort = 4040    
   Winsock1.RemoteHost = "192.168.1.80"  'Hardware IP
   Winsock1.RemotePort = 4040               'Hardware's Port
   Winsock1.connect
   Winsock1.SendData "Hello"
Else
   Winsock1.Close
End If
[\code]

I debugged this code and also the hardware and i found that
  1. A Packet is sent to the hardware after WinSock1.connect command is executed.
  2. But no Packet is been send after SendData Command as it requires Winsock1 in Connected State.

I want to know if i could senddata using winsock without my connection being accepted by the remote machine(hardware).
I am even able to recieve messages from Hardware on the DataArrival Function.
```


----------



## pvc_ (Feb 18, 2008)

it's been a while since I've dealt with udp, but one thing that looks a bit suspicious to me is the fact that both localport and remote port are the same. I don't think you need to set the local port since windows usually uses an available local port and you can't use the same port for both local and remote.


----------



## deepmadan (May 20, 2011)

I removed the line WinSock1.LocalPort = 4040

I just found out that...
after WinSock1.Connect a Packet is recieved at the hardware (microcontroller) with length of 40 bytes. Packet contains my local ip and remote ip 192.168.1.80

But after WinSock1.SendData a Packet do gets recieved at the hardware but its length is 0.

I am debugging the hardware at the first layer only.


----------



## pvc_ (Feb 18, 2008)

are you using a packet sniffer?


----------



## deepmadan (May 20, 2011)

i could not get you..???


----------



## deepmadan (May 20, 2011)

No i am not a packet sniffer.. the hardware is developed by us only. And we need an application communicating with it on VB. Earlier it was on VisualC. Now we are developing it in VB6


----------



## pvc_ (Feb 18, 2008)

lol. packet sniffer is a tool that lets you see what packets you are sending or receiving. CommView is a good one. You could use it to see if your packets are being sent correctly or whatnot...


----------



## deepmadan (May 20, 2011)

hhehe ... lol... thanks for the info...

i just tried sending those packets with remotehost as localhost.. and i found that at data arrival it is giving runtime error of connection reset. as i m not in listen mode....

maybe thats the problem i am facing in hardware communication...as my hardware is not in listen mode.. so it gets a alert of incoming packet but the size goes to zero due to error...

my hardware reads all the incoming packets on its ip and of all ports.... and inside the application it checks for port and other packet details....

is there any way i could send packet to it.. without it being in listen mode....


----------



## pvc_ (Feb 18, 2008)

> Creating a UDP application is even simpler than creating a TCP application because the UDP protocol doesn't require an explicit connection. In the TCP application above, one Winsock control must explicitly be set to "listen," while the other must initiate a connection with the Connect method.
> 
> In contrast, the UDP protocol doesn't require an explicit connection. To send data between two controls, three steps must be completed (on both sides of the connection):
> 1.Set the RemoteHost property to the name of the other computer.
> ...


----------



## deepmadan (May 20, 2011)

Thanks for the help.. But i have already checked and tried this link.. still no success..

I tried using ws2_32.dll and including WSAStartup, connect ,send . recv, etc functions... send functions returns accurately packet length sent. but at the recieving end of my hardware. the first layer shows packet recieved with length zero....


I found an error while searching online....
In winsock control i tried sending the packet to remotehost as "localhost" and in dataarrival i check for recieved packet. in datasend it shows no error but in data arrival at getdata it shows error of socket/connection reset. and its error cause at that site written was reciever not in listen mode.


----------



## pvc_ (Feb 18, 2008)

if the socket on the remote machine binds correctly, I don't see why your connection would be denied. The only thing I can suggest is using a packet sniffer to see what it is that you are actually exchanging.


----------



## deepmadan (May 20, 2011)

I installed a packet sniffer to check what data is being sent.. I found that packet sent is correct...
but it is not reaching the hardware. correctly

I compared the packet sent by my vb code.... and the packet sent by c code which gets accepted by hardware... both the packets info to be sent are same in both.. the difference i could get is in my code LocalIP comes first and then RemoteIP in vb packet ...... while in packet sent by C... RemoteIP comes first then LocalIP.


Also in VB i am using ws2_32.dll files for my connection 
Whereas in Hardware and C code, windows.h file was included


----------



## deepmadan (May 20, 2011)

If you need i can post the two packets here...


----------



## deepmadan (May 20, 2011)

ThankYou PVC_ ....  

It is working now... Packet Sniffer helped in debuging and comparing both the packets.,..


----------



## pvc_ (Feb 18, 2008)

I'm glad to hear that. For these types of projects, a great packet sniffer\monitor would make a world of difference.


----------



## deepmadan (May 20, 2011)

Yes... It removed my load of debugging....


----------

