socket服务端和客户端通讯简单例子

/*client*/
 public partial class FormClient : Form
    {

        public FormClient()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Client client = new Client(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8500));
            lblMsg.Text = "服务器IP及端口:" + client.socket.RemoteEndPoint.ToString();
            try
            {                
                //发送消息
                client.Send(tbMsg.Text);
                listBox1.Items.Add(string.Format("{0}发送信息:{1}", client.socket.LocalEndPoint, tbMsg.Text));            
                //接收
                string recvMsg = client.Receive();
                listBox1.Items.Add(recvMsg);
                listBox1.Items.Add("");
            }
            finally
            {               
                client.Close();
            }
        }


        public class Client
        {
            private const int buffer = 1024;
            public Socket socket;
            public Client(IPEndPoint iep)
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(iep);
            }
            public string Receive()
            {
                byte[] data = new byte[buffer];
                int recv = socket.Receive(data);
                return Encoding.UTF8.GetString(data, 0, recv);
            }
            public void SendFile(string fileName)
            {
                socket.SendFile(fileName);
            }       
            public void Send(string msg)
            {
                byte[] data = Encoding.UTF8.GetBytes(msg);
                socket.Send(data);
            }
            public void Close()
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
        }

    }





/*server*/

 public partial class FormService : Form
    {
        public FormService()
        {
            InitializeComponent();

            Thread serThread = new Thread(new ThreadStart(Listen));
            serThread.Start();
        }


        private void Listen()
        {
            const int buffer = 10240; 
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(ip, 8500);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(iep);
            byte[] cliData = new byte[buffer];

            socket.Listen(10);
            lblMsg.Text = string.Format("开始在{0}{1}上监听", iep.Address.ToString(), iep.Port.ToString());
            Socket client;
            int recv;

            while (true)
            {
                client = socket.Accept();
                recv = client.Receive(cliData);

                //接收消息
                string cliMsg = Encoding.UTF8.GetString(cliData, 0, recv);
                cliMsg = DateTime.Now.ToShortTimeString() + "," + client.RemoteEndPoint.ToString() + "发来信息:" + cliMsg;
                this.listBox1.Items.Add(cliMsg);
             

                string serMsg = "服务器返回信息:OK.";
                byte[] serByte = Encoding.UTF8.GetBytes(serMsg);
                client.Send(serByte);
            }

        }

        private void FormService_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值