C Sokcet HELP :P

This is a discussion on C Sokcet HELP :P within the C Programming forums, part of the General Programming Boards category; First i want to say hi all of toy i am new here my name is shellc0de as you see ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    C Sokcet HELP :P

    First i want to say hi all of toy i am new here my name is shellc0de as you see now i have one problem I make simple socket(server) in C tat when he accept connection send string to client.But when i compile & run and connect with telnet i get blank outpu,but thats not a point i solve that.This process is finished very fast that is problem whyi didn't get output now i put Sleep(1); and programs runs perefct but i need help to done this without Sleep() function. Thanks
    Here is code:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(void)
    {
        WSADATA wsaData;
        if(WSAStartup(MAKEWORD(2,0),&wsaData)!=0)
        {
            printf("WSAStartup failed!");
        }
        // Some variables
        int old_socket,new_socket,addrlen;
        addrlen=sizeof(struct sockaddr_in);
        char *message="This is a message to send!\r\n\r\n";
        struct sockaddr_in addr;
    
    
        // Creating a socket
        old_socket=socket(AF_INET,SOCK_STREAM,0);
        if(!old_socket){
            printf("Can't create socket!\n");
        }
        printf("Socket is created.\n");
    
    
        // Binding a socket to a port
        addr.sin_family=AF_INET;
        addr.sin_addr.s_addr=INADDR_ANY;
        addr.sin_port=htons(7000);
        bind(old_socket,(struct sockaddr *)&addr,sizeof(addr));
        printf("Socket is binded on port %d\n",htons(addr.sin_port));
    
    
        // Listening for connection
        listen(old_socket,3);
        printf("Listening...\n");
    
    
        // Accepting a connection
        new_socket=accept(old_socket,(struct sockaddr *)&addr,&addrlen);
        if(!new_socket){
            printf("Can't accept connection!\n");
        }
        printf("Connection accepted.\n");
    
    
        // Sendig data to a connection
        send(new_socket,message,strlen(message),0);
        printf("Data has been send.\n");
        Sleep(1);
    
    
        // Closing connection
        close(new_socket);
        printf("Socket is closed.\n");
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    265
    Are you sure you don't get compiler errors or warnings?

    You initialise addrlen to the sizeof a struct you have not yet declared.

    It's also normal to initialise the struct before using it using memset or

    Code:
     struct  sockaddr_in addr = {0};

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Well I just added this code before the bind()
    Code:
        int yes = 1;
        if (setsockopt(old_socket, SOL_SOCKET, SO_REUSEADDR, &yes,
            sizeof(int)) == -1) {
          perror("setsockopt");
          exit(1);
        }
    and it all seems fine.

    Code:
    // server
    $ ./a.out 
    Socket is created.
    Socket is binded on port 7000
    Listening...
    Connection accepted.
    Data has been send.
    Socket is closed.
    
    // client
    $ telnet localhost 7000
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    This is a message to send!
    
    Connection closed by foreign host.
    Without the SO_REUSEADDR, I just get
    $ telnet localhost 7000
    Trying 127.0.0.1...
    telnet: Unable to connect to remote host: Connection refused

    until the socket has timed out.

    Adding
    close(old_socket);
    is probably a good idea as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    265
    Quote Originally Posted by gemera View Post

    You initialise addrlen to the sizeof a struct you have not yet declared.


    [/code]
    Sorry,ignore that.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    265
    There's some Linux/Windows bleed going on here methinks!

    You used close() which is the way to close sockets in Linux.

    So this should have given you a compiler warning.

    In Windows the function is closesocket().

    Which means you need to use the Windows version of Salem's setsockopt() code or

    Code:
    const char *yes = "TRUE";
    
    if (setsockopt(old_socket, SOL_SOCKET, SO_REUSEADDR, yes,sizeof(yes)))
    {
         perror("setsockopt");
         exit(1);
    }

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Nothing :P again same problem :3 Salem-i am on win platform i use codeblocks with mingw,maybe that code works fine under linux.
    Last edited by shellc0de; 12-21-2012 at 08:10 AM.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Code:
    intyes = 1;if (setsockopt(old_socket, SOL_SOCKET, SO_REUSEADDR, &yes,
        sizeof(int)) == -1) {
      perror("setsockopt");
      exit(1);
    }
    
    This code works thanks,i just needed to put that and to use closesocket() Thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21