Solved

MSwinsock Based app in VB

Posted on 2004-04-23
4
236 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

15 Experts available now in Live!

Get 1:1 Help Now