not able to open socket

General support questions
Post Reply
maxpohl88
Posts: 1
Joined: 2014/10/15 12:03:26

not able to open socket

Post by maxpohl88 » 2014/10/15 12:15:26

Hi,
i want to create a ethernet socket to communicate with a other linux device.
i found a little sourcecode which should do that:

Code: Select all

//udp_server.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main(int argc, char**argv)
{
    int sockfd,n;
       struct sockaddr_in servaddr,cliaddr;
       socklen_t len;
       char mesg[1000];

       sockfd=socket(AF_INET,SOCK_DGRAM,0);

       bzero(&servaddr,sizeof(servaddr));
       servaddr.sin_family = AF_INET;
       servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
       servaddr.sin_port=htons(32000);
       bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

       for (;;)
       {
              len = sizeof(cliaddr);
              n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
              mesg[n] = 0;
              printf("Received the following:\n");
              printf("%s",mesg);
       }
}
but its not working, it seems that the code execution halts at the following line

Code: Select all

 sockfd=socket(AF_INET,SOCK_DGRAM,0);
i hope anybody can help me with that.

User avatar
avij
Retired Moderator
Posts: 3039
Joined: 2010/12/01 19:25:52
Location: Helsinki, Finland
Contact:

Re: not able to open socket

Post by avij » 2014/10/15 13:04:31

1) You may have better luck asking this question on some programming forum.
2) The code example you posted seems to work fine here.

Post Reply

Return to “CentOS 7 - General Support”