Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:45:30+00:00 2026-06-02T03:45:30+00:00

I have a homework to build an application which will send and receive simple

  • 0

I have a homework to build an application which will send and receive simple string between server and client. I know how to establish connection, but don’t know how to send and receive string. This is my code :

public partial class Form1 : Form
{
    private Thread n_server;
    private Thread n_client;
    private Thread n_send_server;
    private TcpClient client;
    private TcpListener listener;
    private int port = 2222;
    private string IP = " ";
    private Socket socket;
    public Form1()
    {
        InitializeComponent();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    public void Server()
    {
        listener = new TcpListener(IPAddress.Any, port);
        listener.Start();
        try
        {
            socket = listener.AcceptSocket();
            if (socket.Connected)
            {
                textBox2.Invoke((MethodInvoker)delegate { textBox2.Text = "Client : " + socket.RemoteEndPoint.ToString(); });
            }
        }
        catch
        {
        }
    }

    public void Client()
    {
        IP = "localhost";
        client = new TcpClient();
        try
        {
            client.Connect(IP, port);
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message);
        }

        if (client.Connected)
        {
            textBox3.Invoke((MethodInvoker)delegate { textBox3.Text = "Connected..."; });

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        n_server = new Thread(new ThreadStart(Server)); 
        n_server.IsBackground = true;
        n_server.Start();
        textBox1.Text = "Server up";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        n_client = new Thread(new ThreadStart(Client));
        n_client.IsBackground = true;
        n_client.Start();
    }

    private void send()
    {
        // I want to use this method for both buttons : "send button" on server side    and "send button"
        // on client side. First I read text from textbox2 on server side or textbox3
        // on client side than accept and write the string to label2(s) or label3(c).
        // 
    }


    private void button3_Click(object sender, EventArgs e)
    {
        n_send_server = new Thread(new ThreadStart(send));
        n_send_server.IsBackground = true;
        n_send_server.Start();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:45:31+00:00Added an answer on June 2, 2026 at 3:45 am

    The following code send and recieve the current date and time from and to the server

    //The following code is for the server application:

    namespace Server
    {
        class Program
        {
            const int PORT_NO = 5000;
            const string SERVER_IP = "127.0.0.1";
    
            static void Main(string[] args)
            {
                //---listen at the specified IP and port no.---
                IPAddress localAdd = IPAddress.Parse(SERVER_IP);
                TcpListener listener = new TcpListener(localAdd, PORT_NO);
                Console.WriteLine("Listening...");
                listener.Start();
    
                //---incoming client connected---
                TcpClient client = listener.AcceptTcpClient();
    
                //---get the incoming data through a network stream---
                NetworkStream nwStream = client.GetStream();
                byte[] buffer = new byte[client.ReceiveBufferSize];
    
                //---read incoming stream---
                int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
    
                //---convert the data received into a string---
                string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received : " + dataReceived);
    
                //---write back the text to the client---
                Console.WriteLine("Sending back : " + dataReceived);
                nwStream.Write(buffer, 0, bytesRead);
                client.Close();
                listener.Stop();
                Console.ReadLine();
            }
        }
    }
    

    //this is the code for the client

    namespace Client
    {
        class Program
        {
            const int PORT_NO = 5000;
            const string SERVER_IP = "127.0.0.1";
            static void Main(string[] args)
            {
                //---data to send to the server---
                string textToSend = DateTime.Now.ToString();
    
                //---create a TCPClient object at the IP and port no.---
                TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
                NetworkStream nwStream = client.GetStream();
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
    
                //---send the text---
                Console.WriteLine("Sending : " + textToSend);
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
    
                //---read back the text---
                byte[] bytesToRead = new byte[client.ReceiveBufferSize];
                int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
                Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
                Console.ReadLine();
                client.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Homework Planner application which I want to create an Alarm for
FYI This is homework. I have to build a Java Chat server. I have
I have a small homework application that writes random numbers from 5 to 77
Have a homework assignment in which I'm supposed to create a vector of pointers
I have a homework assignment that I am supposed to write a http server
I have homework i cant figure out. I know c++ but i am not
I have homework which I have to use scriptlets in , I need to
Alright, so for a homework assignment I had to make a simple application with
I am trying to build an application that will use an Api like google
I have a homework assignment to write a multi-threaded sudoku solver, which finds all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.