分类目录
文章索引模板
udp drag drop - 一月 10, 2010 by yippee

CodeProject: Genesis UDP Server and Client. Free source code and programming help
http://www.codeproject.com/KB/IP/Genesis.aspx



Control.DragOver 事件 – 小石软件 Sshsoft Studio
http://blog.sshsoft.cn/article.asp?id=61
e.Effect = DragDropEffects.None;


DragDropEffects Enumeration (System.Windows.Forms)
http://msdn.microsoft.com/en-us/library/system.windows.forms.dragdropeffects.aspx



SubSonic 多表查询 + 分页_dinghSky
http://hi.baidu.com/dinghsky/blog/item/7e505717f7372c064b90a70e.html



使用SubSonic生成数据访问层步骤 – 踏浪飞鸿的专栏 – CSDN博客
http://blog.csdn.net/qinyanlu/archive/2009/06/01/4231306.aspx
/联合查询


DataTable dt = new Select(Product.Columns.ProductName, Category.Columns.CategoryName,Supplier.Columns.CompanyName).From(Product.Schema).InnerJoin(Category.Schema).InnerJoin(Supplier.Schema).ExecuteDataSet().Tables[0];


 


______________________________________________________________________________


//子查询


DataSet dt = new Select().From(Product.Schema)


    .Where(“productid”).In(1, 2, 3, 4, 5)ExeCuteDataSet();


 


DataSet dt = new Select().From(Product.Schema).Where(Product.Columns.CategoryID)


            .In(


            new Select(“categoryID”).From(Category.Schema).Where(Category.Columns.CategoryName).IsEqualTo(“Produce”))


            .ExecuteDataSet() ;


______________________________________________________________________________________________


//聚合函数使用


 


double result = new


    Select(Aggregate.Sum(“UnitPrice*Quantity”, “ProductSales”))


    .From(OrderDetail.Schema)


    .ExecuteScalar<double>();


用SubSonic构建一个简单Blog引擎_㊣网络淘金_____________________
http://hi.baidu.com/feik/blog/item/17bd7a0ee99519cf7acbe168.html



INNER JOIN语法联接多个表建记录集 – 多个,记录,语法,会员,段号,数据,编号,类别,INNERJ – 中国网管联盟
http://www.bitscn.com/network/protect/200607/49248.html



SQLite数据库能不能完美地代替文本数据库?_阿伟的技术空间
http://hi.baidu.com/awei0611/blog/item/e7803612f830b7c8c2fd7807.html



c# 关于udp服务器的问题
http://topic.csdn.net/u/20080803/17/b8466d09-9fcc-4786-b7ef-4f70c7aaf895.html



CSDN技术中心 使用UDP实现一个时钟服务器
http://dev.csdn.net/article/63102.shtm



Re: UDP并发服务器设计讨论一:一台主机、一个CPU,实现单位时间内最大并发数 – China Linux Forum
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=program&Number=599643



A Simple Multi-threaded TCP/UDP Server and Client V2.
http://www.c-sharpcorner.com/UploadFile/patricklam/simpleTcpUdpServerClientPL211222005040054AM/simpleTcpUdpServerClientPL2.aspx



Simple Udp Server : Udp Server « Network « C# / C Sharp
http://www.java2s.com/Code/CSharp/Network/SimpleUdpServer.htm



C# Udp Socket例子 – 一路前行 – 博客园
http://www.cnblogs.com/zhangpengshou/archive/2008/10/09/1307622.html



Udp 服务器 C#实现代码 – 小李程式™的专栏 – 博客园
http://www.cnblogs.com/nehu/articles/218512.html



 

标签:, ,
vc led udp - 十一月 9, 2009 by yippee

C#&VC以Socket实现UDP通信 搜索墙
http://www.pkwall.com/result/ae21456



Introduction to Using the XML DOM from Visual C++ — CodeGuru.com
http://www.codeguru.com/cpp/misc/misc/article.php/c3707/



Visual C++ 2008“系统无法执行指定的程序”的解决方法_林缘雨梦
http://hi.baidu.com/linyuanyumeng/blog/item/2de91287bf9ae62fc65cc38e.html



vc socket udp_boden
http://hi.baidu.com/bodendong/blog/item/49645545eac0f438879473d3.html



