资源简介

C# TCP服务器客户端通讯源码,帮助出学者学习代码!

资源截图

代码片段和文件信息

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.Sockets;//TcpClient类与TcpListener类是两个专门用于TCP协议编程的类
using System.Net; //利用TcpClient类提供的方法,可以通过网络进行连接、发送和接收网络数据流
//using System.Timers;
using System.Threading;
using System.IO;

namespace tcpclient
{
    public partial class client : Form
    {
        private string ipadd = “127.0.0.1“;//默认本地;
        private int port=5555;
        TcpClient _client;
        NetworkStream ns = null;
        private StreamReader strReader;
        private StreamWriter strWriter;
        
        private Thread recvthread = null;//接收信息线程
        private Thread sendthread = null;//发送信息线程
        private Thread serverthread = null;//服务线程
        bool tag = true;//设置标志位,标志是否接收数据当断开连接的时候,tag=false,表示不接收数据
        

        private void Connection()   //连接服务器的方法
        {
            try
            {
                IPAddress ipaddress = IPAddress.Parse(textBox1.Text);
                ipadd = Convert.ToString(ipaddress);
                port = Convert.ToInt32(textBox2.Text);
                //richTextBox2.Text = “Try to connect to “ + ipaddress + “:“ + port + “...\n“;
                _client = new TcpClient(ipadd port);
                

                ns = _client.GetStream();//返回用于发送和接收的数据流
                strReader = new StreamReader(ns);
                strWriter = new StreamWriter(ns);

                recvthread = new Thread(new ThreadStart(recvdata)); //创建接收信息线程,并启动
                recvthread.Start();
                //richTextBox2.Text += “------与主机“ + ipaddress + “:“ + port + “建立连接----\n“;
                richTextBox2.Text += “------与服务器建立连接------\n“;

                //ns.Close();
                //client.Close();

                //获取本地的IP和本地端口
                IPEndPoint localIEP = (IPEndPoint)_client.Client.LocalEndPoint;
                //MessageBox.Show(localIEP.Address.ToString());//本地IP 
                label3.Text = “本地端口:“ + localIEP.Port.ToString() + ““;//本地端口
                label4.Text = “本地IP:“ + localIEP.Address.ToString() + ““;//本地端口    

                button1.Enabled = false;
                button2.Enabled = true;
                send.Enabled = true;
                tag = true;
                textBox1.Enabled = false;
                textBox2.Enabled = false;


            }
            catch (Exception e)
            {
                //MessageBox.Show(“连接目标主机被拒绝“);
                //richTextBox2.Text += “连接目标主机被拒绝\n“;
                //MessageBox.Show(e.Message “提示“);
                Console.WriteLine(e);
            }
        }

        private void recvdata()  //接收数据
        {
            
            byte[] bytes = new byte[1024];
            //int bytesRead = ns.Read(bytes 0 bytes.Length);//接收从服务器返回的信息
            int bytesRead;
            
      

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

     文件      13312  2014-01-03 11:25  tcp协议\tcpclient\tcpclient\tcpclient\bin\Debug\tcpclient.exe

     文件      28160  2014-01-03 11:25  tcp协议\tcpclient\tcpclient\tcpclient\bin\Debug\tcpclient.pdb

     文件      14328  2014-02-22 14:51  tcp协议\tcpclient\tcpclient\tcpclient\bin\Debug\tcpclient.vshost.exe

     文件        490  2009-06-11 05:14  tcp协议\tcpclient\tcpclient\tcpclient\bin\Debug\tcpclient.vshost.exe.manifest

     文件       8329  2014-01-03 11:26  tcp协议\tcpclient\tcpclient\tcpclient\Form1.cs

     文件       8789  2013-11-28 09:44  tcp协议\tcpclient\tcpclient\tcpclient\Form1.Designer.cs

     文件       5814  2013-11-28 09:44  tcp协议\tcpclient\tcpclient\tcpclient\Form1.resx

     文件        180  2013-11-28 09:44  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.client.resources

     文件        763  2014-02-22 14:51  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.csproj.FileListAbsolute.txt

     文件        847  2013-11-28 09:44  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.csproj.GenerateResource.Cache

     文件      13312  2014-01-03 11:25  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.exe

     文件      28160  2014-01-03 11:25  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.pdb

     文件        180  2013-11-17 10:55  tcp协议\tcpclient\tcpclient\tcpclient\obj\Debug\tcpclient.Properties.Resources.resources

     文件        493  2013-11-20 10:29  tcp协议\tcpclient\tcpclient\tcpclient\Program.cs

     文件       1368  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient\Properties\AssemblyInfo.cs

     文件       2868  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient\Properties\Resources.Designer.cs

     文件       5612  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient\Properties\Resources.resx

     文件       1094  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient\Properties\Settings.Designer.cs

     文件        249  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient\Properties\Settings.settings

     文件       3764  2013-11-17 10:55  tcp协议\tcpclient\tcpclient\tcpclient\tcpclient.csproj

     文件        917  2013-11-17 10:11  tcp协议\tcpclient\tcpclient\tcpclient.sln

    ..A..H.     22016  2014-02-22 14:51  tcp协议\tcpclient\tcpclient\tcpclient.suo

     文件      12800  2013-11-22 11:06  tcp协议\tcpclient\tcpclient - 副本\tcpclient\bin\Debug\tcpclient.exe

     文件      24064  2013-11-22 11:06  tcp协议\tcpclient\tcpclient - 副本\tcpclient\bin\Debug\tcpclient.pdb

     文件      14328  2013-11-25 10:35  tcp协议\tcpclient\tcpclient - 副本\tcpclient\bin\Debug\tcpclient.vshost.exe

     文件        490  2009-06-11 05:14  tcp协议\tcpclient\tcpclient - 副本\tcpclient\bin\Debug\tcpclient.vshost.exe.manifest

     文件       5199  2013-11-22 11:06  tcp协议\tcpclient\tcpclient - 副本\tcpclient\Form1.cs

     文件       9211  2013-11-22 00:37  tcp协议\tcpclient\tcpclient - 副本\tcpclient\Form1.Designer.cs

     文件       6008  2013-11-22 00:37  tcp协议\tcpclient\tcpclient - 副本\tcpclient\Form1.resx

     文件        180  2013-11-22 10:28  tcp协议\tcpclient\tcpclient - 副本\tcpclient\obj\Debug\tcpclient.client.resources

............此处省略69个文件信息

评论

共有 条评论