100求使用socket的例子

主  题:  100求使用socket的例子
作  者:  zxcv11 ()
等  级:  ^
信 誉 值:  100
所属社区:  .NET技术 C#
问题点数:  100
回复次数:  9
发表时间:  2004-10-25 10:14:30

在线等待
flying136@sohu.com



回复人: jackie615(亞馬遜)★(東大傳說) ( 五级(中级)) 信誉:99 2004-10-25 10:20:59 得分: 0

友情up

回复人: fellowcheng(今天心情不错) ( 二级(初级)) 信誉:100 2004-10-25 10:23:04 得分: 0

up

回复人: xiaoslong(龙哥) ( 四级(中级)) 信誉:100 2004-10-25 10:25:02 得分: 70

已发

回复人: Tomgus(小桥流水) ( 五级(中级)) 信誉:87 2004-10-25 10:26:08 得分: 10

发给你了

回复人: cs920(头痛不是两三天)(此情可待) ( 三级(初级)) 信誉:100 2004-10-25 10:28:02 得分: 10

已发,请查收

回复人: xiaohutushen(xiaohutushen) ( 五级(中级)) 信誉:100 2004-10-25 10:33:13 得分: 5

发送端

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 sendsocket As New Net.Sockets.Socket
      (Net.Sockets.AddressFamily.InterNetwork,
      Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
      '实例化socket
        Dim ipendpiont As New Net.IPEndPoint
      (Net.IPAddress.Parse("192.168.0.1"), 8888)'建立终结点
        'OpenFileDialog1.ShowDialog()
        Dim fs As New IO.FileStream("c:\p.doc",
      IO.FileMode.OpenOrCreate, IO.FileAccess.Read)'要传输的文件
        Dim fssize(fs.Length - 1) As Byte
        Dim strread As New IO.BinaryReader(fs)'流处理要传输的文件
        'fs.Read(fssize, 0, fssize.Length - 1)
        strread.Read(fssize, 0, fssize.Length - 1)
        sendsocket.Connect(ipendpiont)'连接远程计算机
        sendsocket.Send(fssize)'发送文件
        Label1.Text = fs.Length()
        fs.Close()
        sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
      '关闭发送连接
        sendsocket.Close()'关闭本机socket
    End Sub
End Class

接收端

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim receivesocket As New Net.Sockets.Socket
      (Net.Sockets.AddressFamily.InterNetwork,
      Net.Sockets.SocketType.Stream,
      Net.Sockets.ProtocolType.Tcp)
    Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim hostipendpiont As New Net.IPEndPoint
      (Net.IPAddress.Parse("192.168.0.1"), 8888)
        receivesocket.Bind(hostipendpiont)
      '建立远程计算机的的socket
        receivesocket.Listen(2)'监听socket
    End Sub

    Private Sub Button1_Click(ByVal sender As Object,
      ByVal e As System.EventArgs) Handles Button1.Click
        Dim recfs As New IO.FileStream("p.doc",
      IO.FileMode.OpenOrCreate)
      '接收数据并将其保存到一个新的文件中
        Dim recbyte(229888) As Byte
        Dim hostsocket As Net.Sockets.Socket =
      receivesocket.Accept()
      '同意和发送端计算机建立连接
        Dim newfilestr As New IO.BinaryWriter(recfs)'流写
        hostsocket.Receive(recbyte)
        'recfs.Write(recbyte, 0, recbyte.Length - 1)
        newfilestr.Write(recbyte, 0, recbyte.Length - 1)
        recfs.Close()
        hostsocket.Shutdown(Net.Sockets.SocketShutdown.Receive)
        hostsocket.Close()
    End Sub
End Class

回复人: happyjun2000(蓝色游侠∮不要介意我无聊乱接分.NET) ( 一星(中级)) 信誉:100 2004-10-25 10:34:28 得分: 5

socket的组播应用

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace send
{
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class send
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: 在此处添加代码以启动应用程序
            //
            UdpClient sock = new UdpClient();
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
            while (true)
            {
                string newtext = Console.ReadLine();
                byte[] data = Encoding.ASCII.GetBytes(newtext);
                sock.Send(data, data.Length, iep);
            }
            sock.Close();
        }
    }
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace recv
{
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class recv
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: 在此处添加代码以启动应用程序
            //
            UdpClient sock = new UdpClient(9050);
            Console.WriteLine("ready...");
            sock.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
            while (true)
            {
                byte[] data = sock.Receive(ref iep);
                string stringdata = Encoding.ASCII.GetString(data, 0, data.Length);
                if (stringdata == "bye")
                    break;
                Console.WriteLine("received:{0}", stringdata);
            }
            sock.Close();
        }
    }
}

回复人: hanbinghai(海宁) ( 五级(中级)) 信誉:100 2004-10-25 10:42:09 得分: 0

up

回复人: jkflyfox(飞狐) ( 五级(中级)) 信誉:100 2004-10-25 17:16:00 得分: 0

强烈BS倒分垃圾
参见
http://community.csdn.net/Expert/topic/3488/3488766.xml?temp=.7287409

该问题已经结贴 ,得分记录: xiaoslong (70)、 Tomgus (10)、 cs920 (10)、 xiaohutushen (5)、 happyjun2000 (5)、

Contributors: FHL