我有c#server-client app..client将它的两个id发送到server.It工作正常一次。但是如果同一个客户端想要发送另一个请求..它不起作用。 因为如果我删除'tcpclient.close'命令,我的服务器执行将暂停。 这是代码..
服务器
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.Sockets;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;
namespace newserver
{
public partial class Form1 : Form
{
Socket sck;
// EndPoint epLocal, epRemote;
IPAddress ipAdress;
TcpListener myList;
string serverIP;
private PerformanceCounter cpuCounter;
int cpuLoad;
public Form1()
{
InitializeComponent();
InitialiseCPUCounter();
timer1.Start();
serverIP = "127.0.0.1";
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
ipAdress = IPAddress.Parse(serverIP);
myList = new TcpListener(ipAdress, Convert.ToInt32("8000"));
myList.Start();
byte[] bytes = new byte[2560];
string data = "";
//sck = myList.AcceptSocket();
TcpClient client = myList.AcceptTcpClient();
btnStart.Text = "Connected";
lblCAFrom.Visible = true;
IPEndPoint ipep = (IPEndPoint)client.Client.RemoteEndPoint;
IPAddress ipa = ipep.Address;
txtCIP.Text = ipa.ToString();
NetworkStream stream = client.GetStream();
int i;
i = stream.Read(bytes, 0, bytes.Length);
while (i != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
i = stream.Read(bytes, 0, bytes.Length);
}
string[] arr = data.Split(new char[] { ';' });
int length = arr.Length;
int no_of_req = length / 2;
// ..............server register....................//
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ayne\Documents\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(constr);
con.Open();
int j = 0;
while (j < no_of_req)
{
string insert_register = "INSERT INTO Register(IP,IMPU,IMPI) Values('" + ipa.ToString() + "','" + arr[0] + "','" + arr[1] + "')";
SqlCommand cmd = new SqlCommand(insert_register, con);
cmd.ExecuteNonQuery();
j++;
}
con.Close();
txtCIP.Visible = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
cpuLoad = Convert.ToInt32(cpuCounter.NextValue());
this.txtCPUusage.Text =
cpuLoad.ToString() +
"%";
}
}
}
客户端:
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.Sockets;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;
namespace newserver
{
public partial class Form1 : Form
{
Socket sck;
// EndPoint epLocal, epRemote;
IPAddress ipAdress;
TcpListener myList;
string serverIP;
private PerformanceCounter cpuCounter;
int cpuLoad;
public Form1()
{
InitializeComponent();
InitialiseCPUCounter();
timer1.Start();
serverIP = "127.0.0.1";
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
ipAdress = IPAddress.Parse(serverIP);
myList = new TcpListener(ipAdress, Convert.ToInt32("8000"));
myList.Start();
byte[] bytes = new byte[2560];
string data = "";
//sck = myList.AcceptSocket();
TcpClient client = myList.AcceptTcpClient();
btnStart.Text = "Connected";
lblCAFrom.Visible = true;
IPEndPoint ipep = (IPEndPoint)client.Client.RemoteEndPoint;
IPAddress ipa = ipep.Address;
txtCIP.Text = ipa.ToString();
NetworkStream stream = client.GetStream();
int i;
i = stream.Read(bytes, 0, bytes.Length);
while (i != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
i = stream.Read(bytes, 0, bytes.Length);
}
string[] arr = data.Split(new char[] { ';' });
int length = arr.Length;
int no_of_req = length / 2;
// ..............server register....................//
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ayne\Documents\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(constr);
con.Open();
int j = 0;
while (j < no_of_req)
{
string insert_register = "INSERT INTO Register(IP,IMPU,IMPI) Values('" + ipa.ToString() + "','" + arr[0] + "','" + arr[1] + "')";
SqlCommand cmd = new SqlCommand(insert_register, con);
cmd.ExecuteNonQuery();
j++;
}
con.Close();
txtCIP.Visible = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
cpuLoad = Convert.ToInt32(cpuCounter.NextValue());
this.txtCPUusage.Text =
cpuLoad.ToString() +
"%";
}
}
}
答案 0 :(得分:0)
今天我问自己这个问题, 我来到YouTube video,很好地解释了它。
将此代码转换为表单应用程序应该不会太难。
主要技巧似乎是对传入连接进行多线程处理 并且使用asynchronous socket而不是TcpListener(TcpListener可能更容易)
我希望这有助于