簡體   English   中英

C#.Net中使用IP地址和端口號的TCP/IP客戶端套接字程序

[英]TCP/IP Client Socket Program in C#.Net Using IP Address And Port Number

TCP/IP 客戶端套接字程序。 這里我的主要要求是客戶端發送消息和服務器接收消息並存儲在 C#.Net 的數據庫表中,使用服務器 IP 地址和端口號。

您正在談論一個簡單的服務器 - 客戶端程序。

你需要做什么。

  • 創建一個服務器程序並先運行它
  • 創建客戶端並使用Connect("SERVER IP", PORT)連接到正在運行的服務器
  • 現在,當客戶端連接到服務器時,接收到服務器的消息並使用數據庫連接將該消息存儲在數據庫中

指南:

更新 - 根據要求和指導,這里是一個工作客戶端和一個服務器

客戶-

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    
    
    namespace socket_prog
    {
        class Client
        {
            private static void Main(String[] args)
            {
                byte[] data = new byte[10];
    
                IPHostEntry iphostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress ipAdress = iphostInfo.AddressList[0];
                IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, 32000);
    
                Socket client = new Socket(ipAdress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    
                try
                {
                    client.Connect(ipEndpoint);
    
                    Console.WriteLine("Socket created to {0}", client.RemoteEndPoint.ToString());
    
                    byte[] sendmsg = Encoding.ASCII.GetBytes("This is from Client\n");
    
                    int n = client.Send(sendmsg);
    
                    int m = client.Receive(data);
    
                    Console.WriteLine("" + Encoding.ASCII.GetString(data));
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
    
                Console.WriteLine("Transmission end.");
                Console.ReadKey();
    
            }
        }
    }

服務器-

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace socket_prog
{
    class Server
    {
        static void Main(string[] args)
        {
            byte[] buffer = new byte[1000];
            byte[] msg = Encoding.ASCII.GetBytes("From server\n");
            string data = null;

            IPHostEntry iphostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = iphostInfo.AddressList[0];
            IPEndPoint localEndpoint = new IPEndPoint(ipAddress, 32000);

            ConsoleKeyInfo key;
            int count = 0;

            Socket sock = new Socket(ipAddress.AddressFamily,
                SocketType.Stream, ProtocolType.Tcp);


            sock.Bind(localEndpoint);
            sock.Listen(5);

            while (true)
            {

                Console.WriteLine("\nWaiting for clients..{0}", count);
                Socket confd = sock.Accept();

                int b = confd.Receive(buffer);
                data += Encoding.ASCII.GetString(buffer, 0, b);

                Console.WriteLine("" + data);
                data = null;

                confd.Send(msg);

                Console.WriteLine("\n<< Continue 'y' , Exit 'e'>>");
                key = Console.ReadKey();
                if (key.KeyChar == 'e')
                {
                    Console.WriteLine("\nExiting..Handled {0} clients", count);
                    confd.Close();
                    System.Threading.Thread.Sleep(5000);
                    break;
                }
                confd.Close();
                count++;
            }
        }
    }

}

先運行服務器。 然后運行客戶端。

您可以從下面提到的鏈接https://www.c-sharpcorner.com/article/socket-programming-in-C-Sharp/獲得幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM