Socket 通信 .net 例子 客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace MySocket
{
    public class SocketClient
    {


        public SocketClient() { }

        /// <summary>
        /// Socket 通信客户端
        /// </summary>
        /// <param name="ip">服务器的IP</param>
        /// <param name="port">服务器端口</param>
        /// <param name="sendMsg">发送的消息</param>
        public void StartClient(string ip, int port, string sendMsg)
        {
            // Data buffer for incoming data.  
            byte[] bytes = new byte[2048];

            // Connect to a remote device.  
            try
            {
                // Establish the remote endpoint for the socket.
                IPAddress ipa = IPAddress.Parse(ip);
                IPEndPoint ipe = new IPEndPoint(ipa, port);
                // Create a TCP/IP  socket.  
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Console.WriteLine("====================MySocket=====================准备连接服务器");

                // Connect the socket to the remote endpoint. Catch any errors.  
                try
                {
                    sender.Connect(ipe);
                    Console.WriteLine("====================MySocket=====================Socket connected to {0}", sender.RemoteEndPoint.ToString());
                    // Encode the data string into a byte array.
                    byte[] msg = Encoding.Default.GetBytes(sendMsg);
                    
                    // Send the data through the socket.
                    int bytesSent = sender.Send(msg);
                    // Receive the response from the remote device.
                    int bytesRec = sender.Receive(bytes); //接收远程服务器信息
                    string reMsg = Encoding.Default.GetString(bytes, 0, bytesRec);
                    Console.WriteLine("====================MySocket=====================Echoed test = {0}", reMsg);

                    // Release the socket.  
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("====================MySocket.Exception=====================ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("====================MySocket.Exception=====================SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("====================MySocket.Exception=====================Unexpected exception : {0}", e.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("====================MySocket.Exception=====================" + e.ToString());
            }


        }



    }
}

 

你可能感兴趣的:(.net,socket)