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

  • 相关阅读:
    JavaScript和Applet通信
    HashTable和HashMap的区别
    JAVA中有关byte,int,long这些基本数据类型的存储方式以及负数在JAVA中的表示方式
    浏览带有最新JAVA技术的Applet
    JAVA byte[], int, long三者之间的相互转换代码
    使用正则表达式将JSP中文本标签抽取出来填到PropertyFile中
    JAVA多线程-继承Thread类和实现Runnable接口的区别
    Java Conditional Compile Java条件编译
    Alexa 网站排名
    AJAX 教程
  • 原文地址:https://www.cnblogs.com/wangxiaoyang/p/3625286.html
Copyright © 2011-2022 走看看