Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The client:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TcpClient tcpc = new TcpClient();
        ASCIIEncoding asce = new ASCIIEncoding();
        NetworkStream ns;
        BinaryReader br;
        BinaryWriter bw;

        public void cli()
        {
            try
            {
                tcpc.Connect(IPAddress.Parse("127.0.0.1"), 5000);

                ns = tcpc.GetStream();

                br = new BinaryReader(ns);
                bw = new BinaryWriter(ns);
                string st = br.ReadString();
                MessageBox.Show("srever send this message:" + st);

            }

            catch { }
        }
        
        public Form1()
        {
            
            InitializeComponent();
            Thread thread = new Thread(new ThreadStart(cli));
            thread.Start();
            
            
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            bw.Write(textBox1.Text);
                     
        }
   
        }

}

=====================================

The server:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;

namespace WindowsFormsApplication2
{
    public partial class server : Form
    {


        BinaryReader br;
        BinaryWriter bw;
        Socket soc;
        NetworkStream ns;
        public void dolisten(){
            try
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                TcpListener tcpl = new TcpListener(ip, 5000);
                tcpl.Start();
                soc = tcpl.AcceptSocket();

                ns = new NetworkStream(soc);
                br = new BinaryReader(ns);
                bw = new BinaryWriter(ns);
                string ss = br.ReadString();
                MessageBox.Show("client send this message" + ss);
            }
            catch             {
                MessageBox.Show("error");
            }
                      

        }
        
        public server()
        {
            InitializeComponent();
            Thread thread = new Thread(new ThreadStart(dolisten));
            thread.Start();
       
        }

       

        private void button1_Click(object sender, EventArgs e)
        {

            string sss = textBox1.Text;
            bw.Write(sss);
            
        }   
    
    }
}


What I have tried:

is it .close() ?
i tried it but VS give me errore of not ending stream or some thing like that!!!
Posted
Updated 15-Dec-17 20:42pm
v2
Comments
Richard MacCutchan 12-Dec-17 13:57pm    
" or some thing like that"
Please don't expect us to guess what the message is; provide the exact text, and explain where it occurs.

It exits because you have designed it to do so: both your client and server threads terminate upon receiving the first input data. This way, after first data receival, the receiving endpoint is no more active.
 
Share this answer
 
listen connection while(true) in server
 
Share this answer
 

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