最近自己做勒个聊天工具
但是接收到的信息不能显示出来(用工具有截到信息)
希望大家给点意见和建议using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
TcpClient tcpClient = null;
StreamWriter sw = null;
StreamReader sr = null;
Thread thConnection = null;
Socket socket = null;
TcpListener tcpListener = null;
NetworkStream ns = null;
string msg;
StringBuilder sb = new StringBuilder(); public string Msg
{
get { return msg; }
set { msg = value; }
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
tcpListener = new TcpListener(IPAddress.Any,5555);
tcpListener.Start();
thConnection = new Thread(new ThreadStart(WaitingConnection));
thConnection.Start();
} private void WaitingConnection()
{
while(true)
{
socket = tcpListener.AcceptSocket();
if(ns==null)
{
ns = new NetworkStream(socket);
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
}
}
}
//用线程读取信息
private void waitingMsg()
{
//this.textBox1 = new TextBox();
while (true)
{
try
{
this.Msg = sr.ReadToEnd();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
//开启连接
private void btnConnection_Click(object sender, EventArgs e)
{
if(ns==null)
{
tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(this.txtIP.Text),5555);
ns = tcpClient.GetStream();
sw = new StreamWriter(ns);
sr = new StreamReader(ns);
Thread th = new Thread(new ThreadStart(waitingMsg));
th.Start();
timer1.Start();
WaitConn.Start();
}
}
//发送信息
private void btnSend_Click(object sender, EventArgs e)
{
string sendmsg = textBox2.Text;
try
{
sw.Write(sendmsg);
textBox2.Text = null;
sw.Flush();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//定时的读取Msg里面的信息并显示
private void timer1_Tick(object sender, EventArgs e)
{
if(this.Msg!=null)
{
sb.AppendLine(this.Msg);
this.textBox1.Text = sb.ToString();
this.msg = null;
}
}
//保持连接
private void WaitConn_Tick(object sender, EventArgs e)
{
sw.Write(" ");
sw.Flush();
}
}
}
但是接收到的信息不能显示出来(用工具有截到信息)
希望大家给点意见和建议using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
TcpClient tcpClient = null;
StreamWriter sw = null;
StreamReader sr = null;
Thread thConnection = null;
Socket socket = null;
TcpListener tcpListener = null;
NetworkStream ns = null;
string msg;
StringBuilder sb = new StringBuilder(); public string Msg
{
get { return msg; }
set { msg = value; }
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
tcpListener = new TcpListener(IPAddress.Any,5555);
tcpListener.Start();
thConnection = new Thread(new ThreadStart(WaitingConnection));
thConnection.Start();
} private void WaitingConnection()
{
while(true)
{
socket = tcpListener.AcceptSocket();
if(ns==null)
{
ns = new NetworkStream(socket);
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
}
}
}
//用线程读取信息
private void waitingMsg()
{
//this.textBox1 = new TextBox();
while (true)
{
try
{
this.Msg = sr.ReadToEnd();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
//开启连接
private void btnConnection_Click(object sender, EventArgs e)
{
if(ns==null)
{
tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(this.txtIP.Text),5555);
ns = tcpClient.GetStream();
sw = new StreamWriter(ns);
sr = new StreamReader(ns);
Thread th = new Thread(new ThreadStart(waitingMsg));
th.Start();
timer1.Start();
WaitConn.Start();
}
}
//发送信息
private void btnSend_Click(object sender, EventArgs e)
{
string sendmsg = textBox2.Text;
try
{
sw.Write(sendmsg);
textBox2.Text = null;
sw.Flush();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//定时的读取Msg里面的信息并显示
private void timer1_Tick(object sender, EventArgs e)
{
if(this.Msg!=null)
{
sb.AppendLine(this.Msg);
this.textBox1.Text = sb.ToString();
this.msg = null;
}
}
//保持连接
private void WaitConn_Tick(object sender, EventArgs e)
{
sw.Write(" ");
sw.Flush();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货