如何在套接字应用程序

时间:2016-05-18 10:05:48

标签: c# sockets

我赢得了here所描述的套接字应用。到目前为止,我正在使用ReadAllBytes方法并从大文件中读取所有内容。我制作了一个新方法,它将以块的形式读取我的文件,这将有助于我的记忆。这是方法:

public static void ChunksToSend(string path)
{
   int chunkSize = 1024;
   byte[] chunk = new byte[chunkSize];

   using (FileStream fileReader = new FileStream(path, FileMode.Open, FileAccess.Read))
   {
        BinaryReader binaryReader = new BinaryReader(fileReader);
        int bytesToRead = (int)fileReader.Length;
        do
        {
            chunk = binaryReader.ReadBytes(chunkSize);
            bytesToRead -= chunkSize;
        } while (bytesToRead > 0);

        binaryReader.Close();
   }
}

现在,我需要从客户端的链接中将此新方法与此行连接:

int bytesSent = sender.Send(result);

看起来很简单,但是我需要发送TCP标头的问题才能获得文件大小和扩展代码。客户端的行是:

//join arrays, file size info, TCP header
...
//file extension info, TCP Header

所以,我需要加入所有内容并像迭代那样在迭代中发送它。问题可能看起来很简单,但如果我能做到,我就不会发布。感谢。

更新

对于我发送文件大小信息的前4个字节,我使用的是来自ReadAllBytes的信息,但现在我可以使用:

long length = new System.IO.FileInfo(path).Length;

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题:

        public static void StartClient()
        {
            byte[] msg;
            try
            {
                string inputFile = @"C:\TCPIP\test_big.iso";
                IPAddress ipAd = IPAddress.Parse("192.168.137.71");
                IPEndPoint remoteEP = new IPEndPoint(ipAd, 1234);
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sender.Connect(remoteEP);

                Console.WriteLine("Client connected to {0}", sender.RemoteEndPoint.ToString());
                Console.WriteLine("Sending file...");

                //8 bytes, file lenght info, TCP Header


                long length = new System.IO.FileInfo(inputFile).Length;
                byte[] fileLenght = BitConverter.GetBytes(length);

                //1 byte, file extension info, TCP Header
                byte extension = 2; //file extension code
                byte[] newArray = new byte[fileLenght.Length + 1];
                fileLenght.CopyTo(newArray, 1);
                newArray[0] = extension;
                fileLenght = newArray;

                //send TCP Header
                int bytesSent = sender.Send(fileLenght);

                //send file in chunks
                int chunkSize = 1024;
                byte[] chunk = new byte[chunkSize];
                int SendPackage;
                using (FileStream fileReader = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
                {
                    BinaryReader binaryReader = new BinaryReader(fileReader);
                    int bytesToRead = (int)fileReader.Length;
                    do
                    {
                        chunk = binaryReader.ReadBytes(chunkSize);
                        bytesToRead -= chunkSize;
                        SendPackage = sender.Send(chunk);
                    } while (bytesToRead > 0);

                binaryReader.Close();
                }

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

                Console.WriteLine("\nPress ENTER to continue...");
                Console.Read();

            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }
        }