Jump to content

need URGENT help in client-server programming

- - - - -

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

#1
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
i need help in client server socket programming, both these programs are running successfully, and supposed to have communication wid each other,,but they dont.......i am using mirosoft visual studio 2010 for this project.

SERVER SOCKET PROGRAM is,


using System;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
int requestCount = 0;

TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
requestCount = 0;

while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " + dataFromClient);
string serverResponse = "Server response " + Convert.ToString(requestCount);
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}
}



AND CLIENT SOCKET PROGRAM IS,


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



namespace WindowsApplication1

{

static class Program

{

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

}


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






public Form1()
{
InitializeComponent();
}


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();
this.button1.Location = new System.Drawing.Point(160, 221);
this.button1.Size = new System.Drawing.Size(129, 23);
this.button1.Name = "button1";

this.button1.TabIndex = 0;
this.button1.Text = "click here to send data to server";
this.button1.UseVisualStyleBackColor = true;

//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(15, 30);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Text = " client started";
this.textBox1.Size = new System.Drawing.Size(268, 181);
this.textBox1.TabIndex = 1;

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 = 2;
this.label1.Text = "Client Socket Program - Server Connected ..";

this.label1.AutoSize = true;


this.ClientSize = new System.Drawing.Size(292, 266);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

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();



}
private void button1_Click(object sender, System.EventArgs e)
{
// need object reference here to access the other class and its fields!
System.Windows.Forms.Form frm1= new Form1();



}

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




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;
}

public TextBox textBox1 { get; set; }

public Label label1 { get; set; }



private System.Windows.Forms.Button button1;

public System.EventHandler button1_Click_1 { get; set; }




}

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Hi,
If you wrap your code in code blocks you can indent it and make it easiler to read.

Secondly what is the problem, does the client never seem to connect, does it connect but you don't see any data?

#3
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
when i debugged it, it gives no error,,and when client program run,it supposed to have connection wid server,,, this is the problem that connection b/w client and server is not established

#4
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Hy. Try change the server and client Ip to 127.0.0.1 (it is the localhost -when you run the server+client on your computer) or when in lan run the program please forward the port in router:thumbup1:

In my opinion it is easier that you put 2 textbox(one for ip and one for port address in server&client unless the program run in console)

I work on one lanchat program also:w00t::D

Good Luck!

#5
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
i tried this,but this is not working

#6
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
this worked for me

server

using System;

using System.Net.Sockets;

using System.Text;


namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            TcpListener serverSocket = new TcpListener(8888);

            int requestCount = 0;


            TcpClient clientSocket = default(TcpClient);

            serverSocket.Start();

            Console.WriteLine(" >> Server Started");

            clientSocket = serverSocket.AcceptTcpClient();

            Console.WriteLine(" >> Accept connection from client");

            requestCount = 0;


            while ((true))

            {

                try

                {

                    requestCount = requestCount + 1;

                    NetworkStream networkStream = clientSocket.GetStream();

                    byte[] bytesFrom = new byte[10025];

                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                    Console.WriteLine(" >> Data from client - " + dataFromClient);

                    string serverResponse = "Server response " + Convert.ToString(requestCount);

                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);

                    networkStream.Write(sendBytes, 0, sendBytes.Length);

                    networkStream.Flush();

                    Console.WriteLine(" >> " + serverResponse);

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.ToString());

                }

            }


            clientSocket.Close();

            serverSocket.Stop();

            Console.WriteLine(" >> exit");

            Console.ReadLine();

        }

    }

}



and the client

using System;

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.Net;

using System.Net.Sockets;


namespace WindowsFormsApplication1

{

    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 ...";





            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;

        }

    }

}

My system showed me a popup window with firewall blocking and i dont blocked the program( try turn off firewall or
add an exception to firewall :thumbup1:)

I tryed the program's in Microsoft Visual Studio 2008

Attached Files



#7
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
can u plz guide me,,
when i run the code of server that u have given,it works but for client,it gives 3 errors

Error 1 The name 'InitializeComponent' does not exist in the current context
Error 2 The name 'label1' does not exist in the current context
Error 3 The name 'textBox1' does not exist in the current context

regrads

#8
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
and i m using visual studio 2010,,,,,,is there any difference b/w both which affects the coding?

#9
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Hy. Create a label with name "label1", create a textbox with name"textBox1" then rebulid the project:thumbup1:

When it isn't work here is my Form1.Designer code :thumbup:

namespace client

{

    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.label1 = new System.Windows.Forms.Label();

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

            this.SuspendLayout();

            // 

            // label1

            // 

            this.label1.AutoSize = true;

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

            this.label1.Name = "label1";

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

            this.label1.TabIndex = 0;

            this.label1.Text = "label1";

            // 

            // textBox1

            // 

            this.textBox1.Location = new System.Drawing.Point(15, 34);

            this.textBox1.Multiline = true;

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(265, 222);

            this.textBox1.TabIndex = 1;

            // 

            // Form1

            // 

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

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

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

            this.Controls.Add(this.textBox1);

            this.Controls.Add(this.label1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();


        }


        #endregion


        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.TextBox textBox1;

    }

}


I lose the button because there was no role.

#10
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
still it is not working,i put urs code of form1.designer and also client in their respective position but .......

#11
bushramemon

bushramemon

    Newbie

  • Members
  • PipPip
  • 10 posts
both programs are running but no communication takes place between them

#12
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Ok. Please send me msn/email address then i send you my working source and project.

Regards