• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
三人行者,必有我师焉
为了自己的理想,我愿意付出全部的力量
博客园    首页    新随笔    联系   管理    订阅  订阅

UDP写的点对点的聊天程序,需要写IP

如果下面的代码有错误或者有不懂的地方可以联系QQ:41305987
Code
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 point_talk_to_face
{
    
public partial class Form1 : Form
    {
      
        
public Form1()
        {
            InitializeComponent();
        }
        
private Thread th;
        UdpClient udp, udp2;
        
bool l = true;
        
private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        
private void Form1_Load(object sender, EventArgs e)
        {
           System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls 
= false; 
           th 
= new Thread(new ThreadStart(Listen));
           th.Start();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
   
/*****************************SEND****************************************************/
                udp2 
= new UdpClient(textBox3.Text,9999);
                Byte[] data 
= Encoding.ASCII.GetBytes(textBox2.Text);
                udp2.Send(data, data.Length);
                textBox1.AppendText(
"我对"+textBox3.Text+"说:"+textBox2.Text+"\n");
                udp2.Close();
               
this.textBox2.Clear();
                textBox2.Focus();
   
/**********************************SEND*****************************************************/
        }
        
private void Listen()
        {
  
/********************************************RECEIVE**************************************************/
            udp 
= new UdpClient(9999);
            IPEndPoint remoteiep 
= new IPEndPoint(IPAddress.Any, 9999);

            
while (l)
            {
                
try
                {
                    Byte[] data 
= udp.Receive(ref remoteiep);

                    
if (data.Length > 0)
                    {
                        
string word = Encoding.ASCII.GetString(data);
                        label1.Text 
= remoteiep.Address.ToString();
                        label2.Text 
= remoteiep.Port.ToString();
                        textBox1.AppendText(label1.Text 
+ "对我说:" + word.ToString() + "\n");
                    }
                    
else
                    {
                        
                    }
                }
                
catch (Exception ex)
                {
            
//        textBox1.AppendText(ex.Message);
            
//          MessageBox.Show(ex.Message.ToString());
                }
            }
    
/*******************************************RECEIVE***********************************************************/
        }

        
private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        
private void close(object sender, FormClosingEventArgs e)
        {  
            th.Abort();
            l 
= false;
            udp.Close();
        }

        
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}

 

 

posted @ 2008-09-19 15:35  wsy6634  阅读(356)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3