Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Conexão via socket a um endereço IP

From Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por kyllercg
Última alteração feita por hamishwillee em 09 Dec 2011


A seguir um trecho de código Open C para conectar a um endereço IP dado o nome do ponto de acesso, o endereço IP e a porta. Este código usa ioctl para realizar a tarefa. Da mesma forma, pode-se fazer a conexão sem usar ioctl

int ConnectToIpAdress(char *apname, char *ipaddr , int port)
{
ifreq ifr;
int sockfd;
struct sockaddr_in destAddr, selfAddr;
 
// Nome da interface
strcpy(ifr.ifr_name, apname);
 
sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
ioctl(sockfd,SIOCSIFNAME, &ifr);
ioctl(sockfd, SIOCIFSTART , &ifr);
 
selfAddr.sin_family = AF_INET;
selfAddr.sin_addr.s_addr = INADDR_ANY;
selfAddr.sin_port = htons(port);
 
// Binding
bind(sockfd,(struct sockaddr*)&selfAddr, sizeof(selfAddr));
 
destAddr.sin_family = AF_INET;
destAddr.sin_addr.s_addr = inet_addr( ipaadr );
destAddr.sin_port = htons(port);
connect(sockfd, (struct sockaddr*)&destAddr, sizeof(destAddr));
 
ioctl(sockfd, SIOCIFSTOP, &ifr);
close(sockfd);
return;
}


Links relacionados

This page was last modified on 9 December 2011, at 04:25.
177 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×