客服端

 

 QQ
{
    public partial class Tcpclient : Form
    {
        
private StreamWriter write;
        
private NetworkStream stream;
        
private TcpClient tcpclient;
        
public Tcpclient()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
try
            {
                tcpclient 
= new TcpClient(AddressFamily.InterNetwork);
                tcpclient.Connect(
"127.0.0.1",8000);
                stream 
= tcpclient.GetStream();
                write 
= new StreamWriter(stream);
                write.WriteLine(
this.textBox1.Text);
                write.Flush();
            }
            
catch
            { 
            
            }
        }
    }
}

 

服务器端

 

 QQ
{
    public partial class Tcpserver : Form
    {
        
int port = 8000;//定义侦听端口号
        private Thread threadRead; //创建线程,用以侦听端口号,接受信息
        private TcpListener tcpListen;//侦听端口号 
        private bool blistener = true;
        
private NetworkStream stream;
        
private StreamReader read;
        
private TcpClient client;
        
private delegate void listbox_show(string str);
        listbox_show listboxshow;
        
public Tcpserver()
        {
            InitializeComponent();
            listboxshow 
= new listbox_show(show);
        }

        
private void Listen()
        {
            
try
            {
                tcpListen 
= new TcpListener(8000); //用8000端口来初始化TcpListener实例
                tcpListen.Start();//开始监听
                client = tcpListen.AcceptTcpClient();//通过tcp连接请求
                stream = client.GetStream();  //获取基本的流
                read = new StreamReader(stream); //用得到的网络基础数据流来初始化StreamReader实例
                while (blistener)
                {
                    
string message = read.ReadLine();//从网络基础数据流中读取一行数据
                    if (message == "STOP")
                    {
                        tcpListen.Stop();
//关闭侦听
                        stream.Close();//释放资源
                        read.Close();
                        threadRead.Abort();
                        
return;
                    }
                    
string sTime = DateTime.Now.ToShortDateString();
                    listBox1.Invoke(listboxshow,sTime 
+ " " + message);
                }

            }
            
catch (System.Security.SecurityException)
            {
                MessageBox.Show(
"侦听失败!","错误");
            }
        }
        
private void show(string str)
        {
            
this.listBox1.Items.Add(str);
        }
        
//开始侦听
        private void button1_Click(object sender, EventArgs e)
        {
            threadRead 
= new Thread(new ThreadStart(Listen));
            threadRead.Start();
            
this.button1.Enabled = false;
        }
    }
}

相关文章:

  • 2021-11-01
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-05-27
  • 2021-12-04
  • 2021-08-20
猜你喜欢
  • 2021-11-25
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-09-10
相关资源
相似解决方案