Hi everyone,
I was wondering if anyone can help me. I got two forms I can send data from one form to the other one when I press the SEND button. I’m trying to get data from one form to the other when I press the ‘GET’ button, to get an idea what I’m trying to do have a look at this image:

http://i389.photobucket.com/albums/oo336/neovo-88/VB.jpg

Hopefully it will make it more clear what I’m trying to do. Also before anything is done you have to press the ‘CONNECT’ button, to link to the forms first.

Here the code I used so far:

Form1

Private Sub Form_Load()
tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.Show
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
If tcpServer.State <> sckClosed Then tcpServer.Close
tcpServer.Accept requestID
End Sub
'Listens for data from form 1 and displays it
Private Sub tcpServer_DataArrival(ByVal bytestotal As Long)
Dim strData As String
tcpServer.GetData strData
txtInput.Text = strData
End Sub
Private Sub timer1_timer()
Label1.Caption = tcpServer.State
If tcpServer.State = 0 Then tcpServer.Listen
End Sub

Form 2

Private Sub Form_Load()
tcpClient.RemoteHost = "localhost"
tcpClient.RemotePort = 1001
End Sub
'Connect Form 2 to Form 1
Private Sub cmdConnect_Click()
tcpClient.Connect
End Sub
'Sends data from form 1 to Form 1
Private Sub Command1_Click()
tcpClient.SendData txtOutput.Text
End Sub
Private Sub timer1_timer()
Label1.Caption = tcpClient.State
End Sub

Any help will much appreciated

Thanks

Rich

Recommended Answers

All 4 Replies

Check these examples
Client/Server Socket classes for file transfer
Chatting Application Using DotNet
to get started with sockets.

Personally I would write two classes. First one to be a "server" (listener) and the second one "client" (connects to the listener). Then drop server and the client objects to both forms and start servers (listeners). Now either of the forms can start a client which connects to the server on the other form.

Sounds simple. Well, it isn't _that_ easy ;) But you'll get the code to start with from the links above.

thanks for getting back.

the examples you have provided are a bit too complex for me, im new to using VB. I only know the basics roughly.

Is there any simple code that could be used, like Getdata ??

The first example is in fact a good basis for a client/server chat application with sockets. Since the sockets are a quite "low-level" type of communication, you have to do much work to get a working application.

.NET (2005) does not provide any "high-level" way to "chat" i.e. send string messages.

A one work-around to socket based communication that came in to my mind is My.Computer.Network . It has an UploadFile method and VB.NET has also FileSystemWatcher control. So, save a message as a text file and upload it to directory (or URI in another computer). The second form has FileSystemWatcher control which recognizes a new file, form reads new text file and shows it's content. And I don't see any reason why you can't apply this also as in a "two-way" communication.

Certainly not an elegant solution, but it would be easy to implement.

For Having the value of first form to second form you will have got to wright code on load event of second form

Private sub form2.load()
textbox1.text=form1.textbox1.text
exn sub

Note:-1) textbox1.text is the textbox of second form in which you want to get value
2)form1 is name of firstform

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.