安装Microsoft.NET Framework 3.0老是失败(页 1) – 开发工具和语言 – .NET Framework 相关 – ITPUB论坛 – powered by ITPUB.NET
http://www.itpub.net/archiver/tid-1160750.html



关于System.ComponentModel.Win32Exception的问题,请高手救命!!!
http://topic.csdn.net/u/20070801/09/60dc8dc8-31a9-4daf-b168-2db4f3783f4b.html



关于System.ComponentModel.Win32Exception的问题,请高手救命!!!
http://topic.csdn.net/u/20070801/09/60dc8dc8-31a9-4daf-b168-2db4f3783f4b.html



VC++ XML Read and Write Article: C++ Source Code for Creating and Processing XML documents, Load And Save XML File
http://www.ucancode.net/CPP_Library_Control_Tool/VC-XML-Read-Write-Create-Processing-XML-Document-Article–Source-Code.htm



LED显示器通讯
http://topic.csdn.net/u/20080530/17/f2a92a02-9e06-4ff2-ad7d-5d75dfc45bc2.html



Visual C++ 2008“系统无法执行指定的程序”的解决方法 « WO的博客
http://ggn.cc/blog/1/archives/36



如何用vc创建和读取xml文件?? VC/MFC / 基础类 – CSDN社区 community.csdn.net
http://topic.csdn.net/t/20050428/00/3972087.html



请教c#如何定义DLL的入口参数 – .NET – 编程论坛
http://bbs.zxbc.cn/thread-54042-1-1.html



C# 遍历 XML文件 节点 net XML节点 遍历 – skybot的专栏 – CSDN博客
http://blog.csdn.net/daonidedie/archive/2007/10/26/1845457.aspx



C# 视频监控系列(1):准备 – 农民伯伯 – 博客园 – 孙焱的日志 – 网易博客
http://sunxitao88.blog.163.com/blog/static/68314439200992505150592/



 

标签:, ,
20090114 udpclient rec 10054 - 八月 18, 2009 by yippee

CodeProject: TinyUDP – Simple UDP Client/Server Components for .NET. Free source code and programming help
http://www.codeproject.com/KB/IP/TinyUDP.aspx

C# UdpClient接收消息问题?
http://topic.csdn.net/u/20071226/10/27e8d862-b43a-431b-a44b-adda87d347f3.html

CodeProject: UT2003 Gameserver Status. Free source code and programming help
http://www.codeproject.com/KB/IP/ut2003query.aspx

Strange UDP Socket problem – bytes
http://bytes.com/groups/net-c/237558-strange-udp-socket-problem

Best Udp Client : Udp Client : C# / C Sharp examples (example source code) Organized by topic \\ Network
http://www.demo2s.com/Code/CSharp/Network/BestUdpClient.htm

Best Udp Client – Udp Client
http://www.navioo.com/csharp/source_code/Best_Udp_Client_2178.html

求助:使用UdpClient.Receive方法接收对方回复时,如若对方没有答复,怎么解决Receive方法死等的问题? — 编程爱好者论坛存档帖
http://www.programfan.com/club/showpost.asp?id=13629

UDP Client Problem – bytes
http://bytes.com/groups/net-c/237232-udp-client-problem

UdpClient.BeginReceive gives socket error 10054 – CodeGuru Forums
http://www.codeguru.com/forum/showthread.php?threadid=461370

UdpClient.Receive Method
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.receive(VS.71).aspx

C#中判断socket是否已断开的方法 – 人生如酒 – 博客园
http://www.cnblogs.com/JLL/archive/2008/07/28/203090.html#1269706

UDPListener: System.Net.Sockets.SocketException – bytes
http://bytes.com/groups/net-c/475986-udplistener-system-net-sockets-socketexception

CodeGuru Forums – UdpClient.BeginReceive gives socket error 10054
http://www.codeguru.com/forum/printthread.php?t=461370

.NET csharp SocketException question
http://www.eggheadcafe.com/software/aspnet/32686597/socketexception-question.aspx

将对象转为byte[] – 人生如酒 – 博客园
http://www.cnblogs.com/jll/articles/198851.html

How to: Create a Socket Listener
http://msdn.microsoft.com/en-us/library/bb397809.aspx

udp 远程主机强迫关闭了一个现有的连接
http://topic.csdn.net/u/20081122/16/1983a6f6-b008-4e6f-98c1-8d855679adf2.html

