本人因为不懂用udp在局域网传输数据,所以照着一位大侠的日志写了一个小的聊天程序。因为没有内网的环境,所以只是在单机的不同端口上做了试验。还有好多东西都不是很懂,请各位高人帮帮忙吧~先谢谢了!~~
S:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;namespace S
{
public partial class Form1 : Form
{
UdpClient uc; //声明UDPClient
UdpClient ucs; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
ucs = new UdpClient(); //初始化
uc = new UdpClient(8888); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 ucs.Send(b, b.Length, Dns.GetHostName(), 8889); } private void listen()
{ //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 8888); while (true)
{ //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text); }C:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Configuration;namespace C
{
public partial class Form1 : Form
{
UdpClient uc = null; //声明UDPClient
UdpClient ucs; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
//注意此处端口号要与发送方相同
ucs = new UdpClient(); //初始化
uc = new UdpClient(8889); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
} private void listen()
{ //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);
while (true)
{
//获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox
this.listBox1.Items.Add(text); } } private void button1_Click(object sender, EventArgs e)
{
string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 ucs.Send(b, b.Length, Dns.GetHostName(), 8888);
}
}
}因为本人根本也不懂这些东西,看了很多资料也没有一个从头到尾教局域网聊天的,所以有如下几个问题:
1.能不能不通过服务端与客户端进行聊天,最好只是平级的客户端与客户端的,像QQ那样的?
2.如果上面的东西拿到内网上面会不会不能传输数据,如果不行该怎么改呢?
3.IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);里面的IP改成什么样都一样,这个有什么作用呢?
4.大家有没有做过类似的东西的,希望可以给我点帮助[email protected]
5.因为要交任务,所以只有这几天时间,如果这个根本就不是我想要的东西,那还得重新写,网上众说纷纭,有说简单的,有说复杂的,希望大家给我点指示!~再次感谢中... ...
S:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;namespace S
{
public partial class Form1 : Form
{
UdpClient uc; //声明UDPClient
UdpClient ucs; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
ucs = new UdpClient(); //初始化
uc = new UdpClient(8888); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 ucs.Send(b, b.Length, Dns.GetHostName(), 8889); } private void listen()
{ //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 8888); while (true)
{ //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text); }C:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Configuration;namespace C
{
public partial class Form1 : Form
{
UdpClient uc = null; //声明UDPClient
UdpClient ucs; public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
//注意此处端口号要与发送方相同
ucs = new UdpClient(); //初始化
uc = new UdpClient(8889); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
} private void listen()
{ //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);
while (true)
{
//获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox
this.listBox1.Items.Add(text); } } private void button1_Click(object sender, EventArgs e)
{
string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 ucs.Send(b, b.Length, Dns.GetHostName(), 8888);
}
}
}因为本人根本也不懂这些东西,看了很多资料也没有一个从头到尾教局域网聊天的,所以有如下几个问题:
1.能不能不通过服务端与客户端进行聊天,最好只是平级的客户端与客户端的,像QQ那样的?
2.如果上面的东西拿到内网上面会不会不能传输数据,如果不行该怎么改呢?
3.IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);里面的IP改成什么样都一样,这个有什么作用呢?
4.大家有没有做过类似的东西的,希望可以给我点帮助[email protected]
5.因为要交任务,所以只有这几天时间,如果这个根本就不是我想要的东西,那还得重新写,网上众说纷纭,有说简单的,有说复杂的,希望大家给我点指示!~再次感谢中... ...
解决方案 »
- combobox根据拼音选取对应的项?
- 关于listbox问题
- 请问这有个Com动态库中有个函数定义如下: ReadComm(ref Array bytRead_Result1),哪个参数如何定义及调用?
- 我要启动一个项目,出现下面的提示,....请高手指点.
- Visual Studio 2005 Professional .Edition(RTM)和Visual Studio 2005 Team Suite.Edition(Release Candidate)什么区别?
- 在c#里,有什么办法能让一个字符串数组用一个字符连接起来
- 总是感觉winfrom中的datagrid没有web中好,功能少了很多,请问这是为什么??或者winfrom中有什么独到之处??
- 程序集签名后解析dll怎么都报错,失心疯了
- 那里可以下载基于Exchange的messenger
- WEBFORM怎样在IE开始运行时即将它最大化?
- 怎么取datagridview中datagridviewcomboboxcolumn中的combobox控件
- SQL语句问题
2能
3有地址才能发送消息
4.。
5.。
http://download.csdn.net/source/1550301
C/S的,简单的聊天工具当然容易,就是几个字符串传来传去~但是功能慢慢扩展,当然就会越来越复杂~~~