客户端

 

 QQ
{
    public partial class Socketclient : Form
    {
        
public Socketclient()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
string str = this.textBox1.Text.ToString();
            BeginSend(str);
        }
        
private void BeginSend(string str)
        {
         
            IPAddress serverIp 
= IPAddress.Parse("127.0.0.1");
            IPEndPoint iep 
= new IPEndPoint(serverIp,8000);
            
byte[] byteMessage;
            Socket socket 
= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            socket.Connect(iep);
            byteMessage 
= Encoding.ASCII.GetBytes(str);
            socket.Send(byteMessage);
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
    }
}

 

服务器端

 

 QQ
{
    public partial class SocketServer : Form
    {
        Thread myThread;
        Socket socket;
        
private delegate void listbox_show(string str);
        listbox_show listboxshow;

        
public SocketServer()
        {
            InitializeComponent();
            listboxshow 
= new listbox_show(show);
        }

        
private IPAddress GetServerIp()
        {
            IPHostEntry ieh 
= Dns.GetHostByName(Dns.GetHostName());
            
return ieh.AddressList[0];
        }
        
private void BeginListen()
        {
            IPAddress ip1 
= GetServerIp();
            IPEndPoint iep 
= new IPEndPoint(ip1, 8000);
            socket 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(iep);
            
byte[] byteMessage=new byte[1000];
            
while (true)
            {
                
try
                {
                    socket.Listen(
5);
                    Socket clientSocket 
= socket.Accept();
                    clientSocket.Receive(byteMessage);
                    
string sTime = DateTime.Now.ToShortDateString();
                    
string msg = sTime + ":" + "Message from:";
                    msg 
+= clientSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
                    
this.listBox1.Invoke(listboxshow,msg);
                }
                
catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        
private void show(string str)
        {
            
this.listBox1.Items.Add(str);
        }
        
private void button1_Click(object sender, EventArgs e)
        {
            
try
            {
                myThread 
= new Thread(new ThreadStart(BeginListen));
                myThread.Start();
                
this.button1.Enabled = false;
            }
            
catch (System.Exception er)
            {
                MessageBox.Show(er.Message,
"完成",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }
        }
       
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-26
  • 2022-01-04
  • 2022-12-23
  • 2021-06-17
  • 2022-01-28
  • 2021-05-19
猜你喜欢
  • 2021-08-08
  • 2021-05-06
  • 2022-12-23
  • 2021-04-13
  • 2021-12-23
  • 2021-11-29
  • 2021-05-27
相关资源
相似解决方案