MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / createSocket

Method createSocket

libapp2sys/src/main/cpp/udp.h:660–704  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

658 }
659
660 int createSocket(const char *ip, int port, int timeoutms) {
661 if (m_fd > 0) {
662 return m_fd;
663 }
664
665 this->m_timeoutms = timeoutms;
666 srand(time(NULL));
667
668 pthread_mutex_init(&m_mutexSendQueue, NULL);
669 int ret = pipe(m_pipefd);
670 if (ret < 0) {
671 perr("create pipe ret:%d", ret);
672 return -1;
673 }
674
675 const int fd = socket(AF_INET, SOCK_DGRAM, 0);
676 if (fd < 0) {
677 perr("create socket fd:%d", fd);
678 return -1;
679 }
680
681 struct sockaddr_in address;
682 memset(&address, 0, sizeof(address));
683 address.sin_family = AF_INET;
684 address.sin_port = htons(port);
685
686 if (ip == NULL || strlen(ip) == 0) {
687 address.sin_addr.s_addr = htonl(INADDR_ANY);
688 }
689 else {
690 inet_pton(AF_INET, ip, &(address.sin_addr));
691 }
692 const int bindRet = ::bind(fd, (struct sockaddr *) &address, (socklen_t)sizeof(address));
693 if (bindRet < 0) {
694 perr("create socket bindRet:%d fd:%d %s:%d ", bindRet, fd, ip, port);
695 close(fd);
696 return -2;
697 }
698
699 m_fd = fd;
700 m_fdCloseRequested = false;
701 pwrn("Create socket succ. fd:%d %s:%d pipe:[%d,%d]", fd, ip, port, m_pipefd[0], m_pipefd[1]);
702
703 return fd;
704 }
705
706 int startLoop() {
707 return loop();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected