Qualcuno può dirmi come impostare l'indirizzo della porta di origine nel socket UDP ?. La mia applicazione client deve inviare i pacchetti dalla porta 57002 alla porta del server 58007.Come impostare la porta di origine nel socket UDP in c?
8
A
risposta
2
bind(2)
it. Utilizzare l'indirizzo INADDR_ANY
e il numero di porta.
0
struct sockaddr_in sa;
int ret, fd;
memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_port = htons(LOCAL_PORT);
sa.sin_addr.s_addr = inet_addr(LOCAL_IP_ADDRESS);
ret = bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr));
0
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(32000); //destination port for incoming packets
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr= htonl(INADDR_ANY);
cliaddr.sin_port=htons(33000); //source port for outgoing packets
bind(sockfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
use sendto with servaddr
use recvfrom with cliaddr
if u check wireshark u will see source port as 33000 and destination port as 32000 for sendto operation
8
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define DST_PORT 58007
#define SRC_PORT 57002
#define IP "127.0.0.1"
int main(int argc, char *argv[]) {
struct sockaddr_in addr, srcaddr;
int fd;
char message[] = "Hello, World!";
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(IP);
addr.sin_port = htons(DST_PORT);
memset(&srcaddr, 0, sizeof(srcaddr));
srcaddr.sin_family = AF_INET;
srcaddr.sin_addr.s_addr = htonl(INADDR_ANY);
srcaddr.sin_port = htons(SRC_PORT);
if (bind(fd, (struct sockaddr *) &srcaddr, sizeof(srcaddr)) < 0) {
perror("bind");
exit(1);
}
while (1) {
if (sendto(fd, message, sizeof(message), 0, (struct sockaddr *) &addr,
sizeof(addr)) < 0) {
perror("sendto");
exit(1);
}
sleep(1);
}
return 0;
}
Problemi correlati
- 1. Porta di origine e di destinazione per il socket UDP?
- 2. Numero di porta di origine socket impostato
- 3. Come impostare un socket UDP Winsock?
- 4. Programmazione socket: UDP Client-Server in C
- 5. Come parlare con socket UDP con HTML5?
- 6. Errore socket 10052 su socket UDP
- 7. Timeout set socket UDP
- 8. Come recuperare con garbo da un'eccezione socket C# udp
- 9. C# UDP Socket: Ottieni indirizzo ricevitore
- 10. Due socket UDP diversi in un sistema possono collegare la stessa porta?
- 11. Come utilizzare i socket UDP in Android?
- 12. Impostare la porta nelle richieste
- 13. Impostazione dell'IP di origine per una presa UDP
- 14. programmazione del socket udp non bloccante in C: cosa ottengo?
- 15. come differenziare TCP/UDP durante la programmazione socket
- 16. Come impostare la porta in HTTParty
- 17. Come creare un server UDP in C?
- 18. Port forwarding UDP per emulatore android; "adb forward udp: porta udp: porta" e telnet localhost adb_port non funziona
- 19. Come connettersi a una porta UDP in Python?
- 20. Utilizzo di write()/read() sul socket UDP?
- 21. Semplice UDP Socket Tutorial Necessario
- 22. Come esporre una porta UDP su Docker?
- 23. Trasmissione di pacchetti UDP con indirizzo di origine 0.0.0.0
- 24. Invio stringa su UDP in C++
- 25. Come impostare la porta predefinita per Webrick?
- 26. Socket di routing su un'altra porta
- 27. Ascolta su TCP e UDP sulla stessa porta
- 28. C#: Listener UDP Thread
- 29. Come impostare l'opzione di riutilizzo dell'indirizzo per un socket di datagramma nel codice java?
- 30. Come dividere ricevuto con boost asio udp socket datagrammi uniti
si può dare un codice di esempio. – Balamurugan
La pagina manuale collegata ha un campione. Eccone un altro http://alas.matf.bg.ac.rs/manuals/lspe/snode=47.html –