Net中使用IP地址和端口号的TCP

Net中使用IP地址和端口号的TCP

本文介绍了C#.Net中使用IP地址和端口号的TCP / IP客户端套接字程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

TCP/IP Client Socket Program. Here My Main Requirement is Client Send Message and server receive message and store in database table in C#.Net, Using Server IP Address and Port Number.

推荐答案

您正在谈论一个简单的Server-Client程序。

You are talking about a simple Server-Client program.

您需要做什么。


  • 创建服务器程序并首先运行它

  • 创建客户端并连接到正在运行的服务器现在,使用 Connect( SERVER IP,PORT)

  • 现在,当客户端连接到服务器时,使用数据库连接将消息存储到服务器时,使用服务器将消息存储到数据库

指南:


  • 写服务器-

写客户端-

C#数据库访问[SQL]-

C# database access [SQL] -http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm

更新-根据要求并作为指导,这里是工作中的客户端和服务器

UPDATE - As requested and as a guidance here is a working client and a server

客户端-

    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++;
            }
        }
    }

}

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

Run server first. Then run client.

这篇关于C#.Net中使用IP地址和端口号的TCP / IP客户端套接字程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:59