Visual Basic > Visual Basic Forums > Visual Basic Language > cannot connect to the network using the socket programming(in vb) to send a data through a message board(messaging system).
Ask a questionAsk a question
 

General Discussioncannot connect to the network using the socket programming(in vb) to send a data through a message board(messaging system).

  • Monday, November 02, 2009 9:10 AMDyla Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    hi, i have this codes here whereby my objective is to develop a writing network applications of programming, a means of sending data to a message board(messaging system).
    my problem is i couldnt connect it to the server's IP address and port num to send a data through a message board. however i could only display
    at Form2  txtReceive.Text = HexConvert.Data_Asc_Hex(sData), whereby this code could only convert whatever data(txtSend) that is being typed in, could then convert hex decimal into a textbox(txtReceive).
    the final result that i would want to achieve is to be able to display the at the textbox(txtReceive) is the <"message protocol"> & <" hex message that is being typed"> & <"parity bit"> into the txtReceive box.

    form form 1, my codes are....

    Imports
    System.Net.Sockets
    Imports System.IO
    Public Class Form1
    Const portNo As Integer = 1234
    Dim client As TcpClient
    Dim data() As Byte
    Dim strMsgBd As String
    Dim strMsg1of1 As String
    Dim IPAddress As String = "172.16.156.211"
    Dim WaitingTime As Integer

    Private
    Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.ClickIf txtIPAddress.Text = "172.16.156.211" And txtPortNo.Text = "1234" Then
    Form2.Show()
    strMsg1of1 = Form2.TextBox2.Text
    Call CET_Protocol()
    ConnectToServer(IPAddress, portNo)
    SendMessage(strMsgBd)
    Delay(WaitingTime)
    MsgBox(
    "Access Granted!")
    Disconnect()
    Else
    MsgBox("Access Denied!")
    End If
    End Sub

     

    Public Sub ConnectToServer(ByVal IPAddress As String, ByVal PortNo As String)
    Try
    client = New TcpClient
    client.Connect(IPAddress, PortNo)
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub

     

    Public Sub CET_Protocol()
    Dim MsgCET(0 To 1000) As VariantType
    Static intMsgNum As Integer
    Dim index, intCount, crc As Integer
    'MsgCET is storing decimals up to 255
    MsgCET(0) = &H80 ' protocol for mesage setup display
    MsgCET(1) = &H55
    MsgCET(2) = &H60
    MsgCET(3) = &H82
    intMsgNum = intMsgNum + 1
    If intMsgNum > 10 Then
    intMsgNum = 1
    End If
    MsgCET(4) = intMsgNum 'message display number - range from 01 to 10
    MsgCET(5) = &H20 '&H20 - half screen, clock above, message below
    MsgCET(6) = &HF 'display time upper nibble
    MsgCET(7) = &HF 'display time lower nibble
    MsgCET(8) = 0 'always set to 0
    MsgCET(9) = 3 '1=fast, 2=medium, 3=slow
    MsgCET(10) = 0 'flash speed upper nibble
    MsgCET(11) = &H50 'flash speed lower nibble
    MsgCET(12) = &HF1 'start of message, always set to &HF1
    MsgCET(13) = &H8 'In-Char scroll left continuously
    'MsgCET(13) = &H28 'In-Char scroll left & stop
    MsgCET(14) = &H40 'Out-Char Scroll left
    MsgCET(15) = 0 'Out-Char Scroll right
    For index = 1 To Len(strMsg1of1) ' Message is stored from MsgCET(16)onwards as ASCII
    MsgCET(15 + index) = Asc(Mid$(strMsg1of1, index, 1))
    Next index
    crc = 0
    intCount = 15 + Len(strMsg1of1)
    For index = 0 To intCount
    crc = crc + MsgCET(index)
    'MessageBox.Show("crc: " & crc)
    crc = crc And 255
    'MessageBox.Show("crc AND 255: " & crc)
    Next index
    If crc > 127 Then
    crc = crc Xor &HFF
    End If
    MsgCET(intCount + 1) = crc 'checksum
    MsgCET(intCount + 2) = 255 'terminating byte hex FF or 255
    'convert from decimals to character and store message and CET board protocols in strMsgBd as string or characters
    strMsgBd = ""
    For index = 0 To intCount + 2
    strMsgBd = strMsgBd & Chr(MsgCET(index))
    Next index
    End Sub

     

    Public Sub SendMessage(ByVal message As String)
    Try
    Dim ns As NetworkStream = client.GetStream
    ' Translate the passed message into default ANSI code and store it as a Byte array
    Dim data As Byte() = System.Text.Encoding.Default.GetBytes(message)
    ns.Write(data, 0, data.Length)
    ns.Flush()
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub

     

    Public Sub Disconnect()
    Try
    client.GetStream.Close()
    client.Close()
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub

     

    Public Sub Delay(ByVal Delay As Integer)
    Dim x = 0
    Do While x < Delay 'implement delay
    x = x + 1
    Loop
    End Sub

    End
    Class

    in form2 my codes are....

    Public

     

    Class Form2
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    Dim sData As String
    sData = CStr(txtTransmit.Text)
    txtTransmitLog.Text = sData
    sData = txtTransmit.Text
    Dim HexConvert As New hexconverter()
    txtReceive.Text = HexConvert.Data_Asc_Hex(sData)
    End Sub

    Public

     

    Class hexconverter

     

    Public Function Data_Asc_Hex(ByRef Data As String) As String
    'first take each charcter using substring.
    'then convert character into ascii.
    'then convert ascii value into Hex Format
    Dim sValue As String
    Dim sHex As String = ""
    While Data.Length > 0
    sValue = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()))
    Data = Data.Substring(1, Data.Length - 1)
    sHex = sHex + sValue
    End While
    Return sHex
    End Function
    End
    Class

    thanks in advance!!

All Replies