Socket TCP CS

348 篇文章 0 订阅
317 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            string host = "127.0.0.1";

            ///创建终结点(EndPoint)
            IPAddress ip = IPAddress.Parse(host);//把ip地址字符串转换为IPAddress类型的实例
            IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例

            ///创建socket并开始监听
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字
            s.Bind(ipe);//绑定EndPoint对像(2000端口和ip地址)
            s.Listen(0);//开始监听
            Console.WriteLine("等待客户端连接");
            while (true)
            {
                ///接受到client连接,为此连接建立新的socket,并接受信息
                Socket temp = s.Accept();//为新建连接创建新的socket
                new Thread(new ThreadStart(delegate
                {
                    String strServer = temp.LocalEndPoint.ToString();
                    String strClient = temp.RemoteEndPoint.ToString();
                    Console.WriteLine("建立连接:{0}<={1}", strServer, strClient);
                    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();
                })).Start();

            }
            s.Close();
            Console.ReadLine();
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int port = 2000;
                string host = "127.0.0.1";
                ///创建终结点EndPoint
                IPAddress ip = IPAddress.Parse(host);
                //IPAddress ipp = new IPAddress("127.0.0.1");
                IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndpoint实例

                ///创建socket并连接到服务器
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
                ///
                bool a = false;
                if (!a)
                {
                    port = 2002;
                    host = "127.0.0.1";
                    ///创建终结点(EndPoint)
                    ip = IPAddress.Parse(host);//把ip地址字符串转换为IPAddress类型的实例
                    IPEndPoint ipe_client = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例          
                    c.Bind(ipe_client);//绑定EndPoint对像(2001端口和ip地址)
                }
                //
                Console.WriteLine("Conneting…");
                c.Connect(ipe);//连接到服务器

                ///向服务器发送信息
                string sendStr = "hello!This is a socket test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串编码为字节
                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);//显示服务器返回信息
                ///一定记着用完socket后要关闭
                c.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("argumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}", e);
            }
            Console.WriteLine("Press Enter to Exit");
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值