这是我网上找到的服务器和客户端程序,但是发送数据好像不成功,不知道是什么原因??
服务器:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
namespace 服务器x
{
public partial class Form1 : Form
{
int num = 0;
//导入程序中使用到的名字空间
private Socket socketForClient;
private NetworkStream networkStream;
private TcpListener tcpListener;
private StreamWriter streamWriter;
private StreamReader streamReader;
private Thread _thread1;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
num++;
if (num <= 10)
{
this.pictureBox1.BackColor = Color.Red;
}
if ((num > 10) && (num < 20))
{
this.pictureBox1.BackColor = Color.DimGray;
}
if (num == 20)
{
num = 0;
}
}
private void Listen()
{
//创建一个tcpListener对象,此对象主要是对给定端口进行侦听
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"),8000);
//开始侦听
tcpListener.Start();
//返回可以用以处理连接的Socket实例
socketForClient = tcpListener.AcceptSocket();
try
{
//如果返回值是”true”,则产生的套节字已经接受来自远方的连接请求
if (socketForClient.Connected)
{
MessageBox.Show("已经和客户端成功连接!");
while(true)
{
//创建networkStream对象通过网络套节字来接受和发送数据
networkStream = new NetworkStream(socketForClient);
//从当前数据流中读取一行字符,返回值是字符串
streamReader = new StreamReader(networkStream);
string receive= streamReader.ReadLine();
if (receive == "hello!")
{
this.timer1.Enabled = true;
}
streamWriter = new StreamWriter(networkStream);
//string send= "hello!";
//往当前的数据流中写入一行字符串
//streamWriter.WriteLine(send);
//刷新当前数据流中的数据
//streamWriter.Flush();
}
}
}
catch (Exception ey)
{
MessageBox.Show(ey.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("服务已经启动!");
_thread1 = new Thread(new ThreadStart(Listen));
_thread1.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
num++;
if (num <= 10)
{
this.pictureBox2.BackColor = Color.Red;
}
if ((num > 10) && (num < 20))
{
this.pictureBox2.BackColor = Color.DimGray;
}
if (num == 20)
{
num = 0;
}
}
private void Form1_Closing(object sender, e)
{
//关闭线程和流
networkStream.Close();
streamReader.Close();
streamWriter.Close();
_thread1.Abort();
tcpListener.Stop();
socketForClient.Shutdown(SocketShutdown.Both);
socketForClient.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//关闭线程和流
networkStream.Close();
streamReader.Close();
streamWriter.Close();
_thread1.Abort();
tcpListener.Stop();
socketForClient.Shutdown(SocketShutdown.Both);
socketForClient.Close();
}
}
}
客户端:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;
namespace 客户端x
{
public partial class Form1 : Form
{
private NetworkStream networkStream;
private StreamReader streamReader;
private StreamWriter streamWriter;
TcpClient myclient;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
IPAddress localIP = IPAddress.Parse("127.0.0.1");
IPEndPoint local=new IPEndPoint(localIP,8000);
myclient = new TcpClient(local);
myclient.Connect(IPAddress.Parse("127.0.0.1"),8000);
}
catch
{
MessageBox.Show ("Connect error!!" );
return ;
}
//创建networkStream对象通过网络套节字来接受和发送数据
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
MessageBox.Show("可发送数据");
}
private void Form1_Closing(object sender, e)
{
try
{
streamReader.Close();
streamWriter.Close();
networkStream.Close();
myclient.Close();
}
catch { }
}
private void button2_Click(object sender, EventArgs e)
{
try
{
//往当前的数据流中写入一行字符串
streamWriter.WriteLine("hello!");
//刷新当前数据流中的数据
streamWriter.Flush();
}
catch(Exception ee)
{
MessageBox.Show("从服务器端读取数据出现错误,类型为:" + ee.ToString());
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
}
}