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

如果下面的代码有错误或者有不懂的地方可以联系QQ:41305987
ContractedBlock.gif ExpandedBlockStart.gif 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)
        {
        }
    }
}

 

 

转载于:https://www.cnblogs.com/wsy6634/archive/2008/09/19/1294203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值