help on socket programming in vb.net
-
pls help me. i wonder why my server cannot send a message to my client and my client cannot read from server. anyone pls help me to debug or give me tip to do it by showing wats the command to write.My deadline for this project is comong soon. Pls help me. thanks a lot. here is my source code server: Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.IO Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form Private Client As TcpClient Private Listener As Socket Private RemotePort As Integer = 1337 Private RemoteHost As String = "localhost" Dim add As IPAddress = IPAddress.Parse("220.255.41.189") Public Shared Connected As New ManualResetEvent(False) #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub blisten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blisten.Click Listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Listener.Blocking = False Listener.Bind(New IPEndPoint(add, RemotePort)) Listener.Listen(1) ListBox1.Items.Add("Waiting for connection...") Listener.BeginAccept(New AsyncCallback(AddressOf AcceptConnection), Listener) End Sub Public Sub AcceptConnection(ByVal ar As IAsyncResult) ListBox1.Items.Add("Connection established.") Connected.Set() Dim serverSock As Socket = CType(ar.AsyncState, Socket) Dim clientSock As Socket = serverSock.EndAccept(ar) Dim buffer(1024) As Byte While True If (clientSock.Poll(100, SelectMode.SelectRead)) Then Dim count As Integer = clientSock.Receive(buffer) ListBox1.Items.Add(System.Text.Encoding.ASCII.GetString(buffer, 0, count)) End If End While End Sub Private Sub bsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsend.Click Dim Stream As NetworkStream = Client.GetStream() Dim dataSend As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text) 'Send Message Stream.Write(dataSend, 0, dataSend.Length) Stream.Flush() End Sub End Class my client source code: Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.IO Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form Private Client As TcpClient Private Listener As Socket Private RemotePort As Integer = 1337 Private RemoteHost As String = "localhost" Dim add As IPAddress = IPAddress.Parse("220.255.41.189") Public Shared Connected As Ne
-
pls help me. i wonder why my server cannot send a message to my client and my client cannot read from server. anyone pls help me to debug or give me tip to do it by showing wats the command to write.My deadline for this project is comong soon. Pls help me. thanks a lot. here is my source code server: Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.IO Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form Private Client As TcpClient Private Listener As Socket Private RemotePort As Integer = 1337 Private RemoteHost As String = "localhost" Dim add As IPAddress = IPAddress.Parse("220.255.41.189") Public Shared Connected As New ManualResetEvent(False) #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub blisten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blisten.Click Listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Listener.Blocking = False Listener.Bind(New IPEndPoint(add, RemotePort)) Listener.Listen(1) ListBox1.Items.Add("Waiting for connection...") Listener.BeginAccept(New AsyncCallback(AddressOf AcceptConnection), Listener) End Sub Public Sub AcceptConnection(ByVal ar As IAsyncResult) ListBox1.Items.Add("Connection established.") Connected.Set() Dim serverSock As Socket = CType(ar.AsyncState, Socket) Dim clientSock As Socket = serverSock.EndAccept(ar) Dim buffer(1024) As Byte While True If (clientSock.Poll(100, SelectMode.SelectRead)) Then Dim count As Integer = clientSock.Receive(buffer) ListBox1.Items.Add(System.Text.Encoding.ASCII.GetString(buffer, 0, count)) End If End While End Sub Private Sub bsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsend.Click Dim Stream As NetworkStream = Client.GetStream() Dim dataSend As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text) 'Send Message Stream.Write(dataSend, 0, dataSend.Length) Stream.Flush() End Sub End Class my client source code: Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.IO Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form Private Client As TcpClient Private Listener As Socket Private RemotePort As Integer = 1337 Private RemoteHost As String = "localhost" Dim add As IPAddress = IPAddress.Parse("220.255.41.189") Public Shared Connected As Ne
For sending messages and files over network you can see many articles and you can get refrences that is fully managed .so you can use in your project.if you are in neck. eample :unolibs.Net.dll its vey useful.search here in articls or googling it Ishak