Server and client novice

Don't talk too much, just go to the script.
The first is the server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Sever
{
    class Program
    {
        static byte[] buffer = new byte[1024];  
        static void Main(string[] args)
        {
            Socket severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint severpoint = new IPEndPoint(ip,88);
            severSocket.Bind(severpoint);
            severSocket.Listen(10);
            severSocket.BeginAccept(accept,severSocket);
            
            Console.ReadKey();
            
        }
        static void accept(IAsyncResult ar)
        {
            Socket severSocket = ar.AsyncState as Socket;
            Socket client = severSocket.EndAccept(ar);
           //Sending data
            string str = "hello";
            client.Send(Encoding.UTF8.GetBytes(str));
            //Receive 
            client.BeginReceive (buffer, 0, 1024, SocketFlags.None, receive, client);
            severSocket.BeginAccept(accept, severSocket);
        }
        static void receive(IAsyncResult ar)
        {
            Socket client = null;
            try {
                client = ar.AsyncState as Socket;
                int count = client.EndReceive(ar);
                if (count == 0)
                {
                    //Normal closure
                    client.Close();return;
                }
                string str = Encoding.UTF8.GetString(buffer, 0, count);
                Console.WriteLine(str);
                client.BeginReceive(buffer, 0, 1024, SocketFlags.None, receive, client);
            }
            catch(Exception e)//Abnormal shutdown
            {
                Console.WriteLine(e);
                if (client != null)
                {
                    client.Close();
                }
            }
           

        }
        
    }
}

Both the server and the client are loaded asynchronously. Of course, a thread can be opened, but it doesn't matter.

The second is for the client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint point = new IPEndPoint(ip, 88);
            client.Connect(point);
            //Receive
            byte[] buffer = new byte[1024];
            int count = client.Receive(buffer);
            Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, count));
            //Send out
            while (true)
            {
                string s = Console.ReadLine();
                if (s=="c")
                {
                    //Active closure
                    client.Close();
                    return;
                }
                client.Send(Encoding.UTF8.GetBytes(s));
            }
            Console.ReadKey();
            client.Close();
            
        }
    }
}

This is just a simple connection. Multiple clients can connect to the server

Published 6 original articles, won praise 0, visited 35
Private letter follow

Keywords: socket encoding

Added by triffik on Sat, 22 Feb 2020 18:16:49 +0200