完整的udp客户端

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace BestUdpClient
{
    
class Program
    
{
        
private static byte[] data = new byte[1024];
        
private static IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9050);
        
private static EndPoint remote = (EndPoint)sender;
        
private static int size = 30;
        
private static int AdvSndRcvData(Socket s, byte[] message, EndPoint rmtdevice)
        
{
            
int recv = 0;
            
int retry = 0;
            
while (true)
            
{
                Console.WriteLine(
"Attemp #{0}", retry);
                
try
                
{
                    s.SendTo(message, message.Length, SocketFlags.None, rmtdevice);
                    data 
= new byte[size];
                    recv 
= s.ReceiveFrom(data, ref remote);
                }

                
catch (SocketException e)
                
{
                    
if (e.ErrorCode == 10054)
                        recv 
= 0;
                    
else if (e.ErrorCode == 10040)
                    
{
                        Console.WriteLine(
"Error receiving packet");
                        size 
+= 10;
                        recv 
= 0;
                    }

                }

                
if (recv > 0)
                    
return recv;
                
else
                
{
                    retry
++;
                    
if (retry > 4)
                    
{
                        
return 0;
                    }

                }

            }

        }

        
static void Main(string[] args)
        
{
            
string input, stringdata;
            
int recv;
            IPEndPoint ipep 
= new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
            Socket server 
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            
int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine(
"Default timeout:{0}", sockopt);
            server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 
3000);
            sockopt 
= (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine(
"New timeout:{0}", sockopt);
            
string welcome = "Hello,are you there";
            data 
= Encoding.ASCII.GetBytes(welcome);
            recv 
= AdvSndRcvData(server,data, ipep);
            
if (recv > 0)
            
{
                stringdata 
= Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(stringdata);
            }

            
else
            
{
                Console.WriteLine(
"Unable to communicate with remote host");
                
return;
            }

            
while (true)
            
{
                input 
= Console.ReadLine();
                
if (input == "exit")
                
{
                    
break;
                }

                recv 
= AdvSndRcvData(server, Encoding.ASCII.GetBytes(input), ipep);
                
if (recv > 0)
                
{
                    stringdata 
= Encoding.ASCII.GetString(data, 0, recv);
                    Console.WriteLine(stringdata);
                }

                
else
                    Console.WriteLine(
"Did not receive an answer");
            }

            Console.WriteLine(
"Stopping  client");
            server.Close();
        }

    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值