zoukankan      html  css  js  c++  java
  • 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方法。

  • 相关阅读:
    python_day11 mysql
    python_day10 多线程 协程 IO模型
    python_day09 多进程 多线程 协程 paramiko模块
    python_day08 接口与归一化设计 多态与多态性 封装 面向对象高级 异常处理 网络编程
    python_day07 常用模块xml/configparser/hashlib/subprocess 面向对象程序设计
    python_day06 匿名函数与内置函数 常用模块
    python_day05 协程函数 面向过程编程 递归和二分法 模块和包的使用 常用模块 开发代码规范
    python_day04 函数嵌套 名称空间和作用域 闭包 装饰器 迭代器 生成器 列表解析 三元表达式 生成器表达式
    python_day03 文件处理复习和函数定义调用参数对象
    python_day02 基本数据类型操作,字符编码,文件处理
  • 原文地址:https://www.cnblogs.com/zqonline/p/1634792.html
Copyright © 2011-2022 走看看