Reading Files

package main

import (
    "bufio"
    "fmt"
    "io"
    "io/ioutil"
    "os"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main() {
    dat, err := ioutil.ReadFile("/tmp/dat")
    check(err)
    fmt.Print(string(dat))

    f, err := os.Open("/tmp/dat")
    check(err)

    b1 := make([]byte, 5)
    n1, err := f.Read(b1)
    check(err)
    fmt.Printf("%d bytes: %s\n", n1, string(b1))

    o2, err := f.Seek(6, 0)
    check(err)
    b2 := make([]byte, 2)
    n2, err := f.Read(b2)
    check(err)
    fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))

    o3, err := f.Seek(6, 0)
    check(err)
    b3 := make([]byte, 2)
    n3, err := io.ReadAtLeast(f, b3, 2)
    check(err)
    fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))

    _, err = f.Seek(0, 0)
    check(err)

    r4 := bufio.NewReader(f)
    b4, err := r4.Peek(5)
    check(err)
    fmt.Printf("5 bytes:%s\n", string(b4))

    f.Close()
}

                                    
展开阅读全文

Error reading from socket

08-26

我写了一个MIDAS程序。rn在一个线程的execute中循环调用:rn只调用了ClientDataSet1.Open;rnClientDataSet1.execute;rn在客户端响应了AfterExecute,rn在服务器端响应了BeforeExecute,AfterExecutern这三个事件响应函数只用到了OwnerData参数.rn为什么运行一阵子之后便出现这样的错误,错误出在(26)(27)两行?如何解决?我都不知道什么时候会调用这个函数TSocketTransport.Receivernrnrnrn(1) function TSocketTransport.Receive(WaitForInput: Boolean; Context: Integer): IDataBlock;rn(2) varrn(3) RetLen, Sig, StreamLen: Integer;rn(4) P: Pointer;rn(5) FDSet: TFDSet;rn(6) TimeVal: PTimeVal;rn(7) RetVal: Integer;rn(8) beginrn(9) Result := nil;rn(10) TimeVal := nil;rn(11) FD_ZERO(FDSet);rn(12) FD_SET(FSocket.SocketHandle, FDSet);rn(13) if not WaitForInput thenrn(14) beginrn(15) New(TimeVal);rn(16) TimeVal.tv_sec := 0;rn(17) TimeVal.tv_usec := 1;rn(18) end;rn(19) RetVal := select(0, @FDSet, nil, nil, TimeVal);rn(20) if Assigned(TimeVal) thenrn(21) FreeMem(TimeVal);rn(22) if RetVal = SOCKET_ERROR thenrn(23) raise ESocketConnectionError.Create(SysErrorMessage(WSAGetLastError));rn(24) if (RetVal = 0) then Exit;rn(25) RetLen := FSocket.ReceiveBuf(Sig, SizeOf(Sig));rn(26) if RetLen <> SizeOf(Sig) thenrn(27) raise ESocketConnectionError.CreateRes(@SSocketReadError);rn(28) if (Sig and CallSig <> CallSig) andrn(29) (Sig and ResultSig <> ResultSig) thenrn(30) raise Exception.CreateRes(@SInvalidDataPacket);rn(31) RetLen := FSocket.ReceiveBuf(StreamLen, SizeOf(StreamLen));rn(32) if RetLen = 0 thenrn(33) raise ESocketConnectionError.CreateRes(@SSocketReadError);rn(34) if RetLen <> SizeOf(StreamLen) thenrn(35) raise ESocketConnectionError.CreateRes(@SSocketReadError);rn(36) Result := TDataBlock.Create as IDataBlock;rn(37) Result.Size := StreamLen;rn(38) Result.Signature := Sig;rn(39) P := Result.Memory;rn(40) Inc(Integer(P), Result.BytesReserved);rn(41) while StreamLen > 0 dorn(42) beginrn(43) RetLen := FSocket.ReceiveBuf(P^, StreamLen);rn(44) if RetLen = 0 thenrn(45) raise ESocketConnectionError.CreateRes(@SSocketReadError);rn(46) if RetLen > 0 thenrn(47) beginrn(48) Dec(StreamLen, RetLen);rn(49) Inc(Integer(P), RetLen);rn(50) end;rn(51) end;rn(52) if StreamLen <> 0 thenrn(53) raise ESocketConnectionError.CreateRes(@SInvalidDataPacket);rn(54) InterceptIncoming(Result);rn(55) end;rnrn 论坛

没有更多推荐了,返回首页