Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends i need an client server socket program that is a string value should be shared between client and server that is if client(A) send a message to server(B) the B should view the message and Respond to A its my Task pls Help....

Thanks in Advance
Posted
Comments
[no name] 6-Jul-13 7:23am    
Exactly the same answer as the last time you asked. Use the "send" or "write" method for whatever protocol you are using.
usha C 6-Jul-13 7:29am    
using Tcp/ip protocol pls help ..
[no name] 6-Jul-13 7:46am    
Help with what? All you have here is "do my work for me". To get help you need to ask a specific question and/or describe some sort of a problem. You have not described any kind of a problem nor have you asked any kind of a question. If you have read the FAQ then very first thing you would have seen is "Have you searched or Googled for a solution?". My guess is no because if you had, you would have found tons of example code for passing strings using TCP/IP.
usha C 8-Jul-13 5:12am    
I done myself

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
usha C 8-Jul-13 5:25am    
Thank You
OriginalGriff 8-Jul-13 5:58am    
You're welcome - I'm glad you got it done so quickly!
usha C 8-Jul-13 6:30am    
Ya :-)
Sushil Mate 8-Jul-13 5:32am    
My +5 for encouraging the OP. See below ans :)
Client Code

C#
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.Sockets;
using System.Threading;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;


namespace HAN
{
    public partial class multinet : Form
    {
        Connection cn = new Connection();
        SqlCommand cmd;
        string rand, pub, enc,label;
        int hc;
        int rd;
        TcpClient clientSocket = default(TcpClient);
        string message;
        public multinet(string s, string s1, string s2, string s3)
        {
            InitializeComponent();
            label = s.ToString();
            rand = s1.ToString();
            pub = s2.ToString();
            enc = s3.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cn.con.Open();
        }

        public void msg(string mesg)
        {
          listBox1.Items.Add(">> " + mesg);
        }


        private void button1_Click(object sender, EventArgs e)
        {
         
           // string message = "Test";
            message = textBox1.Text.ToString();

        try
        {
            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            Int32 port = 500;
            TcpClient client = new TcpClient(textBox2.Text.ToString(),port); //Unsure of IP to use.

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // Get a client stream for reading and writing.
            //  Stream stream = client.GetStream();

            NetworkStream stream = client.GetStream();

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            listBox1.Items.Add(String.Format("Sent: {0}", message));

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            listBox1.Items.Add(String.Format("Validation Sucess: {0}", responseData));

            // Close everything.
            stream.Close();
            client.Close();
        }
        catch (ArgumentNullException an)
        {
            listBox1.Items.Add(String.Format("ArgumentNullException: {0}", an));
        }
        catch (SocketException se)
        {
            listBox1.Items.Add(String.Format("SocketException: {0}", se));
        }
        }

        private void button2_Click(object sender, EventArgs e)
        {
          
            rd = Convert.ToInt32(rand);
            int pubk = Convert.ToInt32(pub);
            hc = rd * pubk;
            string hcd = Convert.ToString(hc);
            MessageBox.Show("Enter The Key to Transfer as Ittration 1"+hcd.ToString());
           
          
          
        }
      
        private void button3_Click(object sender, EventArgs e)
        {
            split slt = new split(label.ToString(), rand.ToString(), pub.ToString(), enc.ToString());
            slt.Show();
            this.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("The Shared Key is"+label.ToString());
        }
        int bc;
        private void button4_Click(object sender, EventArgs e)
        {
            int nsx = hc * rd;
             bc = 2 * nsx;
            string bcn = Convert.ToString(bc);
            MessageBox.Show("Enter The Key to Transfer as Ittration 2" + bcn.ToString());
        }
        int neibur;
        private void button5_Click(object sender, EventArgs e)
        {
            int ncw = bc * rd;
             neibur = 2 * ncw;
            string ncd = Convert.ToString(neibur);
            MessageBox.Show("Enter The Key to Transfer as Ittration 3" + ncd.ToString());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int cc = neibur * 2;
            string cc1 = Convert.ToString(cc);
            MessageBox.Show("Enter The Key to Transfer as Ittration 4" + cc1.ToString());
        }

       
        

       


       

    }
}


Server Code

C#
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.Sockets;
using System.Net;

namespace serve
{
    public partial class Form1 : Form
    {
       // backgroundWorker1 =bw;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

            if (backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                try
                {
                    // Set the TcpListener on port 1333.
                    Int32 port = 500;
                    IPAddress localAddr = IPAddress.Parse("192.168.1.7");
                    TcpListener server = new TcpListener(IPAddress.Any, port);

                    // Start listening for client requests.
                    server.Start();

                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data = null;

                    // Enter the listening loop.
                    while (true)
                    {
                        backgroundWorker1.ReportProgress(0, "Waiting for a connection... ");
                        // Perform a blocking call to accept requests.
                        // You could also user server.AcceptSocket() here.
                        TcpClient client = server.AcceptTcpClient();
                        backgroundWorker1.ReportProgress(0, "Connected!");

                        data = null;

                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();

                        int i;

                        // Loop to receive all the data sent by the client.
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            backgroundWorker1.ReportProgress(0, String.Format("Received: {0}", data));

                            // Process the data sent by the client.
                            data = String.Format("I Have Received Your Packet: {0}", data);

                            byte[] mssg = System.Text.Encoding.ASCII.GetBytes(data);

                            // Send back a response.
                            stream.Write(mssg, 0, mssg.Length);
                            backgroundWorker1.ReportProgress(0, String.Format("Sent: {0}", data));
                        }

                        // Shutdown and end connection
                        client.Close();
                    }
                }
                catch (SocketException se)
                {
                    backgroundWorker1.ReportProgress(0, String.Format("SocketException: {0}", se));
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            listBox1.Items.Add(e.UserState);
        }
    }
}



It was done by myself only with some emergency case we are getting your help sir Thank You...Thanks fr Your Motivation...
 
Share this answer
 
Comments
Sushil Mate 8-Jul-13 5:34am    
Great, That's the way to do it :)
usha C 8-Jul-13 5:40am    
Yep :-)
OriginalGriff 8-Jul-13 5:58am    
Well done!

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