PDA

Click to See Complete Forum and Search --> : visual basic sockets: how to send msg and how to receive msg


zip10021nyc
Sep 4th, 2006, 09:40 AM
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

demon.KILER
Sep 4th, 2006, 12:08 PM
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]


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


Imports System.Net.Sockets
Public Class Form1
Const portno As Integer = 1000
Dim listner As TcpClient
Dim CheckForIllegalCrossThreadCalls() As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "client"
Dim value As Boolean

value = Control.CheckForIllegalCrossThreadCalls

Control.CheckForIllegalCrossThreadCalls = False
listen()
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


Function listen()
listner = New TcpClient("localhost", portno)

If listner.Connected Then

RichTextBox1.Text = "connected " & vbCrLf
ElseIf listner.Connected = False Then
listner.Connect("localhost", portno)
listner.SendBufferSize = 2
Else
MsgBox("canot ")
End If

Dim write As New System.IO.StreamWriter(listner.GetStream)
write.AutoFlush = True
write.Write("CHAT|hi")




End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim byt(255) As Byte
listner.GetStream.BeginWrite(byt, 0, 256, AddressOf Write, "hi")
End Sub
Sub Write(ByVal ar As IAsyncResult)
If ar.IsCompleted Then
RichTextBox1.Text &= "writecomple " & vbCrLf
End If
End Sub

End Class

demon.KILER
Sep 4th, 2006, 12:10 PM
if I devolp this I will tell u else if u did pls tell me