Re: Socket Programming Problem



On Fri, 16 May 2008 21:33:39 +0500, arnuld wrote:

I am pretty much a socket programming beginner. I have read some parts
of Beej's Guide and created a client-server program where client sends a
number to the server and server increments the number and sends it back to
the client. Program works fine.

...SNIP....


Here is my working program. See if you can advise on some improvement:


/* a simple server
*
* it simply does very simple things step by step :)
*
* 1.) creates a socket file-descriptor.
* 2.) creates a internet socket structure and fills the server IP
* and the port number we are going to into its structure.
* 3.) binds the structure with the socket file-descriptor we creared
* in step (1). We need a pointer to that structure here.
* 4.) listen for any incoming connection from some client. We hang-in
there, waiting for some idiot user to call our echo server.
5.) accept the incoming connection.
6.) reads the number sent by client *
7.) increments the number and sends it back to the client.
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>


#define MY_SERVER "127.0.0.1"

enum { ARRSIZE = 21 };

void add_1( char * );


int main( int argc, char** argv )
{
const int MAX_CONNECTIONS = 5;

int sockfd, accept_fd, MY_PORT;


struct sockaddr_in server_str;
char stored_input[ARRSIZE + 1];
memset( stored_input, '\0', ARRSIZE+1);


if( argc == 2 )
{
MY_PORT = atoi( argv[1] );
}
else
{
fprintf(stderr, "I expect the port number as argument\n");
exit( EXIT_FAILURE );
}


if( (sockfd = socket( PF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
fprintf(stderr, "SOCKET ERROR: can not create socket\n");
exit( EXIT_FAILURE );
}


server_str.sin_family = AF_INET;
server_str.sin_port = htons(MY_PORT);
server_str.sin_addr.s_addr = inet_addr( MY_SERVER );
memset( server_str.sin_zero, '\0', sizeof( server_str.sin_zero ) );

if( bind( sockfd, (struct sockaddr*) &server_str,
sizeof( struct sockaddr )) == -1 )
{
fprintf(stderr, "BIND() error!\n");
exit( EXIT_FAILURE );
}


if( listen( sockfd, MAX_CONNECTIONS ) == -1 )
{
fprintf(stderr, "LISTEN() error!\n");
exit( EXIT_FAILURE );
}

if( (accept_fd = accept( sockfd, 0, 0 ) ) == -1 )
{
fprintf(stderr, "ACCEPT() error!\n");
exit( EXIT_FAILURE );
}


if( recv( accept_fd, stored_input, ARRSIZE+1, 0) == -1 )
{
fprintf(stderr, "RECV() error!\n");
exit( EXIT_FAILURE );
}

printf( "NUMBER == %d\n", atoi( stored_input ) );
add_1( stored_input );
printf( "NUMBER == %d\n", atoi( stored_input ));

if( send( accept_fd, stored_input, ARRSIZE+1, 0 ) == -1 )
{
fprintf(stderr, "SEND() error!\n");
exit( EXIT_FAILURE );
}


/* ok, enough chit-chat ;) now close the connection */
close( sockfd );

return 0;
}


void add_1( char* pc )
{
sprintf( pc, "%d", atoi(pc) + 1 );
}

-------------------------------------------------------------------------

/* a simple client to send a number to a server
* and then receiving the incremented number back.
*
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#define MY_SERVER "127.0.0.1"

enum { ARRSIZE = 21 };

int main( int argc, char** argv )
{
int sockfd, MY_PORT;
struct sockaddr_in client_str;
char arrc[ARRSIZE+1];
memset( arrc, '\0', ARRSIZE+1);


sprintf( arrc, "%d", rand() );

printf("%s\n", arrc);

if( argc == 2 )
{
MY_PORT = atoi( argv[1] );
}
else
{
fprintf(stderr, "I expect the port number as argument\n");
exit( EXIT_FAILURE );
}


if( (sockfd = socket( PF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
fprintf(stderr, "SOCKET ERROR: can not create socket\n");
exit( EXIT_FAILURE );
}



client_str.sin_family = AF_INET;
client_str.sin_port = htons(MY_PORT);
client_str.sin_addr.s_addr = inet_addr( MY_SERVER );
memset( client_str.sin_zero, '\0', sizeof( client_str.sin_zero ) );

if( connect( sockfd, (struct sockaddr*) &client_str,
sizeof( struct sockaddr)) == -1 )
{
fprintf(stderr, "CONNECT() error\n");
exit( EXIT_FAILURE );
}

if( send( sockfd, arrc, ARRSIZE+1, 0 ) == -1 )
{
fprintf(stderr, "SEND() error!\n");
exit( EXIT_FAILURE );
}


if( recv( sockfd, arrc, ARRSIZE+1, 0) == -1 )
{
fprintf(stderr, "RECV() error!\n");
exit( EXIT_FAILURE );
}

printf("---------------------------------\n%s\n", arrc);



return 0;

}



--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

.



Relevant Pages

  • echo client using threads
    ... pollwhile client is written using threads. ... server recvs some characters from a client and then echoes the ... int main ... exit(EXIT_FAILURE); ...
    (comp.unix.programmer)
  • Re: Learnign select()
    ... sends some text to server and then server sends back the same text to ... client and then client prints it onto the screen. ... int main ... exit(EXIT_FAILURE); ...
    (comp.unix.programmer)
  • echo client/server give problems if array sizes are different
    ... client asks for user input and then sends that line to the server ... void echo_back(int); ... exit(EXIT_FAILURE); ...
    (comp.unix.programmer)
  • Learnign select()
    ... I have created a TCP echo server-client pair where client ... sends some text to server and then server sends back the same text to ... int main ... exit(EXIT_FAILURE); ...
    (comp.unix.programmer)
  • Socket Programming in C
    ... I'm new to socket programming and what I am trying to do is create ... a client and server program that simulates rolling a die. ... struct sockaddr_in ServAddr, ClntAddr; ...
    (comp.unix.programmer)