基本信息
源码名称:Socket 服务器和客户端通讯
源码大小:0.08M
文件格式:.zip
开发语言:C#
更新时间:2019-04-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

Socket 服务器和客户端通讯

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
           udp();//可以手动切换tcp/udp代码
            
        }
        /// <summary>
        /// udp不需要建立面向连接的通信(通过广播发送数据)
        /// </summary>
        private static void udp()
        {
            Socket udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);//允许 发送广播
            IPEndPoint iepA = new IPEndPoint(IPAddress.Broadcast, 9095);
            //IPEndPoint iepA = new IPEndPoint(IPAddress.Parse("192.168.2.160"), 5566);
            Console.WriteLine("udp已经建立,请输入您要发送的内容");
            while (true)
            {
                string sss = Console.ReadLine();
                int len = udp.SendTo(System.Text.Encoding.Unicode.GetBytes(sss), iepA);

            }
        }
     
        /// <summary>
        /// tcp需要建立面向连接的通讯,并且需要三次握手.也就是说,每次通信后,tcp都需要关闭,下次关闭前重新建立连接。(不能通过广播发送数据)
        /// </summary>
        private static void tcp()
        {
            Socket newclient;
            while (true)
            {

                Console.WriteLine("请输入您要发送的内容");
                string content = Console.ReadLine();
                using (newclient = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp))
                {
                    byte[] data = new byte[1024];
                    IPEndPoint ie = new System.Net.IPEndPoint(IPAddress.Parse("192.168.1.10"), 9050);//服务器的IP和端口

                    try
                    {
                        //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
                        newclient.Connect(ie);//第一次交互
                        int recv = newclient.Receive(data);//第二次交互
                        //连接服务器成功
                        string stringdata = System.Text.Encoding.Unicode.GetString(data, 0, recv);
                        if (stringdata == "连接服务器成功")
                        {
                            newclient.Send(System.Text.Encoding.Unicode.GetBytes(content));//第三次交互
                            newclient.Shutdown(System.Net.Sockets.SocketShutdown.Send);
                            data = new byte[1024];
                            recv = newclient.Receive(data);
                            string result = System.Text.Encoding.Unicode.GetString(data, 0, recv);
                            newclient.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
                            newclient.Close();
                            Console.WriteLine(result);

                        }
                        else
                        {
                            Console.WriteLine("连接服务器失败");
                        }
                    }
                    catch (System.Net.Sockets.SocketException e)
                    {
                        Console.WriteLine(e.ToString());
                        return;
                    }
                }                
            }
        }

        static byte[] GetBytes(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        //获取内网IP
        static string GetInternalIP()
        {
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }
    }
}