Jump to content

Error in Client Socket Program, plz help!!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts
hellooo everyone,
I am using the Microsoft Visual studio and i have done the first part of C# Server Socket Program,But debugging of Client Socket Program has given this error ('WindowsFormsApplication1.Form1.Dispose(bool)': no suitable method found to override").Kindly help me out.

The Client socket Program is,

using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("127.0.0.1", 8888);
label1.Text = "Client Socket Program - Server Connected ...";
}

private void button1_Click(object sender, EventArgs e)
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();

byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
msg("Data from Server : " + returndata);
}

public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
}
}
}

					
					

#2
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Hy.

The program start out, or no? When become you an error message?(because this source worked for me i put also using System.Net; into the program)
I have a complett messenger program in c# with source:thumbup1: and if you want i send you the source:cool:

#3
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
What version of Visual Studio are you using?? The problem appears to be with an incorrect call to the Forms Dispose method. This may be due to incorrect including(using) of other namespaces. I've quickly got it to compile with the following:

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private TcpClient clientSocket = new System.Net.Sockets.TcpClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            msg("Client Started");
            clientSocket.Connect("127.0.0.1", 8888);
            label1.Text = "Client Socket Program - Server Connected ...";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            msg("Data from Server : " + returndata);
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        
     
    }
}

However, make sure that the following is in Form.Designer.cs


namespace WindowsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 25);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(268, 181);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(9, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(37, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Status";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(160, 221);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(129, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "click me";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click_1);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
    }
}



Also, I'm assuming that you created a textbox,a label, and a button in your graphical form. It should be noted that there are un-handled exceptions in your code that you should handle in case it crashes. I tried running it and got an error with the Socket programming. To be more specific I got an InvalidOperationException from the NetworkStream object in your button1_Click method.

Hope that helps man.

#4
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts

While(!EOF) said:

What version of Visual Studio are you using?? The problem appears to be with an incorrect call to the Forms Dispose method. This may be due to incorrect including(using) of other namespaces. I've quickly got it to compile with the following:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net.Sockets;

using System.Net;


namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        private TcpClient clientSocket = new System.Net.Sockets.TcpClient();


        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            msg("Client Started");

            clientSocket.Connect("127.0.0.1", 8888);

            label1.Text = "Client Socket Program - Server Connected ...";

        }


        private void button1_Click(object sender, EventArgs e)

        {

            NetworkStream serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");

            serverStream.Write(outStream, 0, outStream.Length);

            serverStream.Flush();


            byte[] inStream = new byte[10025];

            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

            string returndata = System.Text.Encoding.ASCII.GetString(inStream);

            msg("Data from Server : " + returndata);

        }


        public void msg(string mesg)

        {

            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;

        }


        

     

    }

}


However, make sure that the following is in Form.Designer.cs



namespace WindowsApplication1

{

    partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;


        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }


        #region Windows Form Designer generated code


        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.label1 = new System.Windows.Forms.Label();

            this.button1 = new System.Windows.Forms.Button();

            this.SuspendLayout();

            // 

            // textBox1

            // 

            this.textBox1.Location = new System.Drawing.Point(12, 25);

            this.textBox1.Multiline = true;

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(268, 181);

            this.textBox1.TabIndex = 0;

            // 

            // label1

            // 

            this.label1.AutoSize = true;

            this.label1.Location = new System.Drawing.Point(9, 9);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(37, 13);

            this.label1.TabIndex = 1;

            this.label1.Text = "Status";

            // 

            // button1

            // 

            this.button1.Location = new System.Drawing.Point(160, 221);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(129, 23);

            this.button1.TabIndex = 2;

            this.button1.Text = "click me";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click_1);

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(292, 266);

            this.Controls.Add(this.button1);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.textBox1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);

            this.PerformLayout();


        }


        #endregion


        private System.Windows.Forms.TextBox textBox1;

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.Button button1;

    }

}




Also, I'm assuming that you created a textbox,a label, and a button in your graphical form. It should be noted that there are un-handled exceptions in your code that you should handle in case it crashes. I tried running it and got an error with the Socket programming. To be more specific I got an InvalidOperationException from the NetworkStream object in your button1_Click method.

Hope that helps man.



I am using version 10. Thnx I try then let u know!!

#5
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts

yoda174 said:

Hy.

The program start out, or no? When become you an error message?(because this source worked for me i put also using System.Net; into the program)
I have a complett messenger program in c# with source:thumbup1: and if you want i send you the source:cool:

No, the Program did not start!!!

#6
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts
Now the Program is giving the following errors:

Error 1 The name 'label1' does not exist in the current context

Error 2 The name 'textBox1' does not exist in the current context

#7
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
hy. Try create a label with name label1 and a textbox with name textBox1 then rebuild the project:thumbup1:

#8
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
Since you are using Visual Studio you can create them graphically without any programming. However, you may need to change the names like yoda174 said. From your toolbox pane just drag a label and a textbox on the Form Designer. Then in their properties pane just name them label1 and textBox1 respectively. :)

#9
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts
hey guys, i have run the program :) thnx 2 all of you!!

#10
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
We are glad that it work:thumbup1:

#11
hir@

hir@

    Newbie

  • Members
  • PipPip
  • 11 posts
hey guys i am back again,lols. Both, the client and server programs are running but now i have to made connection between them so that they can communicate, kindly tell me how can i do this without changing the code !!

#12
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Sry...I dont understand please write datails...What do you mean under communicate...etc:thumbup1: