using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace ReceiveFiles
{
    public partial class Form1 : Form
    {
        private const int BufferSize = 1024;
        public string Status = string.Empty;
        public Thread T = null;
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Server is Running...";
            ThreadStart Ts = new ThreadStart(StartReceiving);
            T = new Thread(Ts);
            T.Start();           
        }
        public void StartReceiving()
        {
            ReceiveTCP(6666);
        }
        
        
        public void ReceiveTCP(int portN)
        {
            TcpListener Listener = null;
            try
            {
                Listener = new TcpListener(IPAddress.Any, portN);
                Listener.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }            byte[] RecData = new byte[BufferSize];
            int RecBytes;            for (; ; )
            {
                TcpClient client = null;
                NetworkStream netstream = null;
                Status = string.Empty;
                try
                {
                    
                   string message = "Accept the Incoming File ";
                   string caption = "Incoming Connection";
                   MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                   DialogResult result;
                  if (Listener.Pending())
                   {
                        client = Listener.AcceptTcpClient();
                        netstream = client.GetStream();
                       
                        Status = "Connected to a client\n";
                       result = MessageBox.Show(message, caption, buttons);                       
                       if (result == System.Windows.Forms.DialogResult.Yes)
                       {
                          
                            string SaveFileName=string.Empty;
                            SaveFileDialog DialogSave = new SaveFileDialog();
                            DialogSave.Filter = "All files (*.*)|*.*";
                            DialogSave.RestoreDirectory = true;
                            DialogSave.Title = "Where do you want to save the file?";
                            DialogSave.InitialDirectory = @"C:/";
                            if (DialogSave.ShowDialog() == DialogResult.OK)
                                SaveFileName = DialogSave.FileName;
                            if (SaveFileName != string.Empty)
                            {
                                int totalrecbytes = 0;
                                FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
                                while ((RecBytes = netstream.Read(RecData, 0, RecData.Length)) > 0)
                                {
                                    Fs.Write(RecData, 0, RecBytes);
                                    totalrecbytes += RecBytes;
                                }
                                Fs.Close();
                            }
                            netstream.Close();
                            client.Close();                       }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    //netstream.Close();
                }
            }
        }        private void btnExit_Click(object sender, EventArgs e)
        {
            T.Abort();
            this.Close();
        }        
    }
}

解决方案 »

  1.   

    这是网上找来学习的接收部分代码,从客户端传来的文件这个接收程序有反映,但程序现在只执行到
     Status = "Connected to a client\n";
    这句,以下的代码不执行,也就是后面的保存文件界面根本出不来,给看看需要修改什么么,查了好久了~~~
      

  2.   

    在测试的时候在CMD下打开netstat命令没有发现指定端口的TCP连接 但接收端出现
                        
                       string message = "Accept the Incoming File ";
                       string caption = "Incoming Connection";
       这一段代码执行成功的效果,这样算是通过socket建立了tcp连接了么 代码不执行的原因是因为连接每建立上么还是别的问题 比如权限,在线等明白人答疑
      

  3.   


    //判断连接
            
            //执行一个阻塞调用来接受请求,可以用server.AcceptSocket()在这里。
            TcpClient client = server.AcceptTcpClient();            
            //已经联通        data = null;        //获取用于读取和写入一个流对象
            NetworkStream stream = client.GetStream();        int i;
            // 循环来接收所有由客户端发送的数据.
            while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
            {   
              // 数据字节转换为ASCII字符串.
              data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
              //接收
           
              //由客户端发送的数据.
              data = data.ToUpper();          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);          //发回一个响应
              stream.Write(msg, 0, msg.Length);
              //发送         
            }
     
            client.Close();
      

  4.   

    你的程序,应该加个判断,在你执行到你那句status时,是不是正在等待客户端响应啊