c# Sockect 通信

1.Server

using System;

using System.Collections.Generic;

using System.Text;

//添加Socket类

using System.Net;

using System.Net.Sockets;



namespace ScoketServer

{

    class Program

    {

        static void Main(string[] args)

        {



            int port = 8080;

            string host = "17.1.1.78";



            //创建终结点

            IPAddress ip = IPAddress.Parse(host);

            IPEndPoint ipe = new IPEndPoint(ip, port);



            //创建Socket并开始监听



            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket对象,如果用UDP协议,则要用SocketTyype.Dgram类型的套接字

            s.Bind(ipe);    //绑定EndPoint对象(2000端口和ip地址)

            s.Listen(0);    //开始监听

            Console.WriteLine("等待客户端连接");





            //接受到Client连接,为此连接建立新的Socket,并接受消息

            Socket temp = s.Accept();   //为新建立的连接创建新的Socket

            Console.WriteLine("建立连接");

            string recvStr = "";

            byte[] recvBytes = new byte[1024];

            int bytes;

            bytes = temp.Receive(recvBytes, recvBytes.Length, 0); //从客户端接受消息

            recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);



            //给Client端返回信息

            Console.WriteLine("server get message:{0}", recvStr);    //把客户端传来的信息显示出来

            string sendStr = "ok!Client send message successful!";

            byte[] bs = Encoding.ASCII.GetBytes(sendStr);

            temp.Send(bs, bs.Length, 0);  //返回信息给客户端

            temp.Close();

            s.Close();

            Console.ReadLine();

        }

    }

}

2.client

using System;

using System.Collections.Generic;

using System.Text;

//添加Socket类

using System.Net;

using System.Net.Sockets;



namespace SocketClient

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int port = 8080;

                string host = "17.1.1.78";

                //创建中街店EndPoint

                IPAddress ip = IPAddress.Parse(host);

                IPEndPoint ipe = new IPEndPoint(ip, port);  //把ip和端口转化为IPEndPoint的实例



                //创建Socket并连接到服务器

                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建Socket

                Console.WriteLine("Connecting...");

                c.Connect(ipe); //连接到服务器



                //向服务器发送信息

                string endStr = "Hello,this is a socket test";

                byte[] bs = Encoding.ASCII.GetBytes(endStr);  //把字符串编码为字节

                Console.WriteLine("Send message");

                c.Send(bs, bs.Length, 0); //发送信息



                //接受从服务器返回的信息

                string recvStr = "";

                byte[] recvBytes = new byte[1024];

                int bytes;

                bytes = c.Receive(recvBytes, recvBytes.Length, 0);   //从服务器端接收返回信息

                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);

                Console.WriteLine("client get message:{0}", recvStr); //回显服务器的返回信息

                Console.Read();

                //一定要记着用完Socket后要关闭

                c.Close();

            }

            catch (ArgumentException e)

            {

                Console.WriteLine("argumentNullException:{0}", e);

            }

            catch (SocketException e)

            {

                Console.WriteLine("SocketException:{0}",e);

            }

        }

    }

}

 

你可能感兴趣的:(SOC)