need URGENT help in client-server programming

MICROSOFT VISUAL STUDIO , NETWORKING , Csharp PROGRAMMING Karāchi, Pakistan
  • 10 years ago

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

    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)

  • 10 years ago

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

    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)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry