Solved

MSwinsock Based app in VB

Posted on 2004-04-23
4
238 Views
Last Modified: 2013-12-26
Hi


I have developed an application in VB using Mswinsock .I have a client sending some data on fixed port number to server .
I need to send some data back to the client from the server using the  same connection .How do do this ?


Help its  Urgent

0
Comment
Question by:leork2004
4 Comments
 
LVL 6

Expert Comment

by:mmusante
ID: 10898926
the 'RemoteHostIp' property give you the IP of cliend who sent data so read it in the 'DataArrival' Event and send back your reply data to this IP
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10899361
Sounds like you already have the connection established...

Just use the SendData() function.

    Winsock1.SendData "someData"

or am I not understanding the question very well?

Perhaps you could explain the problem in a  little more detail.

Idle_Mind
0
 
LVL 18

Expert Comment

by:Sethi
ID: 10905265
There are many tutorials on this site that will help you further.
0
 
LVL 3

Accepted Solution

by:
tofra earned 500 total points
ID: 10932325
Here is some help for using the winsock control from the MSDN help. This helped me to build my own newsserver and news robot.

To create a TCP server

Create a new Standard EXE project.
Change the name of the default form to frmServer.
Change the caption of the form to "TCP Server."
Draw a Winsock control on the form and change its name to tcpServer.
Add two TextBox controls to the form. Name the first txtSendData, and the second txtOutput.

Add the code below to the form.
Private Sub Form_Load()
' Set the LocalPort property to an integer.
' Then invoke the Listen method.
tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.Show ' Show the client form.
End Sub

Private Sub tcpServer_ConnectionRequest _
(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.
If tcpServer.State <> sckClosed Then _
tcpServer.Close
' Accept the request with the requestID
' parameter.
tcpServer.Accept requestID
End Sub

Private Sub txtSendData_Change()
' The TextBox control named txtSendData
' contains the data to be sent. Whenever the user
' types into the  textbox, the  string is sent
' using the SendData method.
tcpServer.SendData txtSendData.Text
End Sub

Private Sub tcpServer_DataArrival _
(ByVal bytesTotal As Long)
' Declare a variable for the incoming data.
' Invoke the GetData method and set the Text
' property of a TextBox named txtOutput to
' the data.
Dim strData As String
tcpServer.GetData strData
txtOutput.Text = strData
End Sub

The procedures above create a simple server application. However, to complete the scenario, you must also create a client application.


To create a TCP client

Add a new form to the project, and name it frmClient.
Change the caption of the form to TCP Client.
Add a Winsock control to the form and name it tcpClient.
Add two TextBox controls to frmClient. Name the first txtSend, and the second txtOutput.
Draw a CommandButton control on the form and name it cmdConnect.
Change the caption of the CommandButton control to Connect.

Add the code below to the form.
Important   Be sure to change the value of the RemoteHost property to the friendly name of your computer.

Private Sub Form_Load()
' The name of the Winsock control is tcpClient.
' Note: to specify a remote host, you can use
' either the IP address (ex: "121.111.1.1") or
' the computer's "friendly" name, as shown here.
tcpClient.RemoteHost = "RemoteComputerName"
tcpClient.RemotePort = 1001
End Sub

Private Sub cmdConnect_Click()
' Invoke the Connect method to initiate a
' connection.
tcpClient.Connect
End Sub

Private Sub txtSendData_Change()
tcpClient.SendData txtSend.Text
End Sub

Private Sub tcpClient_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
txtOutput.Text = strData
End Sub

The code above creates a simple client-server application. To try the two together, run the project, and click Connect. Then type text into the txtSendData TextBox on either form, and the same text will appear in the txtOutput TextBox on the other form.

If u need it for UDP, let me know....
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now