sockets( ) programming

This is a discussion on sockets( ) programming within the Networking/Device Communication forums, part of the General Programming Boards category; im tring to make a connection to a port and im tring to send some data to and then recv ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    sockets( ) programming

    im tring to make a connection to a port and im tring to send some data to and then recv it but.. i dont fully understand sockets yet.. anyhelp

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <errno.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    #define MYPORT 3490
    #define ALLOW 10
    
    static void
       BAIL( const char *on_what )
       {
          fputs( strerror( errno), stderr );
          fputs( on_what, stderr );
          exit(1);
       }
    
    int main( int argc, char **argv )
    {
    
      struct sockaddr_in mysock;
      struct sockaddr_in their_addr;
      char *msg = "xlordt was here";
      int s_sock;
      int b_bind;
      int c_connet;
      int s_listen;
      int a_accept, r_recv;
      int size, len, bsent;
    
      s_sock = socket( AF_INET, SOCK_STREAM, 0 );
      
      if( s_sock == -1 )
      {
         BAIL( "error on: socket( )\n" );
      }
    
      printf( "Status on socket( ): OK!\n" );
    
      mysock.sin_family = AF_INET;
      mysock.sin_port = htons( MYPORT );
      mysock.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    
      memset( &mysock, 0, sizeof mysock );
    
      b_bind = bind( s_sock,( struct sockaddr *)&mysock,sizeof( struct sockaddr ));
    
      if( b_bind == -1 )
      {
         BAIL( "error on: bind( )\n" );
      }
    
      printf( "Status on bind( ): OK!\n" );
    
      c_connet = connect( s_sock,( struct sockaddr *)&mysock,sizeof( struct sockaddr ));
      
      if( c_connet == -1 )
      {
        BAIL( "error on: connect( )\n" );
      }
    
      printf( "Status on connect( ): OK!\n" );
    
      s_listen = listen( s_sock, ALLOW );
    
      if( s_listen == -1 )
      {
        BAIL( "error on: listen( )\n" );
      }
    
      printf( "Status on listen: OK!\n" );
      size = sizeof( struct sockaddr_in );
    
      a_accept = accept( s_sock,( struct sockaddr *)&their_addr,&size );
    
      if( a_accept == -1 )
      {
        BAIL( "error on: accept( )\n" );
      }
    
      printf( "Status on accept: OK!\n" );
      
      len = strlen( msg );
      bsent = send( s_sock, msg, len, 0 );
    
      if( bsent == -1 )
      {
        BAIL( "error on: send( )\n" );
      }
      
      r_recv = recv( s_sock, msg, len, 0 );
      printf( "%s", r_recv );
    
      return 0;
    
    }
    Only the strong survives.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> memset( &mysock, 0, sizeof mysock );

    You just erased what you put *in* the structure.

    >> connect...

    Figure out what you're trying to do here - server or client??

    Server does:

    socket()
    bind()
    loop:
    listen()
    accept()
    loop:
    recv()
    send()

    Client:

    socket()
    connect()
    loop:
    send()
    recv()

    Notice that a server start communicating with a recv(), while the client starts with a send().

    Next, when your server calls accept(), you capture the return value of the function - that will be the socket you will use to communicate with that client. So a client uses a minimum of one socket, a server - two(one for accept()ing, and one for the connection with a client).



    ITSA
    Socket Library!

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>static void BAIL(...)
    Just curious, why is it static?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    I used static void cause.. when i tried using.. struct i was getting errors.. so when i used static it worked like a charm..

    btw.. Sebastiani thanx for the info im still a bit confused in socket programming.. but im sure i will get it soon =) thanx again

    btw.. what would the loop represent.. what would you normaly loop there? cause as far as what i was doing.. i was looping send and recv to get date and send it.. what do you suggest.. is this wrong or write?
    Last edited by xlordt; 09-23-2003 at 02:17 PM.
    Only the strong survives.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I used static void cause.. when i tried using.. struct i was getting errors..
    *begins to giggle wildly*

    *cough cough*
    Erm, have you ever considered the reason that "struct" was giving you errors? You do not declare a function by calling it a struct.
    Code:
    //Example of a struct, and making a global object of one
    struct myStructureWithVariablesInIt
    {
         int variable1;
         char variable2;
         std::string variable3;
         SomeClass variable4;
         float variable5;
    };
    
    //This is the C++ way of making an object of the struct
    myStructureWithVariablesInIt someStructure;
    //This is the C way of making an object of the struct
    struct myStructureWithVariablesInIt someStructure2;
    
    
    //Example of a global function
    void doSomething(int argument1, float argument2)
    {
         someStructure.variable1 = argument1;
         someStructure.variable5 = argument2;
    }
    Note the lack of "struct" or "static" or anything else before the "void".

    I hope this is just a bad day for you, but if it's not then (not meaning to be harsh) I suggest you go back and learn about functions/structures/loops/if's/pointers before taking up socket programming
    Last edited by Hunter2; 09-23-2003 at 04:59 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    *begins to giggle wildly =)*

    hehe its ok.. i know what i know.. its likei said.. i forgot.. but.. im starting to remember now.. but.. anyways thanx.. Your post was really educational
    i understand pointers, loops, if, structures =) but.. anyways.. thanx again just been a while since i coded in c good thing it's similar to php in lots of ways heh
    Only the strong survives.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    *breathes huge sigh of relief*

    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    sorry.. its just hard to remember after a long time.. im coding like if i never coded before.. but.. little by little im getting it all back
    Only the strong survives.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 06:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM

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