在学习WinForm的TCP编程,在写这个小程序的时候出现很多问题,找了好久都没找到解决方法。目前问题如下:
1、程序分为客户端窗口和服务器窗口。
在客户端窗口输入 信息 并点击 “发送”按钮之后必须马上点击服务器的“启动”按钮,才能接收到信息,否则,就会出现 服务器停止工作的提示
2、就算 在客户端点击“发送”按钮之后马上点击服务器的“启动”按钮,服务器也会停止工作。
初步判断是 网上所说的没有循环监听。
服务器端的代码如下: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;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace Server_TCP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public TcpListener tcplistener;
Thread trd;
private void button1_Click(object sender, EventArgs e)
{
trd = new Thread(new ThreadStart(connect));
trd.IsBackground = true;
trd.Start();
}
private void connect()
{ IPAddress ipAd = IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString());
tcplistener = new TcpListener(ipAd, 8500);
while (true)
{
tcplistener.Start();
Byte[] bytes = new Byte[256];
String data = null;
TcpClient client = tcplistener.AcceptTcpClient();
listBox1.Items.Add("Connected!");
NetworkStream stream = client.GetStream();
int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
listBox1.Items.Add("Received: "+data); data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); stream.Write(msg, 0, msg.Length);
listBox1.Items.Add("Sent: "+ data); }
}
}
}
}描述:
Stopped working问题签名:
问题事件名称: CLR20r3
问题签名 01: server_tcp.exe
问题签名 02: 1.0.0.0
问题签名 03: 509cded4
问题签名 04: System
问题签名 05: 2.0.0.0
问题签名 06: 4ef6cc3f
问题签名 07: 291b
问题签名 08: eb
问题签名 09: System.IO.IOException
OS 版本: 6.1.7600.2.0.0.256.1
区域设置 ID: 2052
1、程序分为客户端窗口和服务器窗口。
在客户端窗口输入 信息 并点击 “发送”按钮之后必须马上点击服务器的“启动”按钮,才能接收到信息,否则,就会出现 服务器停止工作的提示
2、就算 在客户端点击“发送”按钮之后马上点击服务器的“启动”按钮,服务器也会停止工作。
初步判断是 网上所说的没有循环监听。
服务器端的代码如下: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;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace Server_TCP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public TcpListener tcplistener;
Thread trd;
private void button1_Click(object sender, EventArgs e)
{
trd = new Thread(new ThreadStart(connect));
trd.IsBackground = true;
trd.Start();
}
private void connect()
{ IPAddress ipAd = IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString());
tcplistener = new TcpListener(ipAd, 8500);
while (true)
{
tcplistener.Start();
Byte[] bytes = new Byte[256];
String data = null;
TcpClient client = tcplistener.AcceptTcpClient();
listBox1.Items.Add("Connected!");
NetworkStream stream = client.GetStream();
int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
listBox1.Items.Add("Received: "+data); data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); stream.Write(msg, 0, msg.Length);
listBox1.Items.Add("Sent: "+ data); }
}
}
}
}描述:
Stopped working问题签名:
问题事件名称: CLR20r3
问题签名 01: server_tcp.exe
问题签名 02: 1.0.0.0
问题签名 03: 509cded4
问题签名 04: System
问题签名 05: 2.0.0.0
问题签名 06: 4ef6cc3f
问题签名 07: 291b
问题签名 08: eb
问题签名 09: System.IO.IOException
OS 版本: 6.1.7600.2.0.0.256.1
区域设置 ID: 2052
tcplistener = new TcpListener(ipAd, 8500);
tcplistener.Start();
Byte[] bytes = new Byte[256];
String data = "";
while (true)
{ data = "";
TcpClient client = tcplistener.AcceptTcpClient();
this.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("Conected!"); }));
NetworkStream stream = client.GetStream();
int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
this.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("Received: " + data); }));
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
this.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("Sent: " + data); }));
}
你的方法可以诶!
网上很多人说什么Invoke,因为刚接触,对Invoke没了解。
你这个可以用。我自己也另外写了一个:
namespace Server_TCP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public TcpListener tcplistener;
Thread trd;
static int port = 8500;
private void button1_Click(object sender, EventArgs e)
{
trd = new Thread(new ThreadStart(connect));
trd.IsBackground = true;
trd.Start();
}
private void connect()
{ IPAddress ipAd = IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString());
while (true)
{
tcplistener = new TcpListener(ipAd, port);
tcplistener.Start();
Byte[] bytes = new Byte[2560];
String data = null;
TcpClient client = tcplistener.AcceptTcpClient();
listBox1.Items.Add("Connected!");
getstring(client, bytes, data);
client.Close();
tcplistener.Stop();
}
}
private void getstring(TcpClient client, Byte[] bytes, String data)
{
NetworkStream stream = client.GetStream();
int i;
if ((i = stream.Read(bytes, 0, bytes.Length)) != 0)//修改了if!!!!
{
data = System.Text.Encoding.Default.GetString(bytes, 0, i);
listBox1.Items.Add("Received: " + data); data = data.ToUpper(); byte[] msg = System.Text.Encoding.Default.GetBytes(data);
listBox1.Items.Add("Sent: " + data);
stream.Close();
}
}
}
}