Socket通信Net例クライアント

3592 ワード

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


        }



    }
}