Reading File in .NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Reading File in .NET

Hybrid View

  1. #1
    Sathya Guest

    Reading File in .NET


    I have a strange problem in my present project. I am creating a server in
    VB.NET that accepts request from multiple clients. Each client passes a text
    file to the server to read. There is no problem with the client code.


    When I am reading the data from the text file in the server

    Public Sub Main1(ByVal iJobNo As Integer)
    Dim soc As Socket = tcpListener.AcceptSocket()
    Try
    Dim s As Stream = New NetworkStream(soc)
    Dim sr As New StreamReader(s)
    Dim sw As New StreamWriter(s)
    sw.AutoFlush = True
    Do
    ---PROBLEM LINE--- Dim name As String = sr.ReadLine()
    If InStr(name, "*", CompareMethod.Text) = 3 Then
    sUsrDetails(iJobNo) = name
    Exit Do
    End If
    If name = "" Or name Is Nothing Then Exit Do
    Loop
    s.Close()
    soc.Close()
    End Sub

    The streamReader(sr) sr.ReadLine() statement abruptly ends after it reads
    the last line and does not go the verify the InStr method.

    The text file is

    //A9FRT1 JOB (A9FRT,AAIS),'TECHReports',CLASS=A,REGION=4M,MSGCLASS=Z,
    // MSGLEVEL=(0,0),PASSWORD=HANDY
    /*JOBPARM T=999,L=999
    //LASER OUTPUT DEST=RMT10,FORMDEF=10FD,PAGEDEF=05PD
    //PROC JCLLIB ORDER=AIS95.SISPROD.PROCLIB
    //FOCUS1 EXEC FCSP,USEDBY=A2SYR,SYSTEM=PROD,NODE=95
    //SYSPRINT DD SYSOUT=W
    //OFFLINE DD SYSOUT=W,OUTPUT=*.LASER,
    // DATACLAS=TTUFBA,DCB=(BLKSIZE=133)
    //SYSIN DD *
    EX FFR054 TERM_CYT=19991,
    DEPT=HIST
    FIN
    /*
    //
    //*a9frt,handy,sathya76@yahoo.com,c:\temp\tempJCL.trd


    The problem is it does work some time with the same code without any changes
    being made.. but most other times it does not work.. this is strange though..
    Pls let me know, if u can help me out here.. thanks in advance

    Sathya

  2. #2
    David Williams Guest

    Re: Reading File in .NET

    On the ones that just stop, does the text file have a concluding CrLF? I
    have seen cases where StringReader seems to require it when reading from
    memorystreams.


    HTH
    "Sathya" <sathya76@yahoo.com> wrote in message news:3d1352e9$1@10.1.10.29...
    >
    > I have a strange problem in my present project. I am creating a server in
    > VB.NET that accepts request from multiple clients. Each client passes a

    text
    > file to the server to read. There is no problem with the client code.
    >
    >
    > When I am reading the data from the text file in the server
    >
    > Public Sub Main1(ByVal iJobNo As Integer)
    > Dim soc As Socket = tcpListener.AcceptSocket()
    > Try
    > Dim s As Stream = New NetworkStream(soc)
    > Dim sr As New StreamReader(s)
    > Dim sw As New StreamWriter(s)
    > sw.AutoFlush = True
    > Do
    > ---PROBLEM LINE--- Dim name As String = sr.ReadLine()
    > If InStr(name, "*", CompareMethod.Text) = 3 Then
    > sUsrDetails(iJobNo) = name
    > Exit Do
    > End If
    > If name = "" Or name Is Nothing Then Exit Do
    > Loop
    > s.Close()
    > soc.Close()
    > End Sub
    >
    > The streamReader(sr) sr.ReadLine() statement abruptly ends after it reads
    > the last line and does not go the verify the InStr method.
    >
    > The text file is
    >
    > //A9FRT1 JOB (A9FRT,AAIS),'TECHReports',CLASS=A,REGION=4M,MSGCLASS=Z,
    > // MSGLEVEL=(0,0),PASSWORD=HANDY
    > /*JOBPARM T=999,L=999
    > //LASER OUTPUT DEST=RMT10,FORMDEF=10FD,PAGEDEF=05PD
    > //PROC JCLLIB ORDER=AIS95.SISPROD.PROCLIB
    > //FOCUS1 EXEC FCSP,USEDBY=A2SYR,SYSTEM=PROD,NODE=95
    > //SYSPRINT DD SYSOUT=W
    > //OFFLINE DD SYSOUT=W,OUTPUT=*.LASER,
    > // DATACLAS=TTUFBA,DCB=(BLKSIZE=133)
    > //SYSIN DD *
    > EX FFR054 TERM_CYT=19991,
    > DEPT=HIST
    > FIN
    > /*
    > //
    > //*a9frt,handy,sathya76@yahoo.com,c:\temp\tempJCL.trd
    >
    >
    > The problem is it does work some time with the same code without any

    changes
    > being made.. but most other times it does not work.. this is strange

    though..
    > Pls let me know, if u can help me out here.. thanks in advance
    >
    > Sathya




  3. #3
    PWilmarth Guest

    Re: Reading File in .NET


    I am not sure I can completely solve your problem, but it seems to me that
    you need to rework your Dim statements (you are mixing and matching different
    objects). I am eclosing some source code that demonstrates creating a stream
    for reading and writing. See if this helps you locate your error.
    Public Sub RunServer()
    Dim listener As TcpListener
    Dim counter As Integer = 1

    ' wait for request, then establish connection
    Try

    ' Step 1: create TcpListener
    listener = New TcpListener(5000)

    ' Step 2: TcpListener waits for connection request
    listener.Start()

    ' Step 3: establish connection upon client request
    While True
    txtDisplay.Text = "Waiting for connection" & vbCrLf

    ' accept an incoming connection
    connection = listener.AcceptSocket()

    ' create NetworkStream object associated with socket
    socketStream = New NetworkStream(connection)

    ' create objects for reading and writing across stream
    writer = New BinaryWriter(socketStream)
    reader = New BinaryReader(socketStream)

    txtDisplay.Text &= "Connection " & counter & _
    " received." & vbCrLf

    ' inform client that connection was successfull
    writer.Write("SERVER>>> Connection successful")

    txtInput.ReadOnly = False
    Dim theReply As String = ""

    ' Step 4: read String data sent from server
    Try

    ' loop until client signals termination
    Do
    theReply = reader.ReadString() ' read data

    ' display message
    txtDisplay.Text &= vbCrLf & theReply

    Loop While (theReply <> "CLIENT>>> TERMINATE" _
    AndAlso connection.Connected)

    ' handle exception if error reading data
    Catch inputOutputException As IOException
    MessageBox.Show("Client application closing")

    ' close connections
    Finally

    txtDisplay.Text &= vbCrLf & _
    "User terminated connection"

    txtInput.ReadOnly = True

    ' Step 5: close connection
    writer.Close()
    reader.Close()
    socketStream.Close()
    connection.Close()

    counter += 1
    End Try

    End While

    ' handle exception if error occurs in establishing connection
    Catch inputOutputException As IOException
    MessageBox.Show("Server application closing")

    End Try

    End Sub ' RunServer

    End Class ' FrmServer


  4. #4
    PWilmarth Guest

    Re: Reading File in .NET


    Once again, I don't know if I can solve your problem for you, but
    I keep looking at your DO loop. Is it a DO WHILE or DO UNTIL loop?
    Unless something is missing from this piece of code, I can't see
    where you have stated any conditions to DO. So the program never enters
    the DO LOOP; it just opens a Stream for Reading, opens a Stream for Writing
    then closes each stream; which sounds like what you are experiencing.

    PW

    Public Sub Main1(ByVal iJobNo As Integer)
    Dim soc As Socket = tcpListener.AcceptSocket()
    Try
    Dim s As Stream = New NetworkStream(soc)
    Dim sr As New StreamReader(s)
    Dim sw As New StreamWriter(s)
    sw.AutoFlush = True
    Do
    ---PROBLEM LINE--- Dim name As String = sr.ReadLine()
    If InStr(name, "*", CompareMethod.Text) = 3 Then
    sUsrDetails(iJobNo) = name
    Exit Do
    End If
    If name = "" Or name Is Nothing Then Exit Do
    Loop
    s.Close()
    soc.Close()
    End Sub

    The streamReader(sr) sr.ReadLine() statement abruptly ends after it reads
    the last line and does not go the verify the InStr method.

    The text file is

    //A9FRT1 JOB (A9FRT,AAIS),'TECHReports',CLASS=A,REGION=4M,MSGCLASS=Z,
    // MSGLEVEL=(0,0),PASSWORD=HANDY
    /*JOBPARM T=999,L=999
    //LASER OUTPUT DEST=RMT10,FORMDEF=10FD,PAGEDEF=05PD
    //PROC JCLLIB ORDER=AIS95.SISPROD.PROCLIB
    //FOCUS1 EXEC FCSP,USEDBY=A2SYR,SYSTEM=PROD,NODE=95
    //SYSPRINT DD SYSOUT=W
    //OFFLINE DD SYSOUT=W,OUTPUT=*.LASER,
    // DATACLAS=TTUFBA,DCB=(BLKSIZE=133)
    //SYSIN DD *
    EX FFR054 TERM_CYT=19991,
    DEPT=HIST
    FIN
    /*
    //
    //*a9frt,handy,sathya76@yahoo.com,c:\temp\tempJCL.trd


    The problem is it does work some time with the same code without any
    changes
    being made.. but most other times it does not work.. this is strange
    though..
    Pls let me know, if u can help me out here.. thanks in advance

    Sathya


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center