Socket编程二
private Thread threadRead; //创建线程,用以侦听端口号,接受信息
private TcpListener tcpListen;//侦听端口号
private bool blistener = true;
private NetworkStream stream;
private StreamReader read;
private TcpClient client;
private delegate void listbox_show(string str);
listbox_show listboxshow;
public Tcpserver()
{
InitializeComponent();
listboxshow = new listbox_show(show);
}
客服端
Code
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;//使用IPAredd类,IPHostEntry类
namespace QQ
{
public partial class Tcpclient : Form
{
private StreamWriter write;
private NetworkStream stream;
private TcpClient tcpclient;
public Tcpclient()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
tcpclient = new TcpClient(AddressFamily.InterNetwork);
tcpclient.Connect("127.0.0.1",8000);
stream = tcpclient.GetStream();
write = new StreamWriter(stream);
write.WriteLine(this.textBox1.Text);
write.Flush();
}
catch
{
}
}
}
}
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;//使用IPAredd类,IPHostEntry类
namespace QQ
{
public partial class Tcpclient : Form
{
private StreamWriter write;
private NetworkStream stream;
private TcpClient tcpclient;
public Tcpclient()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
tcpclient = new TcpClient(AddressFamily.InterNetwork);
tcpclient.Connect("127.0.0.1",8000);
stream = tcpclient.GetStream();
write = new StreamWriter(stream);
write.WriteLine(this.textBox1.Text);
write.Flush();
}
catch
{
}
}
}
}
服务器端
Code
using System.Net.Sockets;//使用了TcpListen类
using System.Threading;//使用到了线程
using System.IO;//使用到了StreamReader类
using System.Net;
namespace QQ
{
public partial class Tcpserver : Form
{
int port = 8000;//定义侦听端口号
private Thread threadRead; //创建线程,用以侦听端口号,接受信息
private TcpListener tcpListen;//侦听端口号
private bool blistener = true;
private NetworkStream stream;
private StreamReader read;
private TcpClient client;
private delegate void listbox_show(string str);
listbox_show listboxshow;
public Tcpserver()
{
InitializeComponent();
listboxshow = new listbox_show(show);
}
private void Listen()
{
try
{
tcpListen = new TcpListener(8000); //用8000端口来初始化TcpListener实例
tcpListen.Start();//开始监听
client = tcpListen.AcceptTcpClient();//通过tcp连接请求
stream = client.GetStream(); //获取基本的流
read = new StreamReader(stream); //用得到的网络基础数据流来初始化StreamReader实例
while (blistener)
{
string message = read.ReadLine();//从网络基础数据流中读取一行数据
if (message == "STOP")
{
tcpListen.Stop();//关闭侦听
stream.Close();//释放资源
read.Close();
threadRead.Abort();
return;
}
string sTime = DateTime.Now.ToShortDateString();
listBox1.Invoke(listboxshow,sTime + " " + message);
}
}
catch (System.Security.SecurityException)
{
MessageBox.Show("侦听失败!","错误");
}
}
private void show(string str)
{
this.listBox1.Items.Add(str);
}
//开始侦听
private void button1_Click(object sender, EventArgs e)
{
threadRead = new Thread(new ThreadStart(Listen));
threadRead.Start();
this.button1.Enabled = false;
}
}
}
using System.Net.Sockets;//使用了TcpListen类
using System.Threading;//使用到了线程
using System.IO;//使用到了StreamReader类
using System.Net;
namespace QQ
{
public partial class Tcpserver : Form
{
int port = 8000;//定义侦听端口号
private Thread threadRead; //创建线程,用以侦听端口号,接受信息
private TcpListener tcpListen;//侦听端口号
private bool blistener = true;
private NetworkStream stream;
private StreamReader read;
private TcpClient client;
private delegate void listbox_show(string str);
listbox_show listboxshow;
public Tcpserver()
{
InitializeComponent();
listboxshow = new listbox_show(show);
}
private void Listen()
{
try
{
tcpListen = new TcpListener(8000); //用8000端口来初始化TcpListener实例
tcpListen.Start();//开始监听
client = tcpListen.AcceptTcpClient();//通过tcp连接请求
stream = client.GetStream(); //获取基本的流
read = new StreamReader(stream); //用得到的网络基础数据流来初始化StreamReader实例
while (blistener)
{
string message = read.ReadLine();//从网络基础数据流中读取一行数据
if (message == "STOP")
{
tcpListen.Stop();//关闭侦听
stream.Close();//释放资源
read.Close();
threadRead.Abort();
return;
}
string sTime = DateTime.Now.ToShortDateString();
listBox1.Invoke(listboxshow,sTime + " " + message);
}
}
catch (System.Security.SecurityException)
{
MessageBox.Show("侦听失败!","错误");
}
}
private void show(string str)
{
this.listBox1.Items.Add(str);
}
//开始侦听
private void button1_Click(object sender, EventArgs e)
{
threadRead = new Thread(new ThreadStart(Listen));
threadRead.Start();
this.button1.Enabled = false;
}
}
}