socket programming

I have succeed in compiling the below code for creating a client application:

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

int main( void )
{
struct sockaddr_in local;
int s;
int s1;
int rc;
char buf[ 1 ];

local.sin_family = AF_INET;
local.sin_port = htons( 7500 );
local.sin_addr.s_addr = inet_addr( "127.0.0.1");
s = socket( AF_INET, SOCK_STREAM, 0 );
if ( s < 0 )
{
perror( "socket call failed" );
exit( 1 );
}
rc = bind( s, ( struct sockaddr * )&local, sizeof( local ) );
if ( rc < 0 )
{
perror( "bind call failure" );
exit( 1 );
}
rc = listen( s, 5 );
if ( rc )
{
perror( "listen call failed" );
exit( 1 );
}
s1 = accept( s, NULL, NULL );
if ( s1 < 0 )
{
perror( "accept call failed" );
exit( 1 );
}
rc = recv( s1, buf, 1, 0 );
if ( rc <= 0 )
{
perror( "recv call failed" );
exit( 1 );
}
printf( "%c\n", buf[ 0 ] );
rc = send( s1, "2", 1, 0 );
if ( rc <= 0 )
perror( "send call failed" );
exit( 0 );
}

When I run the program, it seems that nothing is happen. How can I test the program? Is there any thing wrong?
What did you expect the program to do? How do you know it's not doing anything?
Did you get any error/warning while compiling? Are you sure you have all the headers you're including?

By the way, it's better to use the [ code ] tags for your sourcecode.
Last edited on
What you've written is typically referred to as a server application since it opens the socket and listens for connections. Clients connect to servers. Servers listen for and accept connections.

Nothing will happen unless you write a client-side application that attempts connection to this port and sends data to it.
Topic archived. No new replies allowed.