나는 요점을 잘랐다. 나는 몇 시간 동안 여기에 붙어있다. Google의 톤과 연구 톤하지만 아직까지는 대답이 없습니다.C# 소켓 (TCP 및 UDP)
완벽하게 작동하는 TCP 용 클라이언트 및 서버 코드가 있지만 클라이언트가 서버 위치와 같이 중요하지 않은 패킷에 대해서도 UDP를 사용할 수있게하려고합니다.
지금 현재 연결 클라이언트의 연결 코드입니다.
public void ConnectToServer(){
tcp_client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
tcp_client.Connect(server_ip, server_port);
tcp_stream = new NetworkStream(this.tcp_client);
this.udp_client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.udp_client.BeginConnect(IPAddress.Parse(server_ip), server_port,new AsyncCallback(udp_connected), null);
}
이제 클라이언트는 내가 udp.Send(byteArray)
를 사용할 때 그것은 예외를 던지는 아니에요하지만, 서버 자체가 수신 데이터에 응답하지 않습니다로 보내는 것으로 나타납니다에 대한 문제를 가지고 한 일이 아니다.
100 % 복사/붙여 넣기 코드가 아닙니다. 무엇이 문제인지 보여주기 위해 변경하십시오.
private Socket c;
private UdpClient udp;
private isRunning = true;
public Client(Socket c){
// This was accepted from TcpListener on Main Server Thread.
this.c = c;
this.networkStream = new NetworkStream(this.c);
udp = new UdpClient();
udp.Connect((IPEndPoint)c.RemoteEndPoint);
// Then starts 2 thread for listening, 1 for TCP and 1 for UDP.
}
private void handleUDPTraffic(){
IPEndPoint groupEP = (IPEndPoint)c.RemoteEndPoint;
while (isRunning){
try{
byte[] udp_received = udp.Receive(ref groupEP);
Console.WriteLine("Received UDP Packet Data: " + udp_received.Length);
}catch{
log.ERROR("UDP", "Couldn't Receive Data...");
}
}
}
내가 바로 당신이 함께 UDP 클라이언트와 서버 작업을 할 수없는 것을 이해하고 있는가? –
필자가 알고있는 바로는 동일한 포트에서 TCP 및 UDP 수신 대기를 동시에 실행할 수 없습니다. 일반적으로 포트에서 하나의 프로토콜 만 실행할 수 있습니다. 예 : 변경 청취 및 전송을위한 UDP 포트 번호. 그러면 작동 할 것입니다. – KBO
@KBO 포트 포워딩 규칙을 보면 "Both"옵션을 허용하므로 단일 포트를 UDP 및 TCP에 사용할 수 있다는 가정을하기 때문에 확실하지 않습니다. –