c#关于c/s结构 计算上机时间的问题 .NET技术 / C# – CSDN社区 community.csdn.net
http://topic.csdn.net/t/20050822/19/4224232.html

请问 在使用UdpClient接收远程主机发送过来的Udp数据包时: .NET技术 / C# – CSDN社区 community.csdn.net
http://topic.csdn.net/t/20040806/10/3247892.html

为什么使用UdpClient向本机发送数据包会出错? – 微软中文技术论坛(MSDN and TechNet)
http://forums.microsoft.com/china/ShowPost.aspx?PostID=3762050&SiteID=15

udp 远程主机强迫关闭了一个现有的连接
http://topic.csdn.net/u/20081122/16/1983a6f6-b008-4e6f-98c1-8d855679adf2.html?2062577672

大家来看看这段简单的代码有什么问题 .NET技术 / C# – CSDN社区 community.csdn.net
http://topic.csdn.net/t/20060214/18/4555932.html

用UDPClient通信出错,怎么回事呢 .NET技术 / C# – CSDN社区 community.csdn.net
http://topic.csdn.net/t/20060317/12/4620994.html

Best Udp Client : Udp Client « Network « C# / C Sharp
http://www.java2s.com/Code/CSharp/Network/BestUdpClient.htm

An existing connection was forcibly closed by the remote host : .NET Framework Networking and Communication : .NET Development : Microsoft Forums
http://social.microsoft.com/Forums/en-US/netfxnetcom/thread/bcc98596-c9c7-48f1-92ca-021bbe42cbb1

Advanced UDP communications and client side packet loss
http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework/2006-01/msg00920.html

microsoft.public.dotnet.languages.csharp: Re: Strange UDP Socket problem
http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2004-02/0959.html

UDP Delivers: Take Total Control Of Your Networking With .NET And UDP
http://msdn.microsoft.com/en-us/magazine/cc163648.aspx

"Late Binding" error is killing me! on VB.NET at whitebinder.com – find your answers.
http://www.whitebinder.com/article/1008528/vb-net/late-binding-error-is-killing-me.aspx

标签:, ,
20081030 c# socket udp python - 七月 2, 2009 by yippee

WingIDE的破解_Forrest World
http://hi.baidu.com/forestener/blog/item/d54b1ef02cb745c17931aaee.html

(WPF学习记录)第六章 Dock与Grid – - CSDNBlog
http://blog.csdn.net/changjiangboy/archive/2008/09/16/2934556.aspx

Python — The eric Python IDE_逆水寒
http://hi.baidu.com/shiiuen/blog/item/69d273810b1f7fdebc3e1eed.html

Eric4在Windows下的安装_闲客暇居
http://hi.baidu.com/runningon/blog/item/091dd009c4c80187d1581b05.html

一起学WPF系列(3):窗体 – Robin\’s Space – 博客园
http://www.cnblogs.com/jillzhang/archive/2008/04/05/1138526.html

用C#写的聊天程序,贴出所有的源代码,大家帮忙瞧瞧!_软界知道
http://www.softwelt.com/Know/KnowDetail-3168871.html

CodeProject: LAN Chat Using Multicating. Free source code and programming help
http://69.10.233.10/KB/cs/LanChat.aspx

Can\’t make UdpClient object send and receive broadcast messages – .NET C#
http://bytes.com/forum/thread269735.html

CodeProject: Using TCP/IP or UDP to Communicate with Serialized Objects. Free source code and programming help
http://69.10.233.10/KB/IP/ComctSerializedObjects.aspx

CodeProject: Genesis UDP Server and Client. Free source code and programming help
http://69.10.233.10/KB/IP/Genesis.aspx

CodeProject: Getting active TCP/UDP connections on a box. Free source code and programming help
http://69.10.233.10/KB/IP/iphlpapi.aspx

CodeProject: Testing TCP and UDP socket servers using C# and .NET. Free source code and programming help
http://69.10.233.10/KB/IP/testingsocketservers.aspx

CodeProject: A Chat Application Using Asynchronous UDP sockets. Free source code and programming help
http://69.10.233.10/KB/IP/ChatAppAsynchUDPSocks.aspx

CodeProject: Scalable Server Events with .NET UDP Sockets. Free source code and programming help
http://69.10.233.10/KB/IP/UDP_Events.aspx

