Results 1 to 2 of 2
  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Tupi, Southern part of the Philippines

    Might be an Encoding Problem

    This is a code that connects to gmail, passes my username and password then retrieve the response. It seems to be working fine but the problem is the response. I was instructed that the response should be like "+OK" or "-Err", instead it shows weird characters. A fellow told me that there must be a problem with the encoding... So here it is... please help me...

    This is my code:

    Dim _server As TcpClient
    Dim _netstream As NetworkStream
    Dim _stmreader As StreamReader
    Dim _senddata() As Byte
    Dim _ssl As Net.Security.SslStream
    Dim _user, _password As String

    _server = New TcpClient("pop.gmail.com", 995)
    _netstream = _server.GetStream
    _ssl = New Net.Security.SslStream(_server.GetStream())
    DirectCast(_ssl, Net.Security.SslStream).AuthenticateAsClient("pop.gmail.com")
    _stmreader = New StreamReader(_server.GetStream)
    Catch _ex As Exception
    Exit Sub
    End Try

    _user = "username@gmail.com"
    _password = "password"

    _senddata = System.Text.Encoding.ASCII.GetBytes(("USER" + _user.Trim + vbCrLf).ToCharArray())
    _netstream.Write(_senddata, 0, _senddata.Length)
    _senddata = System.Text.Encoding.ASCII.GetBytes(("PASS" + _password.Trim + vbCrLf).ToCharArray())
    _netstream.Write(_senddata, 0, _senddata.Length)
    _senddata = System.Text.Encoding.ASCII.GetBytes(("STAT" + vbCrLf).ToCharArray())
    _netstream.Write(_senddata, 0, _senddata.Length)

    I'm using Visual Studio 2008 on Windows 7 Ultimate...

  2. #2
    Fanatic Member
    Join Date
    Feb 2000

    Re: Might be an Encoding Problem

    _senddata = System.Text.Encoding.ASCII.GetBytes(("STAT" + vbCrLf).ToCharArray())
    _netstream.Write(_senddata, 0, _senddata.Length)
    the MsgBox here looks very suspicious. The trouble is straight after you write to the stream you are trying to read a response. This assumes that the response is instant and that the other app and comms have been quicker than Msgbox. This is unlikely to be the case. I would expect some kind of event be raised from the other app and your application lookiing out for that event. Only then can you ensure that you are receiving all data in the stream. Check out Async communication there are loads of examples out there. I suspect this is what you really need.

    also anything you read, you may need to use Decode as opposed to Encode

    Here is an example of mine the uses Async. I happen to be passing xml data to a service and then am waiting for a new xml document to be returned. There is no reason why you cant use this principal in your code.

    On the main thread create a background worker, by doing this it opens a new thread and then waits for this thread to return. My code is in C# but you can easily convert to VB.
     worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    Now we have this established worker_RunWorkerCompleted is the event that receives the returning data, so this is where you would place your msgBox and do anything with the incoming stream.

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    protected void worker_DoWork(object sender, DoWorkEventArgs e)
    The worker_DoWork is where you make the call to the other application.

    All of this is in a WCF Service, but it doesn't have to be
    Last edited by Bill Crawley; Mar 30th, 2012 at 03:35 AM.

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.