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

橙三吉。 提交于 2019-11-29 15:49:43

问题


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.


回答1:


You are talking about a simple Server-Client program.

What you need to do.

  • Create a server program and run it first
  • Create a client and connect to your running server using Connect("SERVER IP", PORT)
  • Now when client is connected to server, receive message to server use database conenctions to store that message in database

Guides :

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

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

  • 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

CLIENT-

    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();

            }
        }
    }

SERVER-

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.



来源:https://stackoverflow.com/questions/27539938/tcp-ip-client-socket-program-in-c-net-using-ip-address-and-port-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!