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 905177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:10:28+00:00 2026-05-15T16:10:28+00:00

I’m trying to create a client-server program that send and receive a string (its

  • 0

I’m trying to create a client-server program that send and receive a string (its a simple chat app over LAN).

The program works fine, but i need to encrypt it, and that’s the problem, when i use encryption it seems that it close the connection after each time sending the data,and i need to re-run it in order to receive further messages (which I can’t because its chatting and should be continually)

By the way I don’t mind even if its a weak encryption as long as its not plain-text is fine.

Here is one side of my program (server):

public static void Main()
   {
      string data;
      IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);

      Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

      socket.Bind(ip);
      socket.Listen(10);

      Socket client = socket.Accept();
      IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
      Console.WriteLine("Connected with {0} at port {1}",newclient.Address, newclient.Port);

      NetworkStream ns = new NetworkStream(client);
      StreamReader sr = new StreamReader(ns);
      StreamWriter sw = new StreamWriter(ns);

      string welcome = "Welcome";
      sw.WriteLine(welcome);
      sw.Flush();

      while(true)
      {
         data = sr.ReadLine();
         Console.WriteLine(data);
         sw.WriteLine(data);
         sw.Flush();
      }
      Console.WriteLine("Disconnected from {0}", newclient.Address);
      sw.Close();
      sr.Close();
      ns.Close(); 
   }

Here is the example of the code, that I’ve tried to use for encrypting:(server)
IPAddress IP2 = IPAddress.Parse(IP);
TcpListener TCPListen = new TcpListener(IP2, port);

                TCPListen.Start();

            TcpClient TCP = TCPListen.AcceptTcpClient();


            NetworkStream NetStream = TCP.GetStream();




            RijndaelManaged RMCrypto = new RijndaelManaged();


            byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };




            CryptoStream CryptStream = new CryptoStream(NetStream,
               RMCrypto.CreateDecryptor(Key, IV),
               CryptoStreamMode.Read);

            StreamReader SReader = new StreamReader(CryptStream);




            message = SReader.ReadToEnd();

            textBox3.Clear();

            textBox3.Text = message;

            CryptStream.Flush();

            SReader.Close();







            NetStream.Flush();

            NetStream.Close();

            TCPListen.Stop();
            TCP.Close();

I’ve tried to only flush the netstream and cryptstream so can leave the connection open.

  • 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-05-15T16:10:29+00:00Added an answer on May 15, 2026 at 4:10 pm

    Have you considered simply using SSL for your socket communication? (Specifying SslProtocols.Tls).

    SSL Socket support has been in .NET since C# 2.0.


    If you want to keep your code mostly the same as you have now simply use an encoding with your encrypted binary data and that’ll probably fix your problem. For example Convert.ToBase64String before sending.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 465k
  • Answers 465k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Filippo, I understand your frustration with the problem and with… May 16, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer MySQL doesn't support ANSI PIVOT/UNPIVOT syntax, so that leave you… May 16, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer NHibernate has the load operation (as opposed to doing a… May 16, 2026 at 1:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.