当前位置:编程学习 > C#/ASP.NET >>

c# socket小问题

我想做一个小程序用socket发送消息并接收,代码如下,但一直不成功,求教,谢谢
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;
using System.Net;
using System.Threading;

namespace SendMessage
{
   
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread trsend = new Thread(new ThreadStart(sendmsg));
            trsend.Start();
            trsend.Abort();
            
        }
        private void sendmsg()
        {
            Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp );
            sendsocket.Connect("127.0.0.1", 8000);
            byte[] bysend = Encoding.UTF8 .GetBytes(textBox1.Text);
            sendsocket.Send(bysend);
            sendsocket.Shutdown(SocketShutdown.Both);
            sendsocket.Close();
        }
        private void receivemsg()
        {
            Socket receivesocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp );
            IPEndPoint receiveip = new IPEndPoint(IPAddress.Parse ("127.0.0.1"), 8000);
            receivesocket.Bind(receiveip);
            receivesocket.Listen(20);
            try
            {
                while (true)
                {
                    Socket sockettmp = receivesocket.Accept();
                    byte[] buffer = new byte[sockettmp.ReceiveBufferSize];
                    if (receivesocket.Receive(buffer) > 0)
                    {
                        richTextBox1.Text += Encoding.UTF8.GetString(buffer) + Environment.NewLine;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show (ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread trreceive = new Thread(new ThreadStart(receivemsg));
            trreceive.Start();
            
        }

       
        

       
        



    }
}

namespace SendMessage
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 231);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.richTextBox1.Location = new System.Drawing.Point(0, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(292, 190);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = "";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 204);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 3;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(150, 231);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 4;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button2;
    }
}

--------------------编程问答-------------------- 我先点击button2,然后才点击button1的 --------------------编程问答-------------------- 需要将UDP改为TCP;UDP协议是不用侦听的(listen) --------------------编程问答-------------------- http://topic.csdn.net/u/20100810/12/832a3cfa-09af-4c03-99d9-84e872a040f7.html --------------------编程问答-------------------- 你的监听代码在哪里?服务器端先要监听,客户端去连接,服务器接收到连接后Accept,此方法返回一个Socket,用那个Socket才能和客户端通信。 --------------------编程问答--------------------
引用 2 楼 windchenhonglong 的回复:
需要将UDP改为TCP;UDP协议是不用侦听的(listen)

改了还是不行啊 --------------------编程问答--------------------
引用 4 楼 ivy_zheng 的回复:
你的监听代码在哪里?服务器端先要监听,客户端去连接,服务器接收到连接后Accept,此方法返回一个Socket,用那个Socket才能和客户端通信。


过程基本就是这样 --------------------编程问答--------------------
            trsend.Start();
            trsend.Abort();
--------------------编程问答--------------------
            trsend.Start();
            trsend.Abort();


这是什么代码? --------------------编程问答--------------------
引用 4 楼 ivy_zheng 的回复:
你的监听代码在哪里?服务器端先要监听,客户端去连接,服务器接收到连接后Accept,此方法返回一个Socket,用那个Socket才能和客户端通信。

能教一下怎么改吗? --------------------编程问答-------------------- 7,8楼sp1234大神已经说的很清楚了,你把线程启动又销毁什么意思? --------------------编程问答-------------------- trsend.Start();
            trsend.Abort();


去掉后面句..没发送就直接中止了你 --------------------编程问答-------------------- http://www.albahari.com/threading/
http://msdn.microsoft.com/zh-tw/library/a9fyxz7d%28v=VS.80%29.aspx
看一下Thread怎麼執行 --------------------编程问答-------------------- 到网上找一段 可以用的代码 研究研究

或者照着书上来 来写

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,