qq_24267927
qq_24267927
2017-11-13 06:48

C# 关于tcp连接的 做了两个窗口,以下是代码和示例图片,客户端的文字没有传输过去

2
  • winform
  • tcp
  • c#

图片说明
图片说明
在服务端点击listen后,客户端输入文字,因该要通过tcp过去,可是服务端没有显示,以下是代码
//服务端//
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;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace TCPMyserver
{
public partial class MyServer : Form
{
delegate void SetTextCallBack(string text);
private TcpClient client;
private TcpListener server;
NetworkStream stream;
struct IpAndPort
{
public string Ip;
public string Port;
}

    public MyServer()
    {
        InitializeComponent();

    }

    private void Listen_Click(object sender, EventArgs e)
    {
        Listen.Enabled = false;
        if (Serverport.Text.Trim() == string.Empty)
        {
            return;
        }
        if (ServerIP.Text.Trim() == string.Empty)
        {
            return;
        }
        Thread thread = new Thread(Receiveandlisten);
        IpAndPort ipHePort = new IpAndPort
        {
            Ip = ServerIP.Text,
            Port = Serverport.Text
        };


        thread.Start((object)ipHePort);

    }
    private void Receiveandlisten(object ipandport)
    {
        IpAndPort ipAndPort = (IpAndPort)ipandport;
        IPAddress iP = IPAddress.Parse(ipAndPort.Ip);
        server = new TcpListener(iP, int.Parse(ipAndPort.Port));
        server.Start();
       while(true)
        {

            client = server.AcceptTcpClient();
           int bufferSize = client.ReceiveBufferSize;
            byte [] buffer = new byte[bufferSize];
            stream = client.GetStream();
            stream.Read(buffer, 0, bufferSize);
            String showdata = Encoding.Default.GetString(buffer).Trim('\0');
            SetText(showdata);
            stream.Flush();
            stream.Close();
            client.Close();


        }
    }
    public void SetText(string text)
    {
        try
        {
            if (this.Txt_ServerShowData.InvokeRequired)
            {
                SetTextCallBack d = new SetTextCallBack(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {

                this.Txt_ServerShowData.Text += DateTime.Now.ToString() + "\n" + text + "\n";
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

//客户端//
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;
using System.Net.Sockets;
using System.Threading;

namespace MyClient
{
public partial class Form1 : Form
{

    string sendString = null;//要发送的字符串
    byte[] sendData = null;//要发送的字节数组
    TcpClient client = null;//TcpClient实例
    NetworkStream stream = null;//网络流
    public IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//远程主机IP
    public int remotePort = 88;//远程主机端口

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(send);

    }
    private void send()
    {
        while (true)
        {
            sendString = Txt_send.Text;
            sendData = Encoding.Default.GetBytes(sendString);
            client = new TcpClient();
            try
            {
                client.Connect(remoteIP, remotePort);

            }

                catch (System.Exception ex)
            {

                MessageBox.Show("连接失败,服务器无响应");
                return;
            }
            stream = client.GetStream();
            stream.Write(sendData, 0, sendData.Length);



        }


    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答