Socket communication connection in C #

Server

  • 1. Create a Socket
  • 2, call the Bind Bind IP address and port number
  • 3, call Listen to wait for client connections
  • 4, call Accept to accept client connections
  • 5, in response to the message and print While in a message sent by the client
using System;
using System.Net;
using System.Net.Sockets;

namespace Serv
{
    class Program
    {
        const int BUFFER_SIZE = 1024;
        static byte[] readBuff = new byte[BUFFER_SIZE];
        static void Main(string[] args)
        {
            Console.Write("Hello World!!!\n");
            //Socket
            Socket listened = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ipAdr = IPAddress.Parse("127.0.0.1");
            IPEndPoint ipEp = new IPEndPoint(ipAdr, 1234);
            listened.Bind(ipEp);
            //listen
            listened.Listen(0);
            Console.WriteLine("Server is starting");

            //Accept
            Socket confd = listened.Accept();//阻塞
            Console.WriteLine("server Accept");
            while (true)
            {
                
                
                int count = confd.Receive(readBuff);
                String str = System.Text.Encoding.Default.GetString(readBuff, 0, count);
                Console.WriteLine(str);
                str = "Server:Hello Heiren!!!";
                byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
                confd.Send(bytes);

            }

            
        }
    }
}

Client

  • 1. Create a Socket
  • 2, call the Connect server connection
  • 3, the transmission information and the print-receiving messages while in the
  • 4, when the console input exit, exit the loop, close the connection
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static Socket socket;
        const int BUFFER_SIZE = 1024;
        static byte[] readBuff = new byte[BUFFER_SIZE];
        public static void Main(string[] args)
        {
            Console.Write("Client Hello World!!!\n");
            socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            socket.Connect("127.0.0.1",1234);
            int num = 0;
            while(true)
            {
                String str = "Client:Hello Heiren!!!";
                if (num == 0) num++;
                else str = Console.ReadLine();
                if (str.Equals("exit"))
                    break;
                byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
                socket.Send(bytes);
                int count = socket.Receive(readBuff);
                str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
                Console.WriteLine(str);
                
            }
          
            socket.Close();

        }
    }
}

Published 12 original articles · won praise 0 · Views 61

Guess you like

Origin blog.csdn.net/qq_33670157/article/details/104510983