qq_24267927 2017-11-13 06:48 采纳率: 0%
浏览 1609
已结题

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

图片说明
图片说明
在服务端点击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条回答 默认 最新

  • Liuzwang 2017-11-13 06:56
    关注

    端口设置的话建议不要设置88,设置成一些大些的数字,说不定者88可能被系统服务占用。
    至于其他的,tcp通信用c#没做过。用qt倒是会。

    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn