http://zhidao.baidu.com/question/337247508.html?fr=im100009
这个有什么问题啊
高手帮看看
具体如下:namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public static IPAddress localaddr = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddr ,8888); private void button1_Click(object sender, EventArgs e)
{
listener.Start();
textBox1.Text = "服务器启动";
// NetworkStream stream = new NetworkStream(sockets); clientconnect();
} private void clientconnect()
{
while (true)
{
textBox1.Text = "等待客户连接!";
try
{
TcpClient newclient = listener.AcceptTcpClient();
}
catch { break; }
textBox1.Text = "有客户连接了!";
// Console.WriteLine("有客户连接了!");
}
}
}
}
就想自己一步步编一个 结果电启动按钮后 就程序不能相应了 单步调试 显示能执行到监听啊 咋回事求高手指点 帮帮我这小菜鸟吧
小弟不胜感激!!!
这个有什么问题啊
高手帮看看
具体如下:namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public static IPAddress localaddr = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddr ,8888); private void button1_Click(object sender, EventArgs e)
{
listener.Start();
textBox1.Text = "服务器启动";
// NetworkStream stream = new NetworkStream(sockets); clientconnect();
} private void clientconnect()
{
while (true)
{
textBox1.Text = "等待客户连接!";
try
{
TcpClient newclient = listener.AcceptTcpClient();
}
catch { break; }
textBox1.Text = "有客户连接了!";
// Console.WriteLine("有客户连接了!");
}
}
}
}
就想自己一步步编一个 结果电启动按钮后 就程序不能相应了 单步调试 显示能执行到监听啊 咋回事求高手指点 帮帮我这小菜鸟吧
小弟不胜感激!!!
程序 你的程序没有跑完 当然死在那边了啊 1·!~ 你要启动监听的时候,启动一个线程,当连接断开的时候 线程消亡 ,这样就不会死机了
{
if (IsServer)
this.sockUDP1.Listen(3211);
else
this.sockUDP1.CloseSock();
}
public void Listen(int Port)
{
try
{
UDP_Server_Port = Port;
UDP_Server = new UdpClient(Port);
// UDP_Server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Udp) ; thdUdp = new Thread(new ThreadStart(GetUDPData) );
thdUdp.Start();
}
catch (Exception e)
{
if (Sock_Error!= null)
Sock_Error(e.ToString());
}
}
using System.IO;
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.Threading;namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
TcpClient newclient;
Thread nth;
Boolean star = false;
public static IPAddress localaddr = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localaddr ,8888); private void button1_Click(object sender, EventArgs e)
{
listener.Start();
textBox1.Text = "服务器启动";
nth = new Thread(new ThreadStart (clientconnect ));
nth.Start();
} private void clientconnect()
{
while (true)
{ try
{
newclient = listener.AcceptTcpClient();
textBox1.Text = "有客户连接了!";
}
catch { break; } // Console.WriteLine("有客户连接了!"); NetworkStream ns = newclient.GetStream();
BinaryReader nsr = new BinaryReader(ns);
BinaryWriter nsw = new BinaryWriter(ns);
star = true;
while (star)
{
textBox1.Text = nsr.ReadString();
}
nsr.Close();
nsw.Close();
ns.Close();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
star = false;
listener.Stop();
//newclient.Close();
}
}
}
为什么 只能连一个客户端啊?
改成TcpClient newclient = TcpClient newclient
就能连多个了
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.Threading;
namespace _0111109
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint MyServer;
private Socket sock;
private bool check=true;
private Socket accSock;
public Form1()
{
InitializeComponent();
}
private void accp()
{
MyServer=new IPEndPoint(myIP,Int32.Parse(textBox2.Text));
sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
MessageBox.Show("成功开监听");
// textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听.....\r\n");
while (check)
{
accSock = sock.Accept();
if (accSock.Connected) //
{
textBox3.AppendText("与客户建立连接,");
Thread thread = new Thread(new ThreadStart(round));
thread.Start();
byte[] Rec=new byte[64];
NetworkStream netStream=new NetworkStream(accSock);
netStream.Read(Rec,0,Rec.Length);
string RecMessage=
System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage+"\r\n");
}
} }
private void round()
{
while (true)
{
byte[] Rec = new Byte[64];
NetworkStream netStream = new NetworkStream(accSock);
netStream.Read(Rec, 0, Rec.Length);
String RecMessage =
System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage + "\r\n");
}
}
private void Form1_Load(object sender, EventArgs e)
{ } private void textBox3_TextChanged(object sender, EventArgs e)
{ } private void label1_Click(object sender, EventArgs e)
{ } private void richTextBox1_TextChanged(object sender, EventArgs e)
{ } private void label4_Click(object sender, EventArgs e)
{ } private void label5_Click(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{ } private void button3_Click(object sender, EventArgs e)
{
try
{sock.Close();
textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"监听停止!\r\n");
}
catch{MessageBox.Show("监听尚未开始,关闭无效!");} } private void button1_Click_1(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch { MessageBox.Show("您输入的IP地址格式下正确,请重新输入!"); }
try
{
Thread thread = new Thread(new ThreadStart(accp));
thread.Start();
}
catch (Exception ee) { textBox3.AppendText(ee.Message); }
} private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] sendByte = new byte[64];
String send = richTextBox2.Text + "\r\n";
NetworkStream netStream = new NetworkStream(accSock);
sendByte = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
netStream.Write(sendByte, 0, sendByte.Length);
// netStream.Flush();
}
catch { MessageBox.Show("连接尚未建立!无法发送!!!"); }
}
}
}
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.Threading;
namespace _01111092
{
public partial class Form1 : Form // partial
{
private IPEndPoint MyServer;
private Socket connectSock;
private bool check = true;
private IPAddress myIP = IPAddress.Parse("127.0.0.1");
public Form1()
{
InitializeComponent();
}
private void receive()
{
while (true)
{
byte[] Rec = new Byte[64];
NetworkStream netStream = new NetworkStream(connectSock);
netStream.Read(Rec, 0, Rec.Length);
String RecMessage =
System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage + "\r\n");
}
}
private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch { MessageBox.Show("输入的IP地址格式不正确,请重新输入!"); }
try
{
MyServer=new IPEndPoint(myIP,Int32.Parse(textBox2.Text));
connectSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectSock.Connect(MyServer);
textBox3.AppendText("与主机" + textBox1.Text + "端口" + textBox2.Text + "连接成功!\r\n");
Thread thread=new Thread(new ThreadStart(receive));
thread.Start();
}
catch(Exception ee){MessageBox.Show(ee.Message);}
} private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] sendByte = new byte[64];
String send = richTextBox2.Text + "\r\n";
NetworkStream netStream = new NetworkStream(connectSock);
sendByte = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
netStream.Write(sendByte, 0, sendByte.Length);
netStream.Flush();
}
catch { MessageBox.Show("连接尚未建立!无法发送!!!"); }
} private void button3_Click(object sender, EventArgs e)
{
try
{
connectSock.Close();
// this.Close();
textBox3.AppendText("与主机" + textBox1.Text + "端口" + textBox2.Text + "断开连接!\r\n");
}
catch { MessageBox.Show("连接尚未建立,断开无效"); }
}
}
}