c# socket(server and client)

来源:互联网 发布:cat linux命令 编辑:程序博客网 时间:2024/05/22 08:04

server代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace Server
{
    public partial class Form1 : Form
    {
        Thread startThread;
        Socket socket;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            startThread = new Thread(new ParameterizedThreadStart(start));
            startThread.Start();
        }
        private void start(object TransMsg)
        {
            int port = 1212;
            System.Net.IPAddress ipAdress = Dns.GetHostEntry("localhost").AddressList[0];
            IPEndPoint hostEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(hostEP);
            while (true)
            {
                socket.Listen(50);
                Socket NewSocket = socket.Accept();

                byte[] bytesSendStr = new byte[1024];
                string sendStr = "";
                int bytes = NewSocket.Receive(bytesSendStr);
                sendStr += Encoding.ASCII.GetString(bytesSendStr, 0, bytes);
                bytesSendStr = Encoding.ASCII.GetBytes(sendStr);
                try
                {
                    NewSocket.Send(bytesSendStr, bytesSendStr.Length, 0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                NewSocket.Shutdown(SocketShutdown.Both);
                NewSocket.Close();
            }
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            socket.Close();
            startThread.Abort();
        }
    }
}

 

client代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace Client
{
    public partial class Form1 : Form
    {
        string kk = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            int port = 1212;
            System.Net.IPAddress ipAdress = Dns.GetHostEntry("localhost").AddressList[0];
            IPEndPoint hostEP = new IPEndPoint(ipAdress, port);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(hostEP);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            string sendStr = DateTime.Now.ToString() + "...";
            //创建bytes字节数组以转换发送串
            byte[] bytesSendStr = new byte[1024];
            //将发送内容字符串转换成字节byte数组
            bytesSendStr = Encoding.ASCII.GetBytes(sendStr);
            try
            {
                //向主机发送请求
                socket.SendTo(bytesSendStr, 0, hostEP);
            }
            catch (Exception ce)
            {
                MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
            }
            string recStr = "";
            byte[] recByte = new byte[1024];
            int bytes = 0;

            try
            {
                bytes = socket.Receive(recByte, recByte.Length, 0);
                recStr += Encoding.ASCII.GetString(recByte, 0, bytes);

                kk = kk + recStr;
                txtShow.Text = kk;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
    }
}

经过一天的了解,自己写出一段简单的有管socket的代码,

希望可以对刚刚开始认识socket的程序员有所帮助,有什么问题可以给我留言!

原创粉丝点击