服务器:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Servers
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
//关闭对文本框的非法线程操作检查
TextBox.CheckForIllegalCrossThreadCalls = false;//还未懂!
}
private void Form1_Load(object sender, EventArgs e)
{
}
Thread threadWatch = null;
Socket socketWatch = null;
private void Serverconn_Click(object sender, EventArgs e)
{
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(txtIP.Text.Trim());
int port = Convert.ToInt32(txtPORT.Text.Trim());
IPEndPoint ipe = new IPEndPoint(ip, port);
socketWatch.Bind(ipe);
socketWatch.Listen(10);
threadWatch = new Thread(Watchconnecting);
threadWatch.IsBackground = true;
threadWatch.Start();
txtMsg.AppendText("开始监听!" + "\r\n");
}
Socket socConnection = null;
private void Watchconnecting()
{
while (true)
{
socConnection = socketWatch.Accept();
txtMsg.AppendText("客户端连接成功!" + "\r\n");
//创建一个通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
Thread thr = new Thread(pts);
thr.IsBackground = true;
thr.Start(socConnection);
}
}
private void ServerRecMsg(object obj)
{
Socket socketServer = obj as Socket;
while (true)
{
byte[] RecvBytes = new byte[1024 * 1024];
int length = socketServer.Receive(RecvBytes);
string RecvStr = Encoding.UTF8.GetString(RecvBytes, 0, length);
txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + RecvStr + "\r\n");
}
}
private void SendMsg_Click(object sender, EventArgs e)
{
ServerSendMsg(txtSend.Text.Trim());
}
private void ServerSendMsg(string SendStr)
{
byte[] SendString = Encoding.UTF8.GetBytes(SendStr);
socConnection.Send(SendString);
txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + SendStr + "\r\n");
}
private void txtSend_TextChanged(object sender, EventArgs e)
{
}
//Enter快捷键,光有代码不行,还需要将KeyDown属性设置一下!
private void txtSend_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ServerSendMsg(txtSend.Text.Trim());
}
}
//获取当前的时间
private DateTime GetTime()
{
DateTime nowTime = new DateTime();
nowTime = DateTime.Now;
return nowTime;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Clients
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
private void Client_Load(object sender, EventArgs e)
{
}
Socket socketClient = null;
Thread threadClient = null;
private void beginListen_Click(object sender, EventArgs e)
{
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(txtIP.Text.Trim());
int port = Convert.ToInt32(txtPORT.Text.Trim());
IPEndPoint ipe = new IPEndPoint(ip, port);
socketClient.Connect(ipe);
threadClient = new Thread(RecvMsg);
threadClient.IsBackground = true;
threadClient.Start();
}
private void RecvMsg()
{
while(true)
{
byte[] arrRecMsg = new byte[1024 * 1024];
int length = socketClient.Receive(arrRecMsg);
string strRecMsg = Encoding.UTF8.GetString(arrRecMsg, 0, length);
txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + strRecMsg + "\r\n");
}
}
private void ClientSendMsg(string sendMsg)
{
byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg);
socketClient.Send(arrClientSendMsg);
txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + sendMsg + "\r\n");
}
private void Send_Click(object sender, EventArgs e)
{
ClientSendMsg(txtCMsg.Text.Trim());
}
private void txtCMsg_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ClientSendMsg(txtCMsg.Text.Trim());
}
}
private DateTime GetTime()
{
DateTime nowTime = new DateTime();
nowTime = DateTime.Now;
return nowTime;
}
}
}