Results 1 to 3 of 3

Thread: visual basic sockets: how to send msg and how to receive msg

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    1

    Thumbs down visual basic sockets: how to send msg and how to receive msg

    hi -

    i can code some basic VB applications. but i am not a professional VB prograamer. i am in desperately need to know, in VB, how to receive data from socket and how to send data to socket. this is a constantly running and checking socket in real time. if there are data in socket, go and get them; otherwise, send some data to socket. all data are ascii text form with linefeeds it it.

    can someone please help me and give me some simple examples????

    thank YOU!!!

    G in nyc

  2. #2
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: visual basic sockets: how to send msg and how to receive msg

    hi zip10021nyc

    I was working on that form yesterday but i didnot made it work I was doing that in vb.net [2005] ....


    all i kbnow until now is we need to two app one server and one client ...

    This what i did until now [server]


    Code:
    Option Strict Off
    Imports System.Threading
    Imports System.Net.Sockets
    Imports System.Text
    
    Public Class Form1
        Dim portno As Integer = 1000
        Dim client As New Hashtable()
        Dim hear As TcpListener
    
    
    
    
    
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "server"
            Dim value As Boolean
    
            value = Control.CheckForIllegalCrossThreadCalls
    
            Control.CheckForIllegalCrossThreadCalls = False
    
            lisen()
            AddHandler Application.ThreadException, AddressOf therading_error
        End Sub
        Sub therading_error(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
            MsgBox(e.Exception.Message)
           
        End Sub
        Sub lisen()
            hear = New TcpListener(portno)
            hear.ExclusiveAddressUse = True
            hear.Start()
    
            hear.BeginAcceptTcpClient(AddressOf connection, Nothing)
    
    
    
    
    
        End Sub
        Sub connection(ByVal ar As System.IAsyncResult)
            If ar.IsCompleted Then
                TextBox2.Text &= "Connction allowed" & vbCrLf
                hear.BeginAcceptTcpClient(AddressOf connection, Nothing)
            End If
    
        End Sub
        
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
        End Sub
    End Class

    and this is for client
    VB Code:
    1. Imports System.Net.Sockets
    2. Public Class Form1
    3.     Const portno As Integer = 1000
    4.     Dim listner As TcpClient
    5.     Dim CheckForIllegalCrossThreadCalls() As Boolean
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Me.Text = "client"
    9.         Dim value As Boolean
    10.  
    11.         value = Control.CheckForIllegalCrossThreadCalls
    12.  
    13.         Control.CheckForIllegalCrossThreadCalls = False
    14.         listen()
    15.         AddHandler Application.ThreadException, AddressOf therading_error
    16.     End Sub
    17.     Sub therading_error(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
    18.         MsgBox(e.Exception.Message)
    19.  
    20.     End Sub
    21.  
    22.  
    23.     Function listen()
    24.         listner = New TcpClient("localhost", portno)
    25.  
    26.         If listner.Connected Then
    27.  
    28.             RichTextBox1.Text = "connected " & vbCrLf
    29.         ElseIf listner.Connected = False Then
    30.             listner.Connect("localhost", portno)
    31.             listner.SendBufferSize = 2
    32.         Else
    33.             MsgBox("canot  ")
    34.         End If
    35.  
    36.         Dim write As New System.IO.StreamWriter(listner.GetStream)
    37.         write.AutoFlush = True
    38.         write.Write("CHAT|hi")
    39.  
    40.  
    41.  
    42.        
    43.     End Function
    44.  
    45.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    46.         Dim byt(255) As Byte
    47.         listner.GetStream.BeginWrite(byt, 0, 256, AddressOf Write, "hi")
    48.     End Sub
    49.     Sub Write(ByVal ar As IAsyncResult)
    50.         If ar.IsCompleted Then
    51.             RichTextBox1.Text &= "writecomple " & vbCrLf
    52.         End If
    53.     End Sub
    54.  
    55. End Class
    Last edited by demon.KILER; Sep 4th, 2006 at 12:12 PM. Reason: sorry for the word used in the code
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  3. #3
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: visual basic sockets: how to send msg and how to receive msg

    if I devolp this I will tell u else if u did pls tell me
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width