我厭倦了弄清楚我的代碼出了什麼問題......請幫助我在這裏!TCP連接失敗
我想在局域網中的服務器客戶端之間創建一個TCP連接。發生的情況是,客戶端嘗試連接時程序崩潰。 請參閱代碼:
public void Go()
{
if (whatjob == true)
{
IPEndPoint server_ipEndPoint = new IPEndPoint(IPAddress.Any, PortNum);
server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server_socket.Bind(server_ipEndPoint);
try
{
server_socket.Listen(1);
server_GotClient = server_socket.Accept();
}
catch (SocketException e)
{
MessageBox.Show("Unable to listen...");
MessageBox.Show(e.ToString());
}
reading = new Thread(new ThreadStart(this.get_msg));
reading.Start();
}
if (whatjob == false)
{
IPEndPoint client_ipEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), PortNum);
client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
client_socket.Blocking = true;
client_socket.Connect(client_ipEndPoint); //here it stops execution
//client_socket.BeginConnect(client_ipEndPoint, new AsyncCallback(NowConnected), client_socket);
}
catch (SocketException e)
{
MessageBox.Show("Unable to connect...");
MessageBox.Show(e.ToString());
return;
}
reading = new Thread(new ThreadStart(this.get_msg));
reading.Start();
}
whatjob是布爾......真手段做服務器的工作和虛假手段做客戶端的工作
此功能是Windows內部形成了聊天窗口。當到達直到client_socket.connect(ipendpoint),它崩潰,甚至聊天窗口deformated ...
螺紋功能
public void get_msg()
{
byte[] byte_message = new byte[1000];
string string_message = null;
int x = 0;
while (true)
{
if (server_GotClient != null)
{
x = server_GotClient.Receive(byte_message);
}
if (client_socket != null)
{
x = client_socket.Receive(byte_message);
}
if (x != 0)
{
string_message = Encoding.ASCII.GetString(byte_message);
this.richTextBox_GetMessage.Invoke(new MethodInvoker(delegate
{
richTextBox_GetMessage.Text = richTextBox_GetMessage.Text + "\nFriend: " + string_message;
}));
}
x = 0;
}
server_socket.Close();
}
對此代碼請幫助。 要進一步補充,我沒有異步技術的知識,用一些簡單的方法幫助我。
什麼是例外? –