日期:2014-05-16 浏览次数:20794 次
/*-
* Copyright (C) mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file udpport.c
* @brief
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fd;
int ret;
struct sockaddr_in servaddr;
struct sockaddr_in addr;
socklen_t len;
fd = socket(PF_INET, SOCK_DGRAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
printf("ret=%d\n", ret);
memset(&addr, 0, sizeof(addr));
len = sizeof(addr);
getsockname(fd, (struct sockaddr *)&addr, &len);
printf("ret=%d\n", ret);
printf("port=%d\n", (int)ntohs(addr.sin_port));
return 0;
}