C#TCP聊天应用程序线程
问题描述:
我急需帮助。我基本上已经创建了一个程序(将使用)加密来来回发送消息。加密部分工作正常,但是我对线程相当新颖,而且我不能在我的生活中使我的客户机/服务器应用程序的部分排队。聊天程序是使用TCP的直接IP连接,因此每个主机都是客户端和服务器。我在调试时遇到的问题是,当客户端尝试连接到服务器线程时服务器线程未准备好,或者它已准备好,它不会放弃线程以便客户端连接!我一直在为此工作数小时,这是非常令人沮丧的..我希望有人可以帮助!我已经在下面包含了我的代码。 这是我的代码片段从我的MainForm,它构造了客户端和服务器方面:C#TCP聊天应用程序线程
private void InitializeComponent() {
server = new ServerSide("127.0.0.1",7865);
servThread = new Thread(new ThreadStart(server.begin));
client = new ClientSide("127.0.0.1",7865);
clientThread = new Thread(new ThreadStart(client.begin));
servThread.Start();
clientThread.Start();
//servThread.Join();
//clientThread.Join();
}
这是我的服务器端代码:
public class ServerSide
{
String IpString;
int tcpPort;
bool goodToGo = false;
System.Net.IPAddress ipAddress = null;
public ServerSide(String ip, int tcpPort)
{
IpString = ip;
bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
if (isValidIp == true) // check if the IP is valid, set the bool to true if so
{
goodToGo = true;
}
else
{
goodToGo = false;
}
}
public void begin()
{
try
{
IPAddress ipAd = IPAddress.Parse(IpString);
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, tcpPort);
Socket s = null;
/* Start Listening at the specified port */
while (true)
{
myList.Start();
if (myList.Pending())
{
s = myList.AcceptSocket();
break;
}
}
String toReceive = "";
while (true)
{
byte[] b = new byte[4096];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
toReceive += Convert.ToString((Convert.ToChar(b[i])));
// ASCIIEncoding asen = new ASCIIEncoding();
var form = MainForm.ActiveForm as MainForm;
if (form != null)
{
form.messageReceived(toReceive);
}
toReceive = "";
}
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
}
客户方代码:
public class ClientSide
{
private String IpString;
private int tcpPort;
private TcpClient tcpInt;
private static Stream stm;
private System.Net.IPAddress ipAddress = null;
private bool goodToGo = false;
public ClientSide(String ip, int tcpPort)
{
IpString = ip;
this.tcpPort = tcpPort;
bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
if (isValidIp == true)
{
goodToGo = true;
}
else
{
goodToGo = false;
}
}
public void begin()
{
try
{
tcpInt = new TcpClient();
// Console.WriteLine("Connecting.....");
tcpInt.Connect(IpString, tcpPort);
// use the ipaddress as in the server program
// Console.WriteLine("Connected");
// String str = Console.ReadLine();
stm = tcpInt.GetStream();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
public void sendMessage(String str)
{
// stm = tcpInt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
/** byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));*/
}
}
}
再次..通常发生的是我的客户端收到一个错误,主机主动拒绝连接..但我不知道如何计时。我正在寻找如何让我的服务器侦听TCP连接,但仍然设法去我的另一个线程创建TCP连接发送服务器(我目前正在测试本地主机上)。 谢谢。
答
在ServerSide
类的构造函数中,您忘记初始化tcpPort
成员。由于您忘记了,服务器将尝试侦听端口0,客户端将尝试连接到端口7865,并且会失败。
尝试将此代码添加到ServerSide
类的构造函数中。
this.tcpPort = tcpPort;
至于线程,如果客户端线程在服务器线程开始侦听之前尝试连接,则可能有问题。如果你没有成功,你可以使用一个循环尝试连接多次(比如说10),如果超时(比如说1秒)。这样,你会重试10次,然后放弃。
+1鹰眼! – 2012-04-18 06:56:10
我想我可能在最近意外删除了,for循环是我寻找的解决方案!这部分似乎工作,谢谢! – Ramrod 2012-04-18 12:47:19