Socket编程笔记-同步

命名空间导入

Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
服务端编程过程:
  1. 监听Listen
  2. 创建连接Accept
  3. 获取数据Receive
  4. 发送数据Send


服务端开始监听Listen:

    Sub Main()
        Console.WriteLine("正在接收客户端连接……")

        listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        listener.Bind(New System.Net.IPEndPoint(IPAddress.Any, 8080))
        listener.Listen(100)


        Dim t = New Thread(AddressOf AcceptWorkThread) '开启线程
        t.Start()

        Console.ReadKey()

    End Sub
  
监听客户端传入
 Sub AcceptWorkThread()
        Threading.Thread.CurrentThread.IsBackground = True

        While True
            Dim accept = listener.Accept

            '获取远程IP
            Dim remoEP As IPEndPoint = CType(accept.RemoteEndPoint, IPEndPoint)
            Dim recString = String.Format("接收到来自 {0} 的连接", remoEP.Address.ToString)
            Console.WriteLine(recString)


            Dim receiveThread As System.Threading.Thread 
= New Thread(New ParameterizedThreadStart(AddressOf ReceiveWorkThread)) receiveThread.Start(accept) End While End Sub

 

接收数据,接收成功后,并发送消息
    Sub ReceiveWorkThread(ByVal obj As Object)
        Thread.CurrentThread.IsBackground = True

        Dim socket As System.Net.Sockets.Socket = CType(obj, Socket)

        Dim buffer() As Byte = Nothing
        Dim isReceive As Boolean = False

        While True
            Dim err As SocketError
            Dim b(256) As Byte
            Dim readbyes = socket.Receive(b, 0, b.Length, SocketFlags.None, err)

            '判断操作是否成功。
            If err = SocketError.Success Then

                If readbyes = 0 Then  '没有接收到数据,退出!
                    socket.Shutdown(SocketShutdown.Both)
                    socket.Close()
                    Console.WriteLine("Socket已退出!")
                    Exit While
                End If

                If buffer Is Nothing OrElse buffer.Length = 0 Then
                    buffer = b
                Else
                    Dim data(buffer.Length + b.Length - 1) As Byte
                    buffer.CopyTo(data, 0)
                    b.CopyTo(data, buffer.Length)

                    buffer = data
                End If

                Console.WriteLine(buffer.Length)

                If socket.Available = 0 AndAlso Not buffer Is Nothing AndAlso buffer.Length > 0 Then
                    Dim f As New IO.FileStream("c:\1.rar", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
                    f.Write(buffer, 0, buffer.Length)
                    f.Close()

                    Console.WriteLine("文件接收成功!")

                    Dim msg = System.Text.Encoding.Default.GetBytes("文件接收成功。")
                    socket.Send(msg)

                End If

                If socket.Available = 0 Then
                    buffer = Nothing
                End If
            Else
                Console.WriteLine(err.ToString)
                Exit While
            End If



        End While

    End Sub
 
客户端编程
  1. 连接服务端Connect
  2. 发送数据
  3. 接收数据

连接服务端

 Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        s.Connect(IPAddress.Parse("127.0.0.1"), 8080)
        Console.WriteLine("按任意键开始传送文件……")
        Console.ReadKey()
发送数据
   '文件读取
        Dim f As New IO.FileStream("d:\2.rar", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
        Dim b(CInt(f.Length) - 1) As Byte
        f.Read(b, 0, b.Length)
        f.Close()


        '获取文件长度
        Console.WriteLine("文件长度:" & b.Length)

        '发送文件
        s.Send(b, b.Length, SocketFlags.None)

 

接收服务端发送来的数据

     Dim t As New System.Threading.Thread(AddressOf ReceiveWorkThread)
        t.Start(s)

其实这里接收数据与服务端的一样,也是调用ReceiveWorkThread方法。

posted on 2009-12-29 11:21  zqonline  阅读(452)  评论(0编辑  收藏  举报

导航