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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据