static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); TCP t = new TCP();
t.fnListen(); }
} public class TCP
{
private Socket host = null; private Socket soc = null; public TCP()
{
this.host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
IPEndPoint ep = new IPEndPoint(ip, 19080);
host.Bind(ep);
this.fnListen();
} public void fnListen()
{
int i = 0;
host.Listen(20);
while (true)
{ soc = host.Accept();
i += 1;
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(fnREV);
System.Threading.Thread th = new System.Threading.Thread(ts);
th.Name = "thread name =" + i.ToString();
Console.WriteLine(th.Name);
th.Start();
}
} public void fnREV()
{
TCPone one = new TCPone(this.soc);
one.fnStart();
}
} public class TCPone
{
Socket host = null;
public TCPone(Socket soc)
{
host = soc;
} public void fnStart()
{
while (true)
{
try
{
byte[] byt = new byte[50];
this.host.Receive(byt);
string temp = System.Text.Encoding.Default.GetString(byt);
Console.WriteLine(temp + " thread name " + System.Threading.Thread.CurrentThread.Name);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
} } Console.WriteLine(" thread name " + System.Threading.Thread.CurrentThread.Name);
} } static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); TcpClient client = new TcpClient();
client.fnMain(); }
} public class TcpClient
{
public TcpClient()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(fnMain);
System.Threading.Thread th = new System.Threading.Thread(ts);
th.Name = " the thread is " + i;
th.Start();
}
Console.ReadLine();
} public void fnMain()
{
IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
IPEndPoint Host = new IPEndPoint(Address, 19080);
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(Host);
this.fnSend(soc);
} public void fnSend(Socket v_socket)
{
double i = 0;
while (true)
{
try
{
string temp = i.ToString();
i += 1;
//temp = Console.ReadLine();
byte[] byt = System.Text.Encoding.Default.GetBytes("1");
//System.Threading.Thread.Sleep ( System.TimeSpan.FromSeconds (0.001));
Console.WriteLine(i + " " + System.Threading.Thread.CurrentThread.Name); v_socket.Send(byt);
}
catch (Exception ex)
{ System.Windows.Forms.MessageBox.Show(ex.Message + " " + System.Threading.Thread.CurrentThread.Name);
System.Threading.Thread.CurrentThread.Abort();
}
//byte[] byts = new byte[10];
//v_socket.Receive(byts);
//Console.WriteLine(System.Text.Encoding.Default.GetString(byts));
} Console.WriteLine(i + " " + System.Threading.Thread.CurrentThread.Name);
}
}
我看了,SERVER的线程没有关闭。
客户端为什么会这样呢??
是不是C#的SOCKET有问题.
你客户端的 发送 是如何写的,调用的 ?
public void fnMain()
{
IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
IPEndPoint Host = new IPEndPoint(Address, 19080);
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(Host);
this.fnSend(soc);
}
你这里已经连上了 如果再次发 没有关闭连接 重新连接会出错的
后面发送只要fnSend(soc);
TCP t = new TCP();
t.fnListen();
也调用了
而且你在host.Listen(20);
之前就host.Bind()没报错吗?
{
IPAddress Address = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
IPEndPoint Host = new IPEndPoint(Address, 19080);
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(Host);
this.fnSend(soc);
}
-----------------------------------------
您能不能告诉我,您这是在干吗?
服务器与客户机运行在同一台计算机上?