基于C/S架构风格的软件体系结构,进行系统设计,并编程实现C/S风格的软件系统。测试,完成所需功能。
系统介绍:基于C/S风格体系结构,开发经典软件系统“HELLO WORLD”。在客户端向服务器端发送“HELLO WORLD”,服务器端接收到信息后,显示出来,并向客户端发送“ACCEPT”,客户端显示返回信息。
使用c#winform窗体实现
服务端:
在ip地址中随便输入一个合法地址即可
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 服务端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
Dictionary<string, Socket> clientList = new Dictionary<string, Socket>();
private void button1_Click(object sender, EventArgs e)
{
Thread myServer = new Thread(MySocket);
myServer.IsBackground = true;
myServer.Start();
}
void MySocket()
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPAddress iP = IPAddress.Parse(textBox1.Text);
IPEndPoint endPoint = new IPEndPoint(iP, int.Parse(textBox2.Text));
try
{
server.Bind(endPoint);
}
catch (Exception)
{
MessageBox.Show("ip地址无效", "提示");
}
server.Listen(20);
listBox1.Items.Add("服务器已经成功开启!");
while (true)
{
Socket connectClient = server.Accept();
if (connectClient != null)
{
string infor = connectClient.RemoteEndPoint.ToString();
clientList.Add(infor, connectClient);
listBox1.Items.Add(infor + "加入服务器!");
string msg = infor + "已成功连接服务器!";
SendMsg(msg);
Thread threadClient = new Thread(ReciveMsg);
threadClient.IsBackground = true;
threadClient.Start(connectClient);
}
}
}
void ReciveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
byte[] arrMsg = new byte[1024 * 1024];
int length = client.Receive(arrMsg);
if (length > 0)
{
string recMsg = Encoding.UTF8.GetString(arrMsg, 0, length);
IPEndPoint endPoint = client.RemoteEndPoint as IPEndPoint;
listBox1.Items.Add(DateTime.Now + "[" + endPoint.Port.ToString() + "]:" + recMsg);
SendMsg("[" + endPoint.Port.ToString() + "]:" + recMsg);
}
}
catch (Exception)
{
client.Close();
clientList.Remove(client.RemoteEndPoint.ToString());
}
}
}
private void label1_Click(object sender, EventArgs e)
{
string ip = IPAddress.Any.ToString();
textBox1.Text = ip;
}
void SendMsg(string str)
{
foreach (var item in clientList)
{
byte[] arrMsg = Encoding.UTF8.GetBytes(str);
item.Value.Send(arrMsg);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
SendMsg(textBox3.Text);
listBox1.Items.Add(DateTime.Now+textBox3.Text);
textBox3.Text = "";
}
}
}
}
客户端:
ip地址中输入服务端地址 在同一台计算机上则输入个人ip地址
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
Socket client;
private void button1_Click(object sender, EventArgs e)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPAddress ip = IPAddress.Parse(textBox1.Text);
IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(textBox2.Text));
try
{
client.Connect(endPoint);
}
catch(Exception)
{
MessageBox.Show("连接失败", "提示");
}
Thread thread = new Thread(ReciveMsg);
thread.IsBackground = true;
thread.Start(client);
}
void ReciveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
byte[] arrList = new byte[1024 * 1024];
int length = client.Receive(arrList);
string msg = DateTime.Now + ": " +Encoding.UTF8.GetString(arrList, 0, length);
listBox1.Items.Add(msg);
}
catch (Exception)
{
client.Close();
}
}
}
void SendMsg(string str)
{
byte[] arrMsg = Encoding.UTF8.GetBytes(str);
client.Send(arrMsg);
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
SendMsg(textBox3.Text);
textBox3.Text = "";
}
}
}
}