• 大小: 11KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签: C#  TCP  IP  通信  

资源简介

C#代码写的TCP/IP通信接收数据,输入IP与端口即可

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCP_Client
{
    public partial class Form1 : Form
    {
        NetworkStream ns;
        TcpClient clientsocket;
        bool connected;
        Thread receive;

        public Form1()
        {
            InitializeComponent(); 
            this.Text = “客户端“;
            iptxt.Text = “127.0.0.1“;
        }
        public delegate void Recdatadelegate(String str);
        private void showInfo(string s)
        {
            Invoke(new Recdatadelegate(rtbChatIn.AppendText) new object[] { s });
        }
        private void EstablishConnection()
        {
            sslbStatus.Text = “正在连接到服务器“;

            try
            {
                string serverIP = iptxt.Text;
                int serverPort = Convert.ToInt32(porttxt.Text);
                clientsocket = new TcpClient(serverIP serverPort);
                ns = clientsocket.GetStream();
                //sr = new StreamReader(ns);
                connected = true;
                sslbStatus.Text = “已连接“;

            }
            catch (Exception)
            {
                MessageBox.Show(“不能连接到服务器!“ “错误“
                 MessageBoxButtons.OK MessageBoxIcon.Exclamation);
                sslbStatus.Text = “已断开连接“;
            }
        }
        private void RegisterWithServer()
        {
            try
            {
                string command = “*RS“ + txtCarNO.Text + “V1000047V0000.0000N00000.0000E000.00000080804FFFFFBFF#“; // string command = “CHAT|“ + clientname + “: “ + ChatOut.Text + “\r\n“;
                Byte[] outbytes;
                if (rdoASCII.Checked)
                    outbytes = System.Text.Encoding.Default.GetBytes(command.ToCharArray());
                else
                    outbytes = System.Text.Encoding.Unicode.GetBytes(command.ToCharArray());


                ns.Write(outbytes 0 outbytes.Length);
                //clientsocket.Close(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void QuitChat()
        {
            if (connected)
            {
                try
                {
                    ns.Close();
                    clientsocket.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                if (receive != null && receive.IsAlive)
                    receive.Abort();
            }

            connected = false;

        }

        private void ReceiveChat()
        {
            bool keepalive = true;
            while (keepalive)
            {
         

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10343  2008-02-01 11:11  Form1.Designer.cs

     文件       6014  2008-02-01 11:11  Form1.resx

     文件        469  2008-02-01 10:27  Program.cs

     文件       3227  2008-02-01 10:28  TCP_Client.csproj

     目录          0  2008-02-01 10:27  Properties

     文件       1184  2008-02-01 10:27  Properties\AssemblyInfo.cs

     文件       2874  2008-02-01 10:27  Properties\Resources.Designer.cs

     文件       5612  2008-02-01 10:27  Properties\Resources.resx

     文件       1093  2008-02-01 10:27  Properties\Settings.Designer.cs

     文件        249  2008-02-01 10:27  Properties\Settings.settings

     文件       7863  2008-02-01 14:00  Form1.cs

----------- ---------  ---------- -----  ----

                38928                    11


评论

共有 条评论