CodeProject: TinyUDP – Simple UDP Client/Server Components for .NET. Free source code and programming help
http://69.10.233.10/KB/IP/TinyUDP.aspx

C#使用UDP Client编写聊天程序 – lanwilliam的专栏 – CSDNBlog
http://blog.csdn.net/lanwilliam/archive/2008/05/13/2440635.aspx

Use UdpClient : UdpClient « Network « C# / CSharp Tutorial
http://www.java2s.com/Tutorial/CSharp/0580__Network/UseUdpClient.htm

用C#写的聊天程序,贴出所有的源代码,大家帮忙瞧瞧!_软界知道
http://www.softwelt.com/Know/KnowDetail-3168871.html

CodeProject: An Introduction to Socket Programming in .NET using C#. Free source code and programming help
http://www.codeproject.com/KB/IP/socketsincsharp.aspx

C#通讯编程(整理)附代码大家一定要认真看很长的帖子!…
http://www.bcbbs.net/bbs/topic.aspx?topicid=15602

Best Udp Client : Udp Client : C# / C Sharp examples (example source code) Organized by topic \\ Network
http://www.demo2s.com/Code/CSharp/Network/BestUdpClient.htm

CodeProject: UDP Client and Server and Remote BSOD prank too!. Free source code and programming help
http://69.10.233.10/KB/IP/RBSOD.aspx

C# Home : Multithreaded TCP/UDP Client Server
http://www.csharp-home.com/index/tiki-print_article.php?articleId=158&PHPSESSID=dcc25c27f19fd78f2756797fb00a97ba

标签:, , ,
20070509 c# udp - 十月 11, 2007 by yippee

20070509 c# udp
http://www.yippeesoft.com

Option Explicit

Private Sub Command1_Click()
Winsock1.SendData "11111111"

End Sub

Private Sub Form_Load()
Winsock1.Bind 5577
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s
Debug.Print s
End Sub

class udpNxt
   &leftsign;
       Socket sock;
       int udpport = 5566;
       public udpNxt()
       &leftsign;
           IPHostEntry hostentry = Dns.GetHostEntry(Dns.GetHostName());
           IPEndPoint lpoint_udp = new IPEndPoint(IPAddress.Any, udpport);
           sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
           //sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
           //sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000);
           sock.Bind(lpoint_udp);
       &rightsign;
       public int udpNxtSend(byte[] bs,int len)
       &leftsign;
           return 0;
       &rightsign;

       public int udpNxtRecfrom()
       &leftsign;
           IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
           EndPoint senderRemote = (EndPoint)sender;
           byte[] msg = new Byte[256];
           Trace.WriteLine("Waiting to receive datagrams from client…");

           // This call blocks.
           sock.ReceiveFrom(msg,  ref senderRemote);
           Trace.WriteLine(BitConverter.ToString(msg));
           sock.SendTo(System.Text.Encoding.ASCII.GetBytes("OK"), senderRemote);
           sock.Close();
           return 0;
       &rightsign;
   &rightsign;

   http://www.codeproject.com/cs/internet/UDP_Events.asp
   Introduction

I recently needed to notify multiple WinForms clients (+/- 350) on a LAN that a certain event has happened which affects one of them.

There was also eventually a requirement to dynamically find these client applications and communicate with them. I went through various solutions to this problem, but the one which ended up working and scaling the best was using UDP broadcasts.

This article explains the process that led me to this, and the final solution.

bool done = false;
           int listenPort = 5566;
           UdpClient listener = new UdpClient(listenPort);
           IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

           try
           &leftsign;
               while (!done)
               &leftsign;
                   Console.WriteLine("Waiting for broadcast");
                   byte[] bytes = listener.Receive(ref groupEP);

                   Console.WriteLine("Received broadcast from &leftsign;0&rightsign; :\\n &leftsign;1&rightsign;\\n",
                       groupEP.ToString(),
                       Encoding.ASCII.GetString(bytes, 0, bytes.Length));
               &rightsign;

           &rightsign;
           catch (Exception ex)
           &leftsign;
               Console.WriteLine(ex.ToString());
           &rightsign;
           finally
           &leftsign;
               listener.Close();
           &rightsign;

标签:,
20070508 c# udp 资料 - 十月 10, 2007 by yippee

20070508 c# udp 资料
http://www.yippeesoft.com

