码迷,mamicode.com
首页 > Windows程序 > 详细

c# 客户端

时间:2016-07-07 09:51:08      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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.Threading;
using System.Net.Sockets;
using System.Net;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket client;
        private void button2_Click(object sender, EventArgs e)
        {
             client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(this.ip.Text);
            IPEndPoint prot = new IPEndPoint(ip, Convert.ToInt32(this.port.Text));
            client.Connect(prot); //服务器端口
            listin("连接成功");
            Thread th = new Thread(sends);
            th.Start();
            
            
            
            
        }

        public void listin(string msg)
        {

            this.textBox2.AppendText(msg + "\r\n");

        }

        void sends() 
        {
            while (true)
            {
                Byte[] buf = new Byte[1024 * 1024 * 3];
                int result = client.Receive(buf);
                if (result == 0)
                {
                    break;
                }
                string str = Encoding.UTF8.GetString(buf, 0, result);
                listin(client.RemoteEndPoint.ToString() + ":" + str);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string str = this.textBox1.Text.Trim();
            Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str);
            client.Send(buf);
        }


    }
}

  

c# 客户端

标签:

原文地址:http://www.cnblogs.com/mengluo/p/5648809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!