日期:2014-05-17  浏览次数:20890 次

我快被SOCKET折磨死了,大家帮我看一下C#代码吧(简洁易懂)
本帖最后由 u011708759 于 2013-10-03 09:43:17 编辑
服务器:

//不解释,跳过
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;
using System.Net.Sockets;
using System.IO;
using System.Threading;



namespace Socket_Programing_Test
{

    public partial class Form1 : Form
    {
        //声明变量,tcpclient,端口和委托
        TcpListener tcplistener;
        int port;
        delegate void AppendStringDelegate(string str);
        AppendStringDelegate appendstringdelegate;


        public Form1()
        {
            InitializeComponent();
            appendstringdelegate = new AppendStringDelegate(AppendString);
        }

        private void button建立连接_Click(object sender, EventArgs e)
        {
            //从textbox1中获取端口
            port = Convert.ToInt32(textBox1.Text);
            try
            {
                tcplistener = new TcpListener(IPAddress.Any,port);
                tcplistener.Start();
            }
            catch(Exception ex)
            {
                listBox1.Items.Add("失败");
                listBox1.Items.Add(ex.Message);
                return;
            }
            //建立新的线程用于获取客户端请求信息
            ThreadStart ts = new ThreadStart(Listenclientconnect);
            Thread mythread = new Thread(ts);
            mythread.Start();
           
            listBox1.Items.Add("成功");

        }