C#中UDP通信程序Demo
//启动接收数据
private void button1_Click(object sender, System.EventArgs e)
&leftsign;;
if (udpClient!=null)
&leftsign;; 
UdpThread.Abort();
  Thread.Sleep(TimeSpan.FromMilliseconds(500d));
udpClient.Close();
&rightsign;;;
try
&leftsign;; 
udpClient=new UdpClient(int.Parse(textBox1.Text));
UdpThread=new Thread(new ThreadStart(UdpReciveThread));
UdpThread.Start();

&rightsign;;
catch(Exception y)
&leftsign;;
MessageBox.Show(this,y.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
&rightsign;;
&rightsign;;

/// <summary>
/// udp连接
/// </summary>
public UdpClient udpClient;
public Thread UdpThread;
//接收数据线程
void UdpReciveThread()
&leftsign;;
IPEndPoint remoteHost=null;

listBox1.Items.Add("启动…");
while(udpClient!=null && Thread.CurrentThread.ThreadState.Equals(ThreadState.Running))
&leftsign;;
try
&leftsign;;  listBox1.Items.Add("等待连接…");
byte[] buf=udpClient.Receive(ref remoteHost);
string bufs=Encoding.UTF8.GetString(buf);
listBox1.Items.Add(string.format("主机:&leftsign;;0&rightsign;; 端口:&leftsign;;1&rightsign;; 数据报长度:&leftsign;;2&rightsign;; ",remoteHost.Address,remoteHost.Port,buf.Length) );
listBox1.Items.Add(bufs);
&rightsign;;
catch(Exception y)
&leftsign;;
          listBox1.Items.Add(y.Message);
listBox1.Items.Add(y.Source);

&rightsign;;

&rightsign;;
listBox1.Items.Add("结束…");
&rightsign;;
//发数据
private void button2_Click(object sender, System.EventArgs e)
&leftsign;;
try
&leftsign;;
// IPHostEntry rHost=Dns.GetHostByName(textBox3.Text);
UdpClient uc=new UdpClient(textBox3.Text,int.Parse(textBox4.Text));
byte[] sendbuf=Encoding.UTF8.GetBytes(textBox2.Text);
uc.Send(sendbuf, sendbuf.Length);

&rightsign;;
catch(Exception y)
&leftsign;;
MessageBox.Show(this,y.Message,"发送失败",MessageBoxButtons.OK,MessageBoxIcon.Hand);
&rightsign;;;
&rightsign;;

&rightsign;;
&rightsign;;

C#

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener
&leftsign;
   private const int listenPort = 11000;
   
   private static void StartListener()
   &leftsign;
       bool done = false;
       
       UdpClient listener = new UdpClient(listenPort);
       IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);

       try
       &leftsign;
           while (!done)
           &leftsign;
               Console.WriteLine("Waiting for broadcast");
               byte[] bytes = listener.Receive( ref groupEP);

               Console.WriteLine("Received broadcast from &leftsign;0&rightsign; :\\n &leftsign;1&rightsign;\\n",
                   groupEP.ToString(),
                   Encoding.ASCII.GetString(bytes,0,bytes.Length));
           &rightsign;
           
       &rightsign;
       catch (Exception e)
       &leftsign;
           Console.WriteLine(e.ToString());
       &rightsign;
       finally
       &leftsign;
           listener.Close();
       &rightsign;
   &rightsign;

   public static int Main()
   &leftsign;
       StartListener();

       return 0;
   &rightsign;
&rightsign;

C#

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
&leftsign;
   static void Main(string[] args)
   &leftsign;
       Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
           ProtocolType.Udp);

       IPAddress broadcast = IPAddress.Parse("192.168.1.255");

       byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);
       IPEndPoint ep = new IPEndPoint(broadcast, 11000);

       s.SendTo(sendbuf, ep);

       Console.WriteLine("Message sent to the broadcast address");
   &rightsign;
&rightsign;

标签:, ,
1229 UDP CAsyncSocket - 六月 1, 2007 by yippee

1229 UDP CAsyncSocket

Sending & Receiving UDP Datagrams with MFC\’s CAsyncSocket C++ class
By .

A simple example to show the the use of the MFC CAsyncSocket class to send and received UDP datagrams

Introduction
The following code is intended to provide a very simple example of how to use Microsoft MFC’s CAsyncSocket class to send and receive UDP datagrams.

I was unable to find a simple example of how to use CAsynSocket to send & receive UDP datagrams, so I created my own.  I thought others might find it useful. The example shown creates two independent send sockets & a single receiver socket, that will receive data from both senders.

Using the code
 To reduce clutter, I have:

included only code relevant to the UDP communications (all socket class code is include, much application code is not).

created separate classes to deal with sending UDP & receiving UDP

The code snippets of my test applications main dialog, <code>UdpTestAppDlg</code> (.cpp & .h), show how to use the simple <code>UdpReceiveSocket</code> & <code>UdpSendSocket</code> classes that I have provide here.  The example code shown creates two independent send sockets & a single receiver socket.  The receive socket will receive datagrams from both senders.

 The rudimentary error handling that is included is sufficient to highlight the commonest socket problems.
———————————————————————————————————————————
// Receiving Socket Class definition
 
#pragma once
#include <afxtempl.h>
 
// UdpReceiveSocket command target
 
class UdpReceiveSocket : public CAsyncSocket
&leftsign;
 void OnReceive(int nErrorCode);
public:
 UdpReceiveSocket();
virtual ~UdpReceiveSocket();
&rightsign;;
// Receiving Socket Class implementation
// UdpReceiveSocket.cpp : implementation file
//
#include"stdafx.h"
#include"UdpTestApp.h"
#include"UdpReceiveSocket.h"
// UdpReceiveSocket
UdpReceiveSocket::UdpReceiveSocket()
&leftsign;
// Just specify input PORT#, local machine is assumed
 BOOL bRet = Create(9122,SOCK_DGRAM,FD_READ);
if (bRet != TRUE)
 &leftsign;
 UINT uErr = GetLastError();
 TCHAR szError[256];
 wsprintf(szError, "Server Receive Socket Create() failed: %d", uErr);
 AfxMessageBox(szError);
 &rightsign;
&rightsign;
UdpReceiveSocket::~UdpReceiveSocket()
&leftsign;
&rightsign;
// UdpReceiveSocket member functions
voidUdpReceiveSocket::OnReceive(int nErrorCode) 
&leftsign;
  staticint i=0;
 
   i++;
 
   TCHAR buff[4096];
  int nRead;
   CString strSendersIp;
   UINT uSendersPort;
 
   // Could use Receive here if you don’t need the senders address&port
   nRead = ReceiveFromEx(buff, 4096, strSendersIp, uSendersPort);
 
  switch (nRead)
   &leftsign;
  case 0:      // Connection was closed.
      Close();    
     break;
  case SOCKET_ERROR:
     if (GetLastError() != WSAEWOULDBLOCK)
      &leftsign;
         AfxMessageBox ("Error occurred");
         Close();
      &rightsign;
     break;
  default:// Normal case: Receive() returned the # of bytes received.
      buff[nRead] = 0;//terminate the string (assuming a string for this example)
      CString strReceivedData(buff);       // This is the input data  
   &rightsign;
   CAsyncSocket::OnReceive(nErrorCode);
&rightsign;
 
// Sending Socket Class definition
#pragmaonce
 
// UdpSendSocket
 
classUdpSendSocket :public CAsyncSocket
&leftsign;
      bool m_bReadyToSend;
public:
       UdpSendSocket();
      virtual ~UdpSendSocket();
      //
      virtualbool Send(constvoid* lpBuf,int nBufLen );
      virtualvoid OnSend(int nErrorCode);
&rightsign;;
————————————————————
// Sending Socket Class implementation
// UdpSendSocket.cpp : implementation file
//
 
#include"stdafx.h"
#include"UdpTestApp.h"
#include"UdpSocket.h"
 
 
// UdpSendSocket
 
UdpSendSocket::UdpSendSocket():m_bReadyToSend(false)
&leftsign;
      // Create a socket for sending
      // **DO NOT SPECIFY DESTINATION ADDRESS HERE**
       BOOL bRet = Create(0,SOCK_DGRAM,FD_WRITE);
      if (bRet != TRUE)
       &leftsign;
             UINT uErr = GetLastError();
             TCHAR szError[256];
             wsprintf(szError, "Send Socket Create() failed: %d", uErr);
             AfxMessageBox(szError);
       &rightsign;
&rightsign;
 
UdpSendSocket::~UdpSendSocket()
&leftsign;
&rightsign;
 
// UdpSendSocket member functions
 
voidUdpSendSocket::OnSend(int nErrorCode)
&leftsign;
   m_bReadyToSend =true;// The socket is now ready to send
     
   CAsyncSocket::OnSend(nErrorCode);
&rightsign;
 
boolUdpSendSocket::Send(constvoid* lpBuf,int nBufLen)
&leftsign;
      if ( ! m_bReadyToSend )
             return(false);
 
       m_bReadyToSend =false;
 
      int dwBytes;
 
       CAsyncSocket *paSocket =this;
       // Specify destination here (IP number obscured – could use computer name instead)
      if ((dwBytes = CAsyncSocket::SendToEx((LPCTSTR)lpBuf,nBufLen,9122,"172.XX.XX.XXX")) == SOCKET_ERROR)
       &leftsign;
             UINT uErr = GetLastError();
            if (uErr != WSAEWOULDBLOCK)
             &leftsign;
                    TCHAR szError[256];
                    wsprintf(szError, "Server Socket failed to send: %d", uErr);
                    AfxMessageBox(szError);
             &rightsign;
            return(false);
       &rightsign;
      return(true);
&rightsign;
// UdpTestAppDlg.cpp

voidCUdpTestAppDlg::SendUdpDatagram()
&leftsign;
 CString strTestMessage = "Hello World";
 
 m_ussSendSocket1.Send(strTestMessage.GetBuffer(),strTestMessage.GetLength()+1);
 m_ussSendSocket2.Send(strTestMessage.GetBuffer(),strTestMessage.GetLength()+1);
 
&rightsign;

/ UdpTestAppDlg.h : header file
//
 
#pragmaonce
#include"UdpReceiveSocket.h"
#include"UdpSendSocket.h"
 
// CUdpTestAppDlg dialog
classCUdpTestAppDlg :public CDialog
&leftsign;

// Implementation
protected:
       HICON m_hIcon;
 
       UdpReceiveSocket       m_ursReceiveSocket;       // Receive socket
       UdpSendSocket             m_ussSendSocket1;      // Just holds Ip&port # for send
       UdpSendSocket             m_ussSendSocket2;      // Just holds Ip&port # for send
 
 
      void SendUdpDatagram();

&rightsign;;//

标签:,

0430 简单 WINSOCK VB VC UDP - 十月 14, 2006 by yippee

0430 简单 WINSOCK VB VC UDP

Option Explicit

Private Sub Command1_Click()
   Winsock1.SendData "11111111111"
End Sub

Private Sub Form_Load()
Winsock2.Bind 10003
 Winsock1.Bind 10002
 Winsock1.RemoteHost = "127.0.0.1"
    Winsock1.RemotePort = 10003
End Sub

Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock2.GetData strData
Debug.Print strData
End Sub

#include <afxsock.h>
CSocket m_SendSocket;
 WORD wVersionRequested;
 WSADATA wsaData;
 wVersionRequested = MAKEWORD( 2, 2 );

 if (WSAStartup( wVersionRequested, &wsaData ))
 &leftsign;
  return ;
 &rightsign;
 BOOL b=m_SendSocket.Create(10001,SOCK_DGRAM);//创建一个套接字句柄(UDP)
 DWORD dw=GetLastError();
 int i=m_SendSocket.SendTo( "123456",6,10003,"127.0.0.1");//发送数据给本地计算机

CSocket m_ReceiveSocket;
 char *buff1[256];

         //接收数据
 m_ReceiveSocket.Create(10001,SOCK_DGRAM);
 m_ReceiveSocket.Receive(buff1,256);

Class CSocket derives from CAsyncSocket and inherits its encapsulation of the Windows Sockets API. A CSocket object represents a higher level of abstraction of the Windows Sockets API than that of a CAsyncSocket object. CSocket works with classes CSocketFile and CArchive to manage the sending and receiving of data.

A CSocket object also provides blocking, which is essential to the synchronous operation of CArchive. Blocking functions, such as Receive, Send, ReceiveFrom, SendTo, and Accept (all inherited from CAsyncSocket), do not return a WSAEWOULDBLOCK error in CSocket. Instead, these functions wait until the operation completes. Additionally, the original call will terminate with the error WSAEINTR if CancelBlockingCall is called while one of these functions is blocking.

标签:, , , , ,