Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
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();
        }

    }
Posted
Updated 17-Dec-10 18:16pm
v2
Comments
RaviRanjanKr 18-Dec-10 0:18am    
always use Code Block to wrap your code to make it readability.
JOAT-MON 18-Dec-10 1:44am    
what is the error you are getting?
Abhinav S 18-Dec-10 4:07am    
What is the error?
Sandeep Mewara 18-Dec-10 6:14am    
Code dump. No details.
Sergey Alexandrovich Kryukov 18-Dec-10 18:21pm    
You need to 1) describe the problem; 2) if there is an exception, provide complete dump of exception, but not just message, also all chain of inner exceptions with types and stack trace (important); 3) describe run-time conditions and what to do during run-time to reproduce your problem.

Now, I cannot see immediate problem, but I can see one "sin": your communication code is interlaced with your UI, form-related code. You really should separate communication from UI as clearly as you can. Not only this would help you to see and formulate your problem more to the point: chances are, you might figure our the resolution of your problem down the road.

1 solution

how can i know the correct answer for this question???
 
Share this answer
 
Comments
fjdiewornncalwe 11-Jan-11 10:35am    
This is better suited to be added as a comment above, not as an answer.
Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900