Socket 通信 .net 例子 客户端_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Socket 通信 .net 例子 客户端

Socket 通信 .net 例子 客户端

 2015/1/27 15:52:34  davidforit  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;namespaceMySocket{publicclassSocketClient{publicSocketClient(){}///<summary>///Socket通信客户端///</summary>///<
  • 标签:.net net 例子 客户 客户端 socket
class="c#" name="code">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());
            }


        }



    }
}

?

发表评论
用户名: 匿名