From an higher level, the architecture of my application is this:
public class NetworkService
{
// Other things
private UdpClient receiving_client_;
private UdpClient sending_client_;
private Thread login_thread_;
private Thread receiving_thread_;
public NetworkService(
int Port,
string MyNickname)
{
this.port_ = Port;
this.my_nickname_ = MyNickname;
receiving_client_ = new UdpClient(port_);
receiving_thread_ = new Thread(Receiver);
receiving_thread_.IsBackground = true;
receiving_thread_.Start();
}
// Other things
private void Receiver()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port_);
while (true)
{
byte[] byte_data = receiving_client_.Receive(ref endPoint);
HandlePacket(data);
}
}
}
From the other side, I have 2 forms: Launcher and Lobby.
Inside Launcher I initialize a NetworkService and I use to contact another IP, when it answers an event is raised and Lobby form is created. Until here everything works fine: I receive and send all the packets correctly.
How I create the other form:
Lobby lobby = new Lobby(ref network_service_, other_peer_ip_);
lobby.ShowDialog();
public Lobby(ref NetworkService NetworkService, string OtherPeerIp)
{
Application.EnableVisualStyles();
InitializeComponent();
network_service_ = NetworkService;
this.my_nickname_ = network_service_.my_nickname_;
network_service_.OnChatMessageReceived += NewChatMessageReceived;
network_service_.OnConnectionEstabilished += ConnectionEstabilished;
}
The problem is that once the new form is loaded, i don't receive packets anymore! I send them, I checked with Wireshark. It seems that the receiving thread don't do anything, but I also checked that it was alive and it is.
I wonder if the problem is caused by the creation of the new form and the passing of NetworkService to it.
Aucun commentaire:
Enregistrer un commentaire