.net Socket 通信简单实例(初级入门)

c/s控制台应用程序,Server、Client分别在两个项目中

服务端

.net Socket 通信简单实例(初级入门)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int port = 4000;
                string host = "127.0.0.1";
                IPAddress ipa = IPAddress.Parse(host);//将IP字符串转换为IP地址的实例
                IPEndPoint ipe = new IPEndPoint(ipa, port);//将网络端点表示为ip地址和端口号
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket类
                s.Bind(ipe);//绑定端口号
                s.Listen(0);//开始监听
                Console.WriteLine("wait for connect");
                Socket tmp = s.Accept();//为新连接创建新的socket
                Console.WriteLine("get a connect");
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = tmp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接收信息
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine("get message:{0}", recvStr);//把从客户端传来的信息显示出来
                string sendStr = "Yeah! Client send message sucessful";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                tmp.Send(bs, bs.Length, 0);//返回客户端成功信息
                tmp.Close();
                s.Close();

            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex);

            }
            catch (SocketException ex)
            { Console.WriteLine(ex); }

            Console.WriteLine("press enter to exit");
            Console.ReadLine();
        }
    }
}
.net Socket 通信简单实例(初级入门)

 

客户端

.net Socket 通信简单实例(初级入门)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int port = 4000;
                string host = "127.0.0.1";
                IPAddress ipa = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ipa, port);//把ip和端口转化为ipendpoint实例
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket
                Console.WriteLine("connecting.....");
                s.Connect(ipe);//连接到服务器
                string sendStr = "Hello! Socket Test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                Console.WriteLine("SendMessage");
                s.Send(bs, bs.Length, 0);//发送测试信息
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = s.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接收返回信息
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine("ClientGetMessage:{0}", recvStr);//显示服务器返回信息
                s.Close();
            }
            catch (ArgumentNullException ex)
            { Console.WriteLine(ex); }
            catch (SocketException ex)
            { Console.WriteLine(ex); }
            Console.WriteLine("press enter to exit");
            Console.ReadLine();
        }

    }
}
.net Socket 通信简单实例(初级入门)


效果图 :

.net Socket 通信简单实例(初级入门).net Socket 通信简单实例(初级入门)

注意事项:先运行server,在运行client.(怎么发布大家都知道的)

源码:http://download.csdn.net/detail/long_3160221/6929581

.net Socket 通信简单实例(初级入门)

上一篇:JS中的eval函数


下一篇:Spring.Net AOP实例