当前位置:编程学习 > C#/ASP.NET >>

tcp 通信 数据发不过去 怎么回事?

下面是server代码
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;
using System.Net.Sockets;
using System.IO;

namespace server
{
    public partial class Form1 : Form
    {
        IPAddress localaddress;
        TcpClient cilent;
        TcpListener server;
        NetworkStream stream;
        StreamReader sr;
        StreamWriter sw;
        
        public Form1()
        {
            InitializeComponent();
            //timer1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           localaddress =IPAddress.Parse(textBox1.Text);
          server = new TcpListener(localaddress, int.Parse(textBox2.Text));
           server.Start();
           button1.Text = "YES";
           cilent = server.AcceptTcpClient();
           stream = cilent.GetStream();
           sr = new StreamReader(stream);
           sw = new StreamWriter(stream);
         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sw.WriteLine(textBox3.Text);
            sw.Flush();


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            textBox4.Text += "客户端" + sr.ReadLine();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cilent.Close();
        }
    }
}

下面是cilent代码:
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.IO;
using System.Net;
using System.Net.Sockets;

namespace cilent1
{
    public partial class Form1 : Form
    {
        TcpClient cilent;
        IPAddress localAddress;
        NetworkStream stream;
        StreamReader sr;
        StreamWriter sw;
        
        public Form1()
        {
            InitializeComponent();
           //timer1.Enabled=true;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            localAddress = IPAddress.Parse(textBox1.Text);
            cilent = new TcpClient();
            cilent.Connect(localAddress, int.Parse(textBox2.Text));
            stream = cilent.GetStream();
            sr = new StreamReader(stream);
            sw = new StreamWriter(stream);
           
        }

        private void button3_Click(object sender, EventArgs e)
        {

            sw.WriteLine(textBox4.Text);
            sw.Flush();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string msg=sr.ReadLine();

            textBox3.Text = "服务器"+msg;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cilent.Close();
        }
    }
}

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,