这个是客户端,代码没有注释,有不明白的地方我们交流交流。
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Collections.Generic;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.ComponentModel;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Data;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Drawing;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Linq;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Text;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Windows.Forms;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Net;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Net.Sockets;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETusing System.Threading;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKETnamespace TCPClient
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET{
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET        public partial class Form1 : Form
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public Socket newclient;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public bool Connected;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public Thread myThread;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public delegate void MyInvoke(string str);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public Form1()
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        InitializeComponent();
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public void Connect()
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        byte[] data = new byte[1024];
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        string ipadd = serverIP.Text.Trim();
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        int port = Convert.ToInt32(serverPort.Text.Trim());
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        try
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                newclient.Connect(ie);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                connect.Enabled = false;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                Connected = true;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        catch(SocketException e)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                MessageBox.Show("连接服务器失败    "+e.Message);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                return;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        ThreadStart myThreaddelegate = new ThreadStart(ReceiveMsg);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        myThread = new Thread(myThreaddelegate);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        myThread.Start();
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public void ReceiveMsg()
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        while (true)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                byte[] data = new byte[1024];
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                int recv = newclient.Receive(data);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                string stringdata = Encoding.UTF8.GetString(data, 0, recv);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                showMsg(stringdata + "\r\n");
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                //receiveMsg.AppendText(stringdata + "\r\n");
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                public void showMsg(string msg)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        //在线程里以安全方式调用控件
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        if (receiveMsg.InvokeRequired)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                MyInvoke _myinvoke = new MyInvoke(showMsg);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                receiveMsg.Invoke(_myinvoke, new object[] { msg });
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        else
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                                receiveMsg.AppendText(msg);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                private void SendMsg_Click(object sender, EventArgs e)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        int m_length = mymessage.Text.Length;
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        byte[] data=new byte[m_length];
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        data = Encoding.UTF8.GetBytes(mymessage.Text);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        int i = newclient.Send(data);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        showMsg("我说:" + mymessage.Text + "\r\n");
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        //receiveMsg.AppendText("我说:"+mymessage.Text + "\r\n");
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        mymessage.Text = "";
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        //newclient.Shutdown(SocketShutdown.Both);
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                private void connect_Click(object sender, EventArgs e)
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                {
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                        Connect();
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET                
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET        }
c# WINFORM SOCKET编程-简单聊天程序(客户端)_SOCKET}