实现一对多的网络通信先收藏了。
//客户端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace TcpClient2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
//if (New_TB.Text.Trim().Length > 0)
//{
// TcpClient TC = new TcpClient(IP_TB.Text,int.Parse(Point_TB.Text));
// NetworkStream NetStr = TC.GetStream();
// NetStr.Write(Encoding.UTF8.GetBytes(New_TB.Text), 0, Encoding.UTF8.GetBytes(New_TB.Text).Length);
// writestring("客户端说:" + New_TB.Text);
// New_TB.Clear();
// Byte[] newdate = new Byte[256];
// int len = NetStr.Read(newdate, 0, newdate.Length);
// writestring("服务端说:" + Encoding.UTF8.GetString(newdate));
// NetStr.Close();
// TC.Close();
//}
if (New_TB.Text.Trim().Length > 0)
{
TcpClient TC = new TcpClient(IP_TB.Text, int.Parse(Point_TB.Text));
byte[] data = Encoding.UTF8.GetBytes(New_TB.Text);
NetworkStream NetStr = TC.GetStream();
NetStr.Write(data, 0, data.Length);
writestring("客户端说:" + New_TB.Text);
New_TB.Clear();
Byte[] newdate = new Byte[256];
int len = NetStr.Read(newdate, 0, newdate.Length);
writestring("服务端说:" + Encoding.UTF8.GetString(newdate));
NetStr.Close();
TC.Close();
}
}
void writestring(string s)
{
Old_TB.Text += DateTime.Now.ToString("yy/MM/dd hh:mm:ss") + s;
Old_TB.Text += "/r/n";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
}
}
//服务器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace TCPClient1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread ServerTh = new Thread(ServerListener);
ServerTh.Start();
}
//void ServerListener()
//{
// IPAddress Ip = IPAddress.Parse(IP_TB.Text);
// TcpListener server = new TcpListener(Ip,int.Parse(Point_TB.Text));
// server.Start();
// Byte[] bytes=new Byte[256];
// String data = null;
// while (true)
// {
// TcpClient client = server.AcceptTcpClient();
// NetworkStream NetStr = client.GetStream();
// int i = 0;
// while ((i = NetStr.Read(bytes, 0, bytes.Length)) != 0)
// {
// data = System.Text.Encoding.UTF8.GetString(bytes);
// writestring("客户端说:" + data);
// data = New_TB.Text;
// byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
// NetStr.Write(msg, 0, msg.Length);
// writestring("服务端说:" + data);
// }
// }
//}
List<TcpClient> list = new List<TcpClient>();
void ServerListener()
{
IPAddress IP = IPAddress.Parse(IP_TB.Text);
TcpListener server = new TcpListener(IP, int.Parse(Point_TB.Text));
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream NetStr = client.GetStream();
int i = 0;
while ((i = NetStr.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.UTF8.GetString(bytes);
writestring("客户端说:" + data);
data = New_TB.Text;
byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
NetStr.Write(msg, 0, msg.Length);
writestring("服务端说:" + data); ;
}
}
}
void writestring(string s)
{
Old_TB.Text += DateTime.Now.ToString("yy/MM/dd hh:mm:ss") + s;
Old_TB.Text += "/r/n";
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
}
}