Tôi đã thực hiện rất nhiều googling nhưng không có nhiều may mắn với các vấn đề của tôi. Tôi mới vào mạng lập trình và cố gắng tìm hiểu, tôi đã cố gắng để thiết lập một máy chủ đơn giản & khách hàng mà giao tiếp (sau đây một hướng dẫn trực tuyến nằm ở đây ->http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server)Làm cách nào để khắc phục lỗi "Chỉ sử dụng một địa chỉ của từng địa chỉ cổng (giao thức/địa chỉ/cổng mạng)"?
Vấn đề tôi đang gặp là tôi giữ nhận ngoại lệ "Chỉ sử dụng một địa chỉ của mỗi địa chỉ socket (giao thức/địa chỉ/cổng mạng)" khi cố gắng khởi động TcpListener trên máy chủ.
Tôi đã thử tắt tường lửa, thay đổi cổng để sử dụng, di chuyển các biến xung quanh nhưng không có kết quả (máy khách hoạt động tốt, nhưng rõ ràng là không thể tìm thấy máy chủ vì tôi không thể khởi chạy nó).
Tôi đã nhìn thấy các giải pháp mô tả việc sử dụng Socket.Poll() nhưng vì tôi chỉ sử dụng đối tượng TcpListener, tôi không biết cách sử dụng chức năng Thăm dò ý kiến.
Mã của tôi:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
namespace ServerTutorial {
class Server {
private readonly Thread m_listenThread;
public Server() {
m_listenThread = new Thread(new ThreadStart(ListenForClients));
m_listenThread.Start();
}
public void ListenForClients() {
var listener = new TcpListener(IPAddress.Any, 3000);
listener.Start();
while (true) {
//Blocks until a client has connected to the server
TcpClient client = listener.AcceptTcpClient();
//Send a message to the client
var encoder = new ASCIIEncoding();
NetworkStream clientStream = client.GetStream();
byte[] buffer = encoder.GetBytes("Hello Client!");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
//Create a thread to handle communication with the connected client
var clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(client);
}
}
private void HandleClient(object clientObj) { //Param thread start can only accept object types, hence the cast
var client = (TcpClient) clientObj;
NetworkStream clientStream = client.GetStream();
var message = new byte[4096];
while (true) {
int bytesRead = 0;
try {
//Block until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
} catch {
//A socket error has occurred
System.Diagnostics.Debug.WriteLine("A socket error has occured");
break;
}
if (bytesRead == 0) {
//The client has disconnected from the server
System.Diagnostics.Debug.WriteLine("A client has disconnected from the server");
client.Close();
break;
}
//Message has been received
var encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
}
}
}
}
Trong phương pháp chính của tôi:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerTutorial {
class Program {
static void Main(string[] args) {
var server = new Server();
server.ListenForClients();
}
}
}
Any help is vô cùng đánh giá cao!
Lưu ý trên 'mingw-w64' bạn cần' closesocket() 'và không' close() 'để giải phóng cổng. – Jeroen