你这样写试试,我是VS2005调试通过了的。
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;
using System.Collections;
namespace TCPtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string RFileName = "ChatRecord.Txt";
public void Wfile(string filename, string filecont)
{
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter wfile = new StreamWriter(fs);
wfile.BaseStream.Seek(0, SeekOrigin.End);
wfile.WriteLine(filecont);
wfile.Flush();
wfile.Close();
}//写纪录
private void CloseWin_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void ViewText_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(RFileName);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private TcpListener Ilistener;
private Thread Iprocessor;
private Socket Iclientsocket2;
private Thread Iclientservice;
private void ExitBtn_Click(object sender, EventArgs e)
{
if (Iprocessor.IsAlive) Iprocessor.Abort();
Ilistener.Stop();
RevStr.Text = "[" + System.DateTime.Now + "]:终止服务\n";
OpenBtn.Enabled = true;//启动键有效
ExitBtn.Enabled = false;//停止键无效
ViewText.Enabled = false;//纪录有效
SendBtn.Enabled = false;//发送有效
}
private void OpenBtn_Click(object sender, EventArgs e)
{
Iprocessor = new Thread(new ThreadStart(IStartListening));
Iprocessor.Start();
RevStr.Text = "[" + System.DateTime.Now + "]:启动服务\n";
if (File.Exists(RFileName)) File.Delete(RFileName);
Wfile(RFileName, RevStr.Text);
OpenBtn.Enabled = false;//启动键无效
ExitBtn.Enabled = true;//停止键有效
ViewText.Enabled = true;//纪录有效
SendBtn.Enabled = true;//发送有效
}
private NetworkStream ns;
private TcpClient clientsocket;
private UserIP=new ((()).AddressList[0].Address);
private void SendBtn_Click(object sender, EventArgs e)
{
int ToPortStr=6886;
if(PortStr.Text.ToString().Trim()!="")ToPortStr=int.Parse(PortStr.Text.ToString());
string ToIpStr=IPStr.Text;
string ToCont=SendStr.Text.ToString();
string ToContentStr=UserIP.ToString()+ToCont;
SendStr.Text="";
try
{
clientsocket = new TcpClient(ToIpStr,ToPortStr);
clientsocket.SendTimeout=1000;
clientsocket.ReceiveTimeout=1000;
clientsocket.ReceiveBufferSize=256;
ns = clientsocket.GetStream();
//
Byte[] outbytes = System.Text.Encoding.Unicode.GetBytes(ToContentStr.ToCharArray());
int WriteLength=(int)clientsocket.ReceiveBufferSize;
if(outbytes.Length<WriteLength)WriteLength=outbytes.Length;
ns.Write(outbytes,0,outbytes.Length);
RevStr.Text = "["+System.DateTime.Now+"]发:"+ToContentStr;
Wfile(RFileName,RevStr.Text);
clientsocket.Close();
}
catch(Exception ex)
{
if(ex!=null)MessageBox.Show("无法连接对方机器或者未启动服务","Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}//发送
private void IServiceClient()
{
bool Ikeepalive=true;
int RCharNum=256;
string ReceiveMsg ="";
if ((Iclientsocket2.Connected)&&(Ikeepalive))
{
try
{
Byte[] buffer = new Byte[RCharNum];
Iclientsocket2.Receive(buffer,0,RCharNum,SocketFlags.None);
string Iclientcommand = System.Text.Encoding.Unicode.GetString(buffer);
ReceiveMsg = Iclientcommand;
}
catch(Exception e)
{
if(e!=null)MessageBox.Show("传输出错"+e.ToString());
Ikeepalive=false;
}
}
RevStr.Text = "["+System.DateTime.Now+"]\n收:"+ReceiveMsg;
Wfile(RFileName,RevStr.Text);
Iclientsocket2.Shutdown(SocketShutdown.Both);
Iclientsocket2.Close();
}//接收
private void IStartListening()
{
int PortNumber = 8998;
if (PortStr.Text.ToString().Trim() != "") PortNumber = int.Parse(PortStr.Text.ToString());
Ilistener = new TcpListener(PortNumber);
try
{
Ilistener.Start();
}
catch (Exception e)
{
if (e != null)
{
MessageBox.Show("无法打开端口", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
ExitBtn_Click(null, null);
}
}
while (Iprocessor.IsAlive)
{
try
{
Iclientsocket2 = Ilistener.AcceptSocket();
Iclientservice = new Thread(new ThreadStart(IServiceClient));
Iclientservice.Start();
}
catch (Exception e)
{
if (e != null)
{
MessageBox.Show("无法建立端口服务,服务终止", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (Iclientservice.IsAlive) Iclientservice.Abort();
break;
}
}
}
}
private void RevStr_TextChanged(object sender, EventArgs e)
{
}
}
}