• C-Sharp网络编程案例解析(Socket类的使用)


    Server端:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net;

    using System.Net.Sockets;

    namespace Server

    {    

    class Program    

    {        

    static void Main(string[] args)        

    {            

    //用于表示客户端发送的信息的长度            

    int recvLenth;            

    //用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组            

    byte[] data=new byte[1024];            

    //本机预使用的IP和端口            

    IPEndPoint iend = new IPEndPoint(IPAddress.Any,9999);            

    Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            

    newsock.Bind(iend);            

    newsock.Listen(10);            

    Console.WriteLine("waiting for a client !");            

    Socket client = newsock.Accept();            

    //当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信            

    IPEndPoint clientAdd = (IPEndPoint)client.RemoteEndPoint;//获取远程终结点            

    Console.WriteLine("连接:"+clientAdd.Address+"端口:"+clientAdd.Port);//获取或设置终结点端口号            

    string welcome = "welcome here !";            

    data = Encoding.ASCII.GetBytes(welcome);            

    client.Send(data,data.Length,SocketFlags.None);//指定套接字的发送和接收行为            

    while(true)            

    {                

    data=new byte[1024];                

    recvLenth = client.Receive(data);                

    if(recvLenth==0)//信息长度为0,说明客户端连接断开                

    {                    

    break;                

    }                

    Console.WriteLine(Encoding.ASCII.GetString(data,0,recvLenth));                

    client.Send(data,recvLenth,SocketFlags.None);            

    }            

    Console.WriteLine("断开连接"+clientAdd.Address);            

    client.Close();            

    newsock.Close();        

    }    

    }

    }

    Client端:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net.Sockets;

    using System.Net;

    namespace Client

    {    

    class Program    

    {        

    static void Main(string[] args)        

    {            

    byte[] data=new byte[1024];            

    Socket newclient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            

    Console.WriteLine("输入服务器地址:");            

    string ipadd = Console.ReadLine();            

    Console.WriteLine();            

    Console.Write("输入服务器端口:");            

    int port = Convert.ToInt32(Console.ReadLine());            

    IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd),port);//服务器的ip和端口            

    try            

    {                

    //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的ip和端口,也不需要监听                

    newclient.Connect(ie);            

    }            

    catch(SocketException ex)            

    {                

    Console.WriteLine("不能连接");                

    Console.WriteLine(ex.ToString());                

    return;            

    }            

    int recv = newclient.Receive(data);            

    string stringdata = Encoding.ASCII.GetString(data,0,recv);            

    Console.WriteLine(stringdata);            

    while(true)            

    {                

    string input = Console.ReadLine();                

    if(input=="exit")                

    {                    

    break;                

    }                

    newclient.Send(Encoding.Default.GetBytes(input));                

    data=new byte[1024];                

    recv = newclient.Receive(data);                

    stringdata = Encoding.Default.GetString(data,0,recv);                

    Console.WriteLine(stringdata);            

    }            

    Console.WriteLine("断开连接");            

    newclient.Shutdown(SocketShutdown.Both);            

    newclient.Close();        

    }    

    }

    }

  • 相关阅读:
    [YNOI2017][bzoj4811][luogu3613] 由乃的OJ/睡觉困难综合症 [压位+树链剖分+线段树]
    [bzoj3270] 博物馆 [期望+高斯消元]
    [bzoj4372] 烁烁的游戏 [动态点分治+线段树+容斥原理]
    [Codeforces438E][bzoj3625] 小朋友和二叉树 [多项式求逆+多项式开根]
    [bzoj3813] 奇数国 [线段树+欧拉函数]
    [BZOJ4205][FJ2015集训] 卡牌配对 [建图+最大流]
    Git常见问题解决办法
    电脑常用快捷键
    egret.Shape渲染集合图形
    TypeScript语法学习--变量的声明
  • 原文地址:https://www.cnblogs.com/Jasxu/p/Socket.html
Copyright © 2020-2023  润新知