Results 1 to 4 of 4

Thread: Socket problem on Ubuntu

  1. #1
    Join Date
    Feb 2008
    Posts
    324

    Socket problem on Ubuntu

    My little UDP Server:

    Code:
    #include<stdio.h>
    #include<signal.h>
    #include<sys/time.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<netdb.h>
    #define PORTS 6260 //port server 
    main()
    {
    struct sockaddr_in sin_serv;  // server structure 
    struct sockaddr_in sin_clien; // client structure 
    int namelen, sock;
    int pid, statut,n,i,lg;
    char line[100];
    if(sock_serv=sock(AF_INET, SOCK_DGRAM, IPPROTO_UDP)<0)
    {
     perror("socket" );
     exit(1);
    }
    bzero(&sin_serv,sizeof(sin));
    sin_serv.sin_family=AF_INET;
    sin_serv.sin_port=htons(PORTS);
    sin_serv.sin_addr.s_addr=INADDR_ANY;
    if(bind(sin_serv,&sin_serv,sizeof(sin_serv))<0)
    {
     perror("bind" );
     exit(2);
    }
    /************ Receive the message **************/
    n=recvfrom(sock_serv,line,strlen(line),0,&sin_clien,sizeof(sin_clien));
    printf("my message %s is well received it is longer: %d", line,strlen(line));
    return;
    }
    under windows: no error and it runs very well.
    Linux (Ubuntu): no error but when I run it I see an error: bind: Socket operation on non-socket! why this error? and mean what?

  2. #2
    Join Date
    May 2008
    Posts
    945

    Re: Socket problem on Ubuntu

    Instead :
    Code:
    if(bind(sin_serv,&sin_serv,sizeof(sin_serv))<0)
    Try :
    Code:
    if(bind(sock_serv,&sin_serv,sizeof(sin_serv))<0)

  3. #3
    Join Date
    Feb 2008
    Posts
    324

    Re: Socket problem on Ubuntu

    Thank you but the problem remains, here are the new:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<signal.h>
    #include<sys/time.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<netdb.h>
    #define PORTS 6260 //port server 
    int main(void)
    {
    struct sockaddr_in sin_serv;  // server structure 
    struct sockaddr_in sin_clien; // client structure 
    int namelen, sock_serv;
    int pid, statut,n,i,lg;
    char line[100];
    if(sock_serv=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)<0)
    {
     perror("socket" );
     return 0;
    }
    bzero(&sin_serv,sizeof(sin_serv));
    sin_serv.sin_family=AF_INET;
    sin_serv.sin_port=htons(PORTS);
    sin_serv.sin_addr.s_addr=INADDR_ANY;
    if(bind(sock_serv,(struct sockaddr *)&sin_serv,sizeof(sin_serv))<0)
    {
     perror("bind" );
     return 0;
    }
    /************ Receive the message **************/
    int size_sin_clien=sizeof(sin_clien);
    n=recvfrom(sock_serv,line,strlen(line),0,(struct sockaddr *)&sin_clien,&size_sin_clien);
    printf("my message %s is well received it is longer: %d", line,strlen(line));
    return 1;
    }
    why its not working?

  4. #4
    Join Date
    May 2008
    Posts
    945

    Re: Socket problem on Ubuntu

    Does the message remains exactly the same (bind: Socket operation on non-socket) or is it that the program goes a step further?

    I have a somewhat similar program, but instead of
    Code:
    sin_serv.sin_addr.s_addr = INADDR_ANY;
    I
    Code:
    sin_serv.sin_addr.s_addr = inet_addr(ip_addr); // e.g. inet_addr("123.10.20.30")
    And below the line of recvfrom does not seem correct. I will rather:
    Code:
    n=recvfrom(sock_serv,line,sizeof(line),0,(struct sockaddr *)&sin_clien,&size_sin_clien);

Similar Threads

  1. Replies: 6
    Last Post: 22-07-2011, 10:24 PM
  2. Problem communication SOCKET TCP / IP in Java
    By Fragant in forum Software Development
    Replies: 5
    Last Post: 14-09-2010, 07:03 PM
  3. Socket problem in java
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 04:23 AM
  4. Core i7/i5 1156 socket problem
    By cornto in forum Hardware Peripherals
    Replies: 3
    Last Post: 19-11-2009, 01:15 PM
  5. MySQL: connection problem of socket
    By Bull50 in forum Software Development
    Replies: 4
    Last Post: 31-03-2009, 12:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,856,698.48453 seconds with 16 queries