⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commonmethod.cs

📁 C常用算法程序集,一部比较经典的程序算法合集
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace mychat1
{
    public class commonmethod
    {
        public static string serverip = "192.168.1.2";
        public static int port = 9050;
        public static string myip = null;
        public static int myport = 0;
        public static void sendtoserver(string str)
        {
            TcpClient server;
            try
            {
               server = new TcpClient(serverip, port);
                byte[] info = Encoding.Unicode.GetBytes(str);
                server.Client.Send(info);
            }
            catch (SocketException)
            {
                throw new Exception("unable to connect to server!");

            }
            server.Close();
        }
        public static void sendtoserver(string str, out string myownip)
        {
            TcpClient server;
            try
            {
                server = new TcpClient(serverip, port);
                myownip = server.Client.LocalEndPoint.ToString();
                string[] mine = myownip.Split(':');
                myip = mine[0];
                myport =Convert .ToInt32 ( mine[1]);
                byte[] info = Encoding.Unicode.GetBytes(str);
                server.Client.Send(info);
            }
            catch (SocketException)
            {
                throw new Exception("unable to connect to server!");
            }

            server.Close();
        }

        public static void sendtoacomputer(string computerip,int computerport,string str)
        {
            byte[] data = new byte[1024];
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(computerip), computerport);
            data = Encoding.Unicode.GetBytes(str);
            server.SendTo(data, data.Length, SocketFlags.None, ipep);
            server.Close();
        }

        public static void sendtosevermessage(string str)//给服务器发信息
        {
            sendtoacomputer(serverip,9053,str);
        }

        public static string receiverfromacomputer(string mycomputerip,int mycomputerport,out string hername)
        {
            string stringdata = null;
            int recv = 0;
            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mycomputerip), mycomputerport);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            newsock.Bind(ipep);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)sender;
            recv = newsock.ReceiveFrom(data, ref Remote);
            newsock.Close();
            stringdata = Encoding.Unicode.GetString(data, 0, recv);
            string[] hernames = stringdata.Split(' ');
            hername = hernames[0];
            return stringdata;
        }

        public static string receiverfromserver(out string hername)
        {
           return  receiverfromacomputer(myip, myport, out hername);
        }

        public static void getherip(string hername, out string herip, out int herport)//向服务器请求某个人的ip信息
        {
            TcpClient server;
            string[] herinfo = null;
            try
            {
                server = new TcpClient(serverip, port);
                byte[] info = Encoding.Unicode.GetBytes(hername + ":her ip information");
                server.Client.Send(info);
                int recv = server.Client.Receive(info);
                string str1 = Encoding.Unicode.GetString(info, 0, recv);
                herinfo = str1.Split(':');
            }
            catch 
            {
                throw new Exception("unable to connect to server!");
            }
            herip = herinfo[0];
            herport = Convert.ToInt32(herinfo[1]);

        }
             
    }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -