调试窗口时不会出现

时间:2013-04-20 08:09:13

标签: winforms window

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Server_chat
{
    class server
    {
        private static int myProt = 10000;
        private static byte[] buf2 = new byte[1024];
        private static Socket serverSocket;
        private static string message2;

        /// <summary>
        /// server socket
        /// </summary>
        public void Server_run()
        {
            IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
            IPAddress ip = ipHost.AddressList[0];

            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(ip,Program.form1.localport));
            serverSocket.Listen(10);
            Program.form1.myscan_box = "启动监听成功 !!";

            Thread myThread = new Thread(ListenClientConnect);
            myThread.IsBackground = true;
            myThread.Start();
        }

        /// <summary>  
        /// 监听客户端连接  
        /// </summary>  
        private static void ListenClientConnect()
        {
            while (true)  // 点对点通信
            {
                Socket clientSocket = serverSocket.Accept();
                if (clientSocket.Connected == true)
                {
                    IPEndPoint client_endpoint = (IPEndPoint)clientSocket.RemoteEndPoint;
                    Program.form1.myclientip_box = client_endpoint.Address.ToString() + "\\" + client_endpoint.Port.ToString();
                    Program.form1.myscan_box = "连接客户端 " + client_endpoint.Address.ToString() + " 成功 !!";
                    //服务端接收线程
                    Thread receiveThread = new Thread(ReceiveMessage);
                    receiveThread.IsBackground = true;
                    receiveThread.Start(clientSocket);
                    //服务端发送线程
                    Thread sendThread = new Thread(SendMessage);
                    sendThread.IsBackground = true;
                    sendThread.Start(clientSocket);
                    break;
                }
            }
        }

        /// <summary>  
        /// receive message 
        /// </summary>  
        /// <param name="clientSocket"></param>  
        private static void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                try
                {
                    int len = myClientSocket.Receive(buf2);
                    message2 = Encoding.ASCII.GetString(buf2).Substring(0, len);
                    IPEndPoint client_endpoint = (IPEndPoint)myClientSocket.RemoteEndPoint;
                    Program.form1.myscan_box = client_endpoint.Address +": "+ message2;
                }
                catch (Exception ex)
                {
                    Program.form1.myscan_box = ex.Message;
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        }


        /// <summary>
        /// send message
        /// </summary>
        /// <param name="clientSocket"></param>
        private static void SendMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;
            while (true)
            {
                if (Program.form1.send_flag11 == 1)
                {
                    myClientSocket.Send(Encoding.ASCII.GetBytes(Program.form1.message11));
                    Program.form1.send_flag11 = 0;
                }
            }

        }

    }
}

`启动或调试程序时,窗口不会出现。但vs2012建议调试会话准备就绪。

启动程序时,会出现一个窗口,但会出现一个弹出窗口,其中显示“程序已停止工作,选择调试或停止”。

有什么问题?我该怎么办?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace Server_chat
{
    public partial class Form1 : Form
    {
        public string localip;
        public int localport = 10002;
        public string s_sendMessage;
        public static int send_flag = 0;


        public string message11
        {
            get { return s_sendMessage; }
        }
        public int send_flag11
        {
            get { return send_flag; }
            set { send_flag = value; }
        }

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddr = ipHost.AddressList[0];
            localip = ipAddr.ToString();

            myip_box.Text = localip;
            port_box.Text = localport.ToString();


            server myserver = new server();
            Thread th_server = new Thread(myserver.Server_run);
            th_server.IsBackground = true;
            th_server.Start();
        }

        private void setting_btn_Click(object sender, EventArgs e)
        {
            localport = Int16.Parse(port_box.Text);
            scan_box.AppendText("\r\n" + "端口改成: " + port_box.Text);
        }

        private void send_btn_Click(object sender, EventArgs e)
        {
            s_sendMessage = chat_box.Text;
            chat_box.Text = "";
            scan_box.AppendText(s_sendMessage + "\r\n");
            send_flag = 1;
        }

    }
}

0 个答案:

没有答案