galaxy_thestars
May 16th, 2008, 03:17 PM
I have to develop a client/server application. I am able to send message to server but can't send message to client from server. How should i do this. I am willing to send message from client to server and from server to client. My coding are as below:
Client
--------------------------
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Const portNo As Integer = 500
Dim client As TcpClient
Dim data() As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpclient As New TcpClient
'---connect to the server---
tcpclient.Connect("127.0.0.1", portNo)
'---use a NetworkStream object to send and receive data---
Dim ns As NetworkStream = tcpclient.GetStream
Dim data As Byte() = Encoding.ASCII.GetBytes("Hello")
'---send the text---
ns.Write(data, 0, data.Length)
End Sub
Server
---------------------------------------------------------------------
Imports System.Text
Public Class Form1
Const portNo As Integer = 500
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim localAdd As System.Net.IPAddress = _
System.Net.IPAddress.Parse("127.0.0.1")
'---listen at the local address---
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
'---Accepts a pending connection request---
Dim tcpClient As TcpClient = listener.AcceptTcpClient()
'---use a NetworkStream object to send and receive data---
Dim ns As NetworkStream = tcpClient.GetStream
Dim data(tcpClient.ReceiveBufferSize) As Byte
'---read incoming stream; Read() is a blocking call---
Dim numBytesRead As Integer = ns.Read(data, 0, _
CInt(tcpClient.ReceiveBufferSize))
'---display data received---
TextBox1.Text = "Received :" & _
Encoding.ASCII.GetString(data, 0, numBytesRead)
End Sub
Plz reply soon about the server solution.
thanks in advance.
Client
--------------------------
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Const portNo As Integer = 500
Dim client As TcpClient
Dim data() As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpclient As New TcpClient
'---connect to the server---
tcpclient.Connect("127.0.0.1", portNo)
'---use a NetworkStream object to send and receive data---
Dim ns As NetworkStream = tcpclient.GetStream
Dim data As Byte() = Encoding.ASCII.GetBytes("Hello")
'---send the text---
ns.Write(data, 0, data.Length)
End Sub
Server
---------------------------------------------------------------------
Imports System.Text
Public Class Form1
Const portNo As Integer = 500
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim localAdd As System.Net.IPAddress = _
System.Net.IPAddress.Parse("127.0.0.1")
'---listen at the local address---
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
'---Accepts a pending connection request---
Dim tcpClient As TcpClient = listener.AcceptTcpClient()
'---use a NetworkStream object to send and receive data---
Dim ns As NetworkStream = tcpClient.GetStream
Dim data(tcpClient.ReceiveBufferSize) As Byte
'---read incoming stream; Read() is a blocking call---
Dim numBytesRead As Integer = ns.Read(data, 0, _
CInt(tcpClient.ReceiveBufferSize))
'---display data received---
TextBox1.Text = "Received :" & _
Encoding.ASCII.GetString(data, 0, numBytesRead)
End Sub
Plz reply soon about the server solution.
thanks in advance.