首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#.Net中使用IP地址和端口号的TCP/IP客户端Socket程序

C#.Net中使用IP地址和端口号的TCP/IP客户端Socket程序
EN

Stack Overflow用户
提问于 2014-12-18 13:43:12
回答 2查看 38.8K关注 0票数 2

TCP/IP客户端套接字程序。这里我的主要需求是客户端发送报文,服务器接收报文,并使用服务器IP地址和端口号将报文存储在C#.Net中的数据库表中。

EN

回答 2

Stack Overflow用户

发布于 2014-12-18 14:07:44

您正在谈论的是一个简单的服务器-客户端程序。

你需要做的是。

  • 创建服务器程序并首先运行它
  • 创建客户端并使用连接(“服务器IP",当客户端连接到服务器时为PORT)
  • Now )连接到正在运行的服务器,接收发送到服务器的消息并使用数据库连接将该消息存储在数据库

指南:

http://csharp.net-informations.com/communications/csharp-server-socket.htm

  • 写入服务器

http://csharp.net-informations.com/communications/csharp-client-socket.htm

  • 写入客户端

SQL - http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm

  • C#数据库访问

UPDATE -根据请求并作为指导,这里提供了一个工作客户端和一个服务器

客户端-

代码语言:javascript
运行
复制
    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();
    
            }
        }
    }

服务器-

代码语言:javascript
运行
复制
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++;
            }
        }
    }

}

首先运行服务器。然后运行客户端。

票数 7
EN

Stack Overflow用户

发布于 2021-02-12 03:46:21

您可以从下面提到的链接https://www.c-sharpcorner.com/article/socket-programming-in-C-Sharp/中获得帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27539938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档