sockets( ) programming [Archive] - C Board

PDA

View Full Version : sockets( ) programming


xlordt
09-20-2003, 09:21 PM
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


#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;

}

Sebastiani
09-22-2003, 10:22 PM
>> 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).

Hunter2
09-23-2003, 11:44 AM
>>static void BAIL(...)
Just curious, why is it static?

xlordt
09-23-2003, 02:15 PM
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?

Hunter2
09-23-2003, 04:54 PM
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.

//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 :eek:

xlordt
09-23-2003, 09:11 PM
*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

Hunter2
09-23-2003, 09:25 PM
*breathes huge sigh of relief*

;)

xlordt
09-23-2003, 09:26 PM
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 ;)