program.cs

来自「it bout file transfer this is about clie」· CS 代码 · 共 56 行

CS
56
字号
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace Server
{
    class Program
    {
        static NetworkStream nStream;
        static StreamReader readImageData;

        static StringBuilder BlockData = new StringBuilder();
        static bool Done=false;

        static void Main(string[] args)
        {
            byte[] IpByte = { 127, 0, 0, 1 };
            IPAddress ipAddress = new IPAddress(IpByte);
            TcpListener tcpListener = new TcpListener(ipAddress, 5555);
            tcpListener.Start();
            Console.WriteLine("Server Started");

            TcpClient tcpClient = tcpListener.AcceptTcpClient();
            Console.WriteLine("Connection Made");
            nStream = tcpClient.GetStream();
            readImageData = new StreamReader(nStream);

            string data;

            while (Done==false)
            {
                while ((data= readImageData.ReadLine()) != null)
                {
                     BlockData.Append(data);
                }

                Done = true;
            }
            byte[] byte_image = Convert.FromBase64String(BlockData.ToString());

            // Change File Name Here 
            FileStream fs = new FileStream("logo.jpg", FileMode.Create);
            fs.Write(byte_image, 0, byte_image.Length);
            fs.Flush();
            Console.WriteLine("Check the Debug folder in the Server Project to see the transfered file.");
            Console.ReadKey();

            readImageData.Close();
            tcpClient.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?