?
Solved

MSwinsock Based app in VB

Posted on 2004-04-23
4
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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