如何从套接字连续接收数据?

Posted

技术标签:

【中文标题】如何从套接字连续接收数据?【英文标题】:How can I receive data from socket continuously? 【发布时间】:2016-06-29 06:38:14 【问题描述】:

我正在尝试通过 Windows 服务从套接字接收数据,但是当我建立连接时,它变得即时。我想不断地接收它(端口关闭时它会断开连接)。我怎样才能做到这一点?

这是我现在的输出;

"GET/HTTP/1.1

主机:127.0.0.1:1994

连接:保持活动

缓存控制:max-age=0

升级不安全请求:1

用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(Khtml,如 Gecko)Chrome/51.0.2704.106 Safari/537.36

接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8

接受编码:gzip、deflate、sdch

接受语言:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4

连接继续?假”

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Linq;
using System.Threading;
using System.Net.Sockets;
using System.Diagnostics;
using System.ComponentModel;
using System.ServiceProcess;
using System.Collections.Generic;

namespace AServiceTest

    public partial class Service1 : System.ServiceProcess.ServiceBase
    
        Thread th;
        bool isRunning = false;
        byte[] bytes = new byte[1024];

        public Service1() 
        
            InitializeComponent();
        
        protected override void OnStart(string[] args)
        
            th = new Thread(DoThis);
            th.Start();
            isRunning = true;            
        
        private void DoThis()
        
            while (isRunning)
            
                    Socket listener, connecter, acc;
                    listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    connecter = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Variables

                    listener.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1994));
                    listener.Listen(0);

                    acc = listener.Accept(); //Accepting comm with client part
                    bool IsConnected = !((listener.Poll(1000, SelectMode.SelectRead) && (listener.Available == 0)) || !listener.Connected);

                    File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?1 " + IsConnected);


                new Thread(() =>
                

                    byte[] bytes = new byte[1024];
                    int byteCount = acc.Receive(bytes, SocketFlags.None);

                    File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?2 " + IsConnected);

                    Console.WriteLine(Encoding.UTF8.GetString(bytes)); //It encodes before writing to the screen
                    File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + " " + Encoding.UTF8.GetString(bytes));

                    File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?3 " + IsConnected);


                    File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed? " + IsConnected);
                    Console.WriteLine("Does connection proceeding? " + IsConnected);

                ).Start();
            
                
        protected override void OnStop() 
        
            isRunning = false;
            th = null;
        
        private void InitializeComponent() 
        
            this.ServiceName = "AServiceTest";
            this.CanStop = true;
            this.AutoLog = false;
            this.EventLog.Log = "Application";
            this.EventLog.Source = "Service1";
        
    

【问题讨论】:

【参考方案1】:

要连续接收数据,您实际上需要放入一些循环。

例如:

private void StartProcessing(Socket serverSocket)

    var clientSocket = serverSocket.Accept();
    StartReceiveing(clientSocket);


private void StartReceiveing(Socket clientSocket)

    const int maxBufferSize = 1024;

    try
    
        while (true)
        
            var buffer = new byte[maxBufferSize];

            var bytesRead = clientSocket.Receive(buffer);

            if (ClientIsConnected(clientSocket))
            
                var actualData = new byte[bytesRead];

                Array.Copy(buffer, actualData, bytesRead);
                OnDataReceived(actualData);
            
            else
            
                OnDisconnected(clientSocket);
            
        
    
    catch (SocketException ex)
    
        Console.WriteLine(ex.Message);
    


private void OnDisconnected(Socket issuedSocket)

    if (issuedSocket != null)
    
        issuedSocket.Shutdown(SocketShutdown.Both);
        issuedSocket.Close();

        StartProcessing(listener);
    


private void OnDataReceived(byte[] data)

    //do cool things


private static bool ClientIsConnected(Socket socket)

    return !(socket.Poll(1000, SelectMode.SelectRead) && socket.Available == 0);

另外,就您而言,我考虑阅读有关 socket async API 以及有关任务和异步编程的信息。有了这些,你可以做一些很酷很干净的事情,比如:

await Task.Factory.FromAsync(serverSocket.BeginAccept, result => serverSocket.EndAccept(result), serverSocket);

哪个不会阻止您当前的Thread

【讨论】:

以上是关于如何从套接字连续接收数据?的主要内容,如果未能解决你的问题,请参考以下文章

用于接收数据的套接字的Tkinter线程

通过 C 中的套接字快速接收不同长度数据包的连续流?

如何通过 iOS 中的套接字从服务器接收数据?

如何使用 Python 从 Web 套接字接收数据

从同一个套接字发送和接收数据的简单 UDP 示例

如何检查套接字在一段时间内没有收到