• 大小: 1.85MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-09-01
  • 语言: C#
  • 标签: c#  socket  同时  

资源简介

vs2005编写,c#使用一个socket同时发送字符串和文件

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//fhf
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Receiver
{
    public partial class FormReceiver : Form
    {
        Socket socket;
        Int32 port = 13000;
        string ip = “127.0.0.1“;
        Thread mythread;
        delegate void setTextbox(string strConn);
        setTextbox _setTextbox = null;

        public FormReceiver()
        {
            InitializeComponent();
        }


        private void FormReceiver_Load(object sender EventArgs e)
        {
            try
            {
                _setTextbox = new setTextbox(this.DispText);
                mythread = new Thread(new ThreadStart(BeginListen));
                mythread.Start();
            }
            catch
            {
                socket.Close();
                mythread.Abort();
            }
        }

        private void FormReceiver_FormClosing(object sender FormClosingEventArgs e)
        {
            System.Environment.Exit(0); 
        }

        private void DispText(string str)
        {
            textBox1.Text = str;
        }

        private void BeginListen1()
        {
            IPAddress ipAddress = IPAddress.Parse(ip);
            socket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(ipAddress port);
            byte[] byteMessage = new byte[1048576];
            socket.Bind(iep);
            while (true)
            {
                Socket newSocket = null;
                try
                {
                    socket.Listen(10);
                    newSocket = socket.Accept();
                    int bytes = 0;
                    int datanum = 0; 
                    string s = ““;
                    while (true)
                    {
                        bytes = newSocket.Receive(byteMessage byteMessage.Length 0);
                        datanum = BitConverter.ToInt32(byteMessage 0);   //从buffer中的前4个字节读出count 
                        if (bytes <= 0)
                            break;
                        s += Encoding.Unicode.GetString(byteMessage4bytes-4);
                    }                   
                    string sTime = DateTime.Now.ToShortTimeString();
                    string msg = sTime + “ “ + “Message from “ + newSocket.RemoteEndPoint.ToString() + “ :“;
                    //字符串
                    msg += s;
                    //在文本框内显示
                    this.Invoke(_setTextbox msg);
                }
                catch(Exception e)
                {
                    newSocket.Close();
                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void Begin

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-08-09 14:26  SocketTest\
     目录           0  2012-08-12 00:39  SocketTest\Receiver\
     目录           0  2012-08-09 22:34  SocketTest\Receiver\bin\
     目录           0  2012-08-12 00:35  SocketTest\Receiver\bin\Debug\
     文件      951755  2012-08-12 00:41  SocketTest\Receiver\bin\Debug\1.jpg
     文件       24576  2012-08-12 00:39  SocketTest\Receiver\bin\Debug\Receiver.exe
     文件       28160  2012-08-12 00:39  SocketTest\Receiver\bin\Debug\Receiver.pdb
     文件        5632  2005-11-11 22:25  SocketTest\Receiver\bin\Debug\Receiver.vshost.exe
     文件        7396  2012-08-12 00:39  SocketTest\Receiver\FormReceiver.cs
     文件        2941  2012-08-12 00:38  SocketTest\Receiver\FormReceiver.Designer.cs
     文件        5814  2012-08-12 00:38  SocketTest\Receiver\FormReceiver.resx
     目录           0  2012-08-09 22:36  SocketTest\Receiver\obj\
     目录           0  2012-08-12 00:39  SocketTest\Receiver\obj\Debug\
     文件         849  2012-08-12 00:38  SocketTest\Receiver\obj\Debug\Receiver.csproj.GenerateResource.Cache
     文件       24576  2012-08-12 00:39  SocketTest\Receiver\obj\Debug\Receiver.exe
     文件         180  2012-08-12 00:38  SocketTest\Receiver\obj\Debug\Receiver.FormReceiver.resources
     文件       28160  2012-08-12 00:39  SocketTest\Receiver\obj\Debug\Receiver.pdb
     文件         180  2012-08-09 22:36  SocketTest\Receiver\obj\Debug\Receiver.Properties.Resources.resources
     目录           0  2012-08-09 22:34  SocketTest\Receiver\obj\Debug\Refactor\
     目录           0  2012-08-10 00:37  SocketTest\Receiver\obj\Debug\TempPE\
     文件        4608  2012-08-10 00:37  SocketTest\Receiver\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
     文件         554  2012-08-12 00:41  SocketTest\Receiver\obj\Receiver.csproj.FileListAbsolute.txt
     文件         474  2012-08-09 22:34  SocketTest\Receiver\Program.cs
     目录           0  2012-08-09 14:26  SocketTest\Receiver\Properties\
     文件        1182  2012-08-09 14:26  SocketTest\Receiver\Properties\AssemblyInfo.cs
     文件        2840  2012-08-10 00:37  SocketTest\Receiver\Properties\Resources.Designer.cs
     文件        5612  2012-08-09 14:26  SocketTest\Receiver\Properties\Resources.resx
     文件        1106  2012-08-10 00:37  SocketTest\Receiver\Properties\Settings.Designer.cs
     文件         249  2012-08-09 14:26  SocketTest\Receiver\Properties\Settings.settings
     文件        3295  2012-08-10 00:54  SocketTest\Receiver\Receiver.csproj
     目录           0  2012-08-12 00:41  SocketTest\Sender\
............此处省略31个文件信息

评论

共有 条评论