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