聊天室C#代码 [英] Chatroom c# code

查看:68
本文介绍了聊天室C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更正此错误C#代码?

how to correct this error c# code?

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

namespace P1
{
    public partial class P1 : Form
    {
        private static Socket client;
        private static byte[] datae = new byte[1024];
        private System.Windows.Forms.OpenFileDialog openFile;

        public P1()
        {
            InitializeComponent();
        }

         //thread safe method used to add items to the listbox
        private void AddToListBox(object oo)
        {
            Invoke(new MethodInvoker(
                           delegate { results.Items.Add(oo); }
                           ));
        }

        void AcceptConn(IAsyncResult iar)
        {
            Socket oldserver = (Socket)iar.AsyncState;
            client = oldserver.EndAccept(iar);
            AddToListBox("Connection from: " + client.RemoteEndPoint.ToString());

            Thread receiver = new Thread(new ThreadStart(ReceiveData));
            receiver.Start();
        }

        void Connected(IAsyncResult iar)
        {
            try
            {
                client.EndConnect(iar);
                AddToListBox("Connected to: " + client.RemoteEndPoint.ToString());
                Thread receiver = new Thread(new ThreadStart(ReceiveData));
                receiver.Start();

            }
            catch (SocketException)
            {
                AddToListBox("Error connecting");
            }
        }

        void SendData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int sent = remote.EndSend(iar);
        }

        void ReceiveData()
        {
            int recv;
            string data;
            while (true)
            {
                byte[] data = new byte[1024];
                recv = client.Receive(data);
                string data =Encoding.ASCII.GetString(data , 0, recv);
                if (string data == "bye")
                break;
                {
                    //........

                }
                else ( (string data == "file")){
                    //............
                }



              AddToListBox(stringData);
                {

            string data = "bye";
            byte[] message = Encoding.ASCII.GetBytes(stringData);
            client.Send(message);
            client.Close();
            AddToListBox("Connection stopped");

            return;

                    {
        private void connect_Click_1(object sender, EventArgs e)
        {

            AddToListBox("Connecting...");


            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(""), 0);
            //......................
        }

        private void listen_Click_1(object sender, EventArgs e)
        {
            AddToListBox("Listining to client");

            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, -1);
          newsock.Bind(iep);
newsock.Listen(100);



        }

        private void sendit_Click_1(object sender, EventArgs e)
        {
          IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

        sock.Bind(ipEnd);
            sock.Listen(100);

            Socket clientSock = sock.Accept();
            byte[] messagea = Encoding.ASCII.GetBytes(txtMsg.Text);
            txtMsg.Clear();
            client.BeginSend(messagea, 0, messagea.Length, 0, new AsyncCallback(SendData), client);
            string clientStr = "Client Data Received";
            byte[] sendData = new byte[1024];
     sendData= Encoding.ASCII.GetBytes(clientStr);
        clientSock.Send(sendData);


        }



        //................................................
        //      Sending File function
        //................................................
        public void ReciveFile()
        {

            try
            {



            }
            catch (Exception ex)
            {
                Console.WriteLine("File Sending fail." + ex.Message);
            }
        }


        public void SendFile(string fileName)
        {
            try
            {
                Console.WriteLine("That program can transfer small file. I''ve test up to 850kb file");


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void sendFile_Click(object sender, EventArgs e)
        {
               byte[] message = Encoding.ASCII.GetBytes("file");

               if (openFile.ShowDialog(this) == DialogResult.OK)
                   txtFileName.Text = openFile.FileName;

                 client.BeginSend(message, 0, message.Length, 0, new AsyncCallback(SendData), client);
                 SendFile(System.IO.Path.GetFileName(txtFileName.Text.ToString()));
            Console.ReadLine();
        }

    }

推荐答案

我如何知道此问题的正确答案???


这篇关于聊天室C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