Socket编程 消息传送 UDP协议(窗口实现) 客户端
来源:互联网 发布:网络信息发布审查制度 编辑:程序博客网 时间:2024/05/18 06:57
//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 UDP_XZH_Client
{
public partial class Form1 : Form
{
Socket Client = null;
int port = 9999;
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint serverIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
int i = 0;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
button2.Enabled = true;
button3.Enabled = false;
}
//start
private void button2_Click(object sender, EventArgs e)
{
button3.Enabled = true;
button2.Enabled = false;
start();
}
//stop
private void button3_Click(object sender, EventArgs e)
{
button2.Enabled = true;
button3.Enabled = false;
stop();
}
//send
private void button1_Click(object sender, EventArgs e)
{
send();
}
//init
private void Form1_Load(object sender, EventArgs e)
{
}
//start
private void start()
{
try
{
Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] data = new byte[1024];
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
string welcome = "hello! ";
data = Encoding.ASCII.GetBytes(welcome);
Client.SendTo(data, data.Length, SocketFlags.None, ip);
textBox1.AppendText("start success!!");
Thread thread = new Thread(getMsg);
thread.IsBackground = true;
thread.Start();
}
catch (SocketException se)
{
Console.WriteLine(se.ToString());
}
}
//stop
private void stop()
{
try
{
Client.Close();
textBox1.AppendText("client stop!");
}
catch (SocketException se)
{
Console.WriteLine(se.ToString());
}
}
//send
private void send()
{
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
string input = textBox2.Text;
Client.SendTo(Encoding.ASCII.GetBytes(input), Encoding.ASCII.GetBytes(input).Length, SocketFlags.None, ip);
byte[] data = new byte[1024];
Console.WriteLine(input);
}
catch (SocketException se)
{
Console.WriteLine(se.ToString());
}
}
//getMsg
private void getMsg()
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
//string input;
EndPoint Remote = (EndPoint)sender;
try
{
while (true)
{
Byte[] data = new byte[1024];
int recv = Client.ReceiveFrom(data, ref Remote);
textBox1.AppendText(ASCIIEncoding.ASCII.GetString(data));
Console.WriteLine(++i+" value of i");
}
}
catch (SocketException se)
{
Console.WriteLine(se.ToString());
}
}
}
}
//design code
namespace UDP_XZH_Client
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(197, 207);
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);
//
// button2
//
this.button2.Location = new System.Drawing.Point(24, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(145, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 51);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(223, 136);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 209);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(167, 21);
this.textBox2.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
}
- Socket编程 消息传送 UDP协议(窗口实现) 客户端
- Socket编程 消息传送 UDP协议(窗口实现) 服务器
- Socket编程 消息传送 TCP协议(窗口实现) 客户端
- Socket编程 消息传送 TCP协议(窗口实现) 服务器
- Windows socket编程 udp协议传送封装自定义帧
- 实现UDP协议,socket编程,调用到windowsAPI,实现客户端和服务器
- Socket网络编程(二)UDP协议实现聊天工具
- Socket编程-UDP协议
- socket udp 客户端实现
- 网络编程(5)—— 基于Linux系统的UDP协议socket服务器和客户端
- 网络编程(6)—— 基于Windws系统的UDP协议socket服务器和客户端
- socket编程复习(5)-UDP协议
- udp广播局域网传送消息的实现
- VC++中Socket编程的实现-UDP客户端
- 编程实现基于UDP的多线程多客户端socket应用
- Socket编程2-UDP协议
- java中进行socket编程实现tcp、udp协议总结
- socket编程实现简单DNS协议实现获取域名ip(UDP)
- Jetty9 NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
- Oracle中的死锁Dead Lock(二)
- Ubuntu安装navicat
- 推荐10个ICON网站,程序员必备! .
- js操作数组
- Socket编程 消息传送 UDP协议(窗口实现) 客户端
- 解读C++对象的内存布局深层剖写
- 创建一个16整型的数组,先输出数组中元素值为3的倍数的数,在输出下标为3的倍数的元素值
- 最火的Android开源项目
- ubuntu 修改时区
- 管理路由器每次要输密码真是麻烦,自己来写个浏览器
- linux下的ln命令
- VC学习: CPoint,CSize,CRect类说明
- Oracle 10g Scheduler 特性