Results 1 to 3 of 3

Thread: Socket Programming

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    4

    Socket Programming

    Hi,

    We are developing an utility using Socket programming, we face few issues here, we would
    like to take your inputs to resolve these errors.

    Firstly, We want to transfer a string data from the client to server, to do this we
    follow the logic given below.

    Sub SendData()
    Dim objTCPClient As New TcpClient()
    Dim objStream As Stream
    Dim bytMessage(1000) As Byte
    Dim strMessage as string = "Sample Message"

    objTCPClient = New TcpClient()
    objTCPClient.Connect(strContactIPAddr, strContactPortNumber)

    objStream = objTCPClient.GetStream()
    bytMessage = System.Text.Encoding.ASCII.GetBytes(strMessage)

    objStream.Write(bytMessage, 0, Len(strMessage))

    objTCPClient.Close()
    objTCPClient = Nothing
    End Sub

    when we execute the above piece of code, it sends the data to the server application
    which has been listening for request at a particular ip address and port. When we verified
    the string data received from the socket's input stream, we get "Sample Message as the
    received text, we would like to know why the ending " (double quotes) is missing here.

    Secondly, from the server application, we have a sub routine called Listener,
    which is invoked periodically using Timer object from System.Threading namespace. Inside
    this subroutine, we used ListenerObject.AcceptSocket() to receive the data sent from
    various systems. Here, we are trying to load windows form instances dynamically based upon
    the client which sent the data. This subroutine receives data from various client systems

    properly, but the form which we are opening from this procedure comes to the foreground, but we

    could not set focus to this form and use it in the normal way. It is constantly blocked by some

    process. The code snippet used for the above process is given below for your reference.

    In a module file, we have the public variables as mentioned below.

    Public objTcpListener As TcpListener
    Public objTimer As System.Threading.Timer

    From a form load event, we execute the code given below.

    objTcpListener = New TcpListener(strPortNumber)
    objTcpListener.Start()

    objTimer = New Timer(New TimerCallback(AddressOf Listener), Nothing, 0, 1000)


    Sub Listener()
    Dim objSocket As Socket
    Dim bytMessage(1000) As Byte

    objSocket = objTcpListener.AcceptSocket()
    objSocket.Receive(bytMessage)
    strMessage = Encoding.ASCII.GetString(bytMessage)

    Dim obj_frm_Message as new frmMessage
    obj_frm_Message.lblMessage = strMessage
    obj_frm_Message.Show()
    obj_frm_Message.Focus()

    objSocket.Close()
    Application.DoEvents()
    End Sub

    As we mentioned earlier, we could execute the logic given above successfully, it pops up
    the new window with the message sent by the tcp client application, but we could not set
    focus to the window which is poped up, it is constantly blocked by some system process and
    we could not do any operation with those windows.

    When we close the main window, all the windows opened through Listenr sub routine get
    closed.

    Can you give us your inputs to resolve the above mentioned issues, we look forward
    for your replies, thank you.

    Regards,
    Deva.

  2. #2
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    1st problem:

    The symbol " disappear because the receive byte buffer you define is 1000 bytes while your message is less than that so the remaining space will fill with hex 0x00. The solution is that after you convert the bytes array to text, try to remove those 0x00 by using TRIM.

    At Server Side:

    objSocket.Receive(bytMessage)
    strMessage = Encoding.ASCII.GetString(bytMessage) strMessage.Trim("")

    another way to do it is to re dimension your bytes array at the client side to the size exactly same with the character you need to sent so that there won't sent extra 0x00 to server side.

    At Client Side:

    redim bytMessage(strMessage.Length-1)
    bytMessage = System.Text.Encoding.ASCII.GetBytes(strMessage)

    objStream.Write(bytMessage, 0, Len(strMessage))


    2nd problem:

    Still not clear about the problem you are facing.... do you mean that the windows had poped up..,.but the application hangs..or you mean other things?

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    4

    socket programming

    Hi Jason,

    Thank you for the reply, we believe that the details provided here would help you to identify our problem and enable you to tell us why it behaves so.

    We are developing an utility using Socket programming, we face a problem here, we would like to take your inputs to resolve these errors.

    We have written a server application which listens for data from multiple clients. The same application is capable of sending messages to other clients, especially to the client
    from whom it received the message. To take care of this requirement, we placed the TCPListener in a subroutine and executed this subroutine using timer object from
    System.Threading namespace. This particular subroutine is invoked periodiacally say once in four seconds or once in five seconds throug this timer object.

    From this subroutine, we used TCPListener object's AcceptSocket method to receive data from other clients. Now, after receiving a message from a client, we want to load a form
    dynamically by displaying the message we received.

    This subroutine receives data from various client systems properly, but the windows form which we are opening from this procedure comes to the foreground, but we could not set focus to this

    form and use it in the normal way. It is constantly blocked by some system process. This form is hangling and we could not do operation using this.

    We have loaded another form before we listen for requests from clients, we are able to use this form in the normal way. It tells us that the application does not hang-up, just the form we loaded through listener procedure is blocked. We have provided the
    code snippet below for your reference.

    In a module file, we have the public variables as mentioned below.

    Public objTcpListener As TcpListener
    Public objTimer As System.Threading.Timer

    From a form load event, we execute the code given below.

    objTcpListener = New TcpListener(strPortNumber)
    objTcpListener.Start()

    objTimer = New Timer(New TimerCallback(AddressOf Listener), Nothing, 0, 1000)


    Sub Listener()
    Dim objSocket As Socket
    Dim bytMessage(1000) As Byte

    objSocket = objTcpListener.AcceptSocket()
    objSocket.Receive(bytMessage)
    strMessage = Encoding.ASCII.GetString(bytMessage)

    Dim obj_frm_Message as new frmMessage
    obj_frm_Message.lblMessage = strMessage
    obj_frm_Message.Show()
    obj_frm_Message.Focus()

    objSocket.Close()
    Application.DoEvents()
    End Sub

    As we mentioned earlier, we could execute the logic given above successfully, it pops up the new window with the message sent by the tcp client application, but we could not set focus to the window which is poped up, it is constantly blocked by some system process and we could not do any operation with this window.

    Can you give us your inputs to resolve the above mentioned issues, we look forward for your replies, thank you.

    Regards,
    Deva.

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