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

  • 相关阅读:
    NSClassFromString,NSSelectorFromString,isKingOfClass
    [ios2]10大iOS开发者最喜爱的类库
    [ios2]iphone编程中使用封装的NSLog来打印调试信息 【转】
    [ios2]蓝牙通信【转】
    [ios] Xcode使用设置相关-快捷键【转】
    iOS图片设置圆角
    iOS 时间戳转时间
    iOS 时间转时间戳
    iOS loading等待图
    iOS简便写法
  • 原文地址:https://www.cnblogs.com/wangxiaoyang/p/3625286.html
Copyright © 2011-2022 走看看