zoukankan      html  css  js  c++  java
  • .Net中的socket编程例子

    vb2010:

    '发送端代码
    Public Class Form1
        Inherits System.Windows.Forms.Form
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
            Dim bytes(1024) As Byte '声明字节数组
            Dim sender1 As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)'初始化socket
            Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)'对发送的数据进行编码
            '***************************
            '指定ip和端口
            Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("a") 'a为要发送到的主机名,也可以指定为本机,那么就是在本机发送和接收

            Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(2) '注意此处在调试时必须取出IPv4协议下的IP地址,根据实际情况变换index值,直到取出取出IPv4协议下的IP地址,不然会报协议和IP不符合的错误
            Dim ipe As New System.Net.IPEndPoint(ipAddress, 11000)
            '**********************
            sender1.Connect(ipe) '建立连接

            Dim bytesSent As Integer = sender1.Send(msg) '发送数据
            '(((((((((
            '关闭socket
            sender1.Shutdown(Net.Sockets.SocketShutdown.Both)
            sender1.Close()
            ')))))))
        End Sub
    End Class

    '接收端代码
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Dim listener As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)
        '初始socket
    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
            '指定ip和端口
            Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
            Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(2)
            Dim localEndPoint As New System.Net.IPEndPoint(ipAddress, 11000)
            listener.Bind(localEndPoint)
            listener.Listen(10)
        End Sub
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
            Dim bytes() As Byte = New [Byte](1024) {}
            Dim handler As System.Net.Sockets.Socket = listener.Accept() '建立连接请求
            Dim data As String = Nothing
            bytes = New Byte(1024) {}
            Dim bytesRec As Integer = handler.Receive(bytes) '接收数据
            data += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec)
            TextBox1.Text = data
            Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
            handler.Shutdown(Net.Sockets.SocketShutdown.Both)
            handler.Close()
        End Sub
    End Class

  • 相关阅读:
    CMDB服务器管理系统【s5day92】:服务器管理回顾
    主机管理+堡垒机系统开发:需求讨论(一)
    saltstack主机管理项目:今日总结(六)
    saltstack主机管理项目:动态调用插件解析-模块解析(五)
    saltstack主机管理项目:编写插件基类-获取主机列表-提取yaml配置文件(四)
    saltstack主机管理项目:计主机管理项目命令分发器(三)
    saltstack主机管理项目:主机管理项目需求分析(一)
    saltstack主机管理项目:主机管理项目架构设计(二)
    CMDB服务器管理系统【s5day89】:采集资产之整合资产
    CMDB服务器管理系统【s5day89】:采集资产之汇报信息
  • 原文地址:https://www.cnblogs.com/wangxiaoyang/p/3625286.html
Copyright © 2011-2022 走看看