Network novice question.
Need I use 2 TCPSockets or can I use 1 for Write and Listen ?
I find the best way to teach people how to use TCP is to ask them to think of it like a telephone conversation
Where telephones allow humans to communicate, TCP Sockets allow applications to communicate.
+ A TCP socket is a virtual telephone.
+ IP addresses and ports are 'just' the virtual phone numbers for calling these virtual telephones.
+ An IP is the number for the host device (PC) and a Port identifies a particular socket on the device.
So, at the far end we have an application which is 'on-hook' and listening for a call.
At the local end we have an application which wants to communicate with the far end.
The local end picks up it's virtual phone (socket) and dials the far end (IP and Port), which it does by calling .connect.
Providing the number is right, the network does it's stuff and the call reaches the application listening at the far end.
The far end hears it's virtual phone ringing, picks up and the near and far end applications are connected.
Just like a phone call;
+ A call may fail to connect for a number of different reasons.
+ It takes time for the network to connect the call.
+ Once connected, both applications can talk to the other, while they remain connected.
+ Both applications must speak the same language (protocol).
Example : I need to send string "STATUS" to app on TCP port 7001 and need to receive results on same port.
Code examples are welcome.
This is off the top of my head, so it may contain typos
//done for you when you add a TCP Socket to a form
Dim Socket1 as New TCPSocket
// populate the events we want to respond to
If me.LastErrorCode = 102 then
Listbox1.Addrow "Socket disconnected"
Listbox1.AddRow ("Socket Error: " + cstr(me.LastErrorCode)
Listbox1.AddRow ("Connected to host " + me.RemoteAddress)
Listbox1.AddRow ("Saying 'STATUS'")
Dim data As String
While me.BytesAvailable > 0
data = data + me.ReadAll
Listbox1.Addrow "Received '" + data + "'"
//Initiate a call
const remoteHost = "192.168.0.1"
const remoteIsListeningOnPort = 7001
//The 'number' of the application we want to call
Socket1.Address = remoteHost
Socket1.Port = remoteIsListeningOnPort
ListBox1.AddRow ("Calling " + me.Address + ":" + cstr(me.Port))
//Make the call
const timeOut = 5000000
dim toStart as double
toStart = Microseconds
Loop Until (Socket1.IsConnected) or (Microseconds - toStart > timeOut)
If Not Socket1.IsConnected Then
ListBox1.Addrow("Call failed to connect within " + cstr(timeOut / 1000000) + " seconds.")
Now remember what I said about both applications needing to speak the same language. Most application protocols require some sort of delimiter. Experience leads me to think you may need to send "STATUS" + EndOfLine to get the far end to respond.