-1
/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>

int main(int argc, char *argv[])
{
    	struct addrinfo hints,*serv,*p;
	int sockfd,newfd,status;
	struct sockaddr_storage their_addr;
	socklen_t addr_size;
	char *msg;
	int len;
	int yes=1;

	if(argc<2)
	{
		printf("Usage = portno\n");
		return(1);
	}
	
	memset(&hints,0,sizeof(hints));
	hints.ai_family=AF_UNSPEC;
	hints.ai_socktype=SOCK_STREAM;
	hints.ai_flags=AI_PASSIVE;

	if((status = getaddrinfo(NULL,argv[1],&hints,&serv)) != 0)
	{
		fprintf(stderr,"Error : %s\n",gai_strerror(status));
		return(1);
	}

	for(p=serv;p!=NULL;p=p->ai_next)
	{
	
		if( (sockfd = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
		{
			printf("Err sock!!\n");
			continue;
		}
		
		if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
	        sizeof(int)) == -1) 
		{
			perror("setsockopt");
			return(1);
		}


		if((bind(sockfd,p->ai_addr,p->ai_addrlen)) == -1);
		{
			printf("Bind err\n\n");
			close(sockfd);
			continue;
		}
	
		break;
	}

	if((listen(sockfd,10)) == -1);
		printf("Listen err!\n");
	addr_size=sizeof(their_addr);

	newfd = accept(sockfd,(struct sockaddr *)&their_addr,&addr_size);
	msg = "Aneesh was here";
	len = strlen(msg);	
	if( send(sockfd,msg,len,0) != len)
	{
		printf("Message partally reatched or not reached sucessfully\n");
	}
	freeaddrinfo(serv);
	return(0);
}

This program does not binds why .....:'(

Thank you in advance......

4
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by malathi renin
0

hey i fixed up the above program but now i am facing problems in recv() function..

This program successfully sends "Hiii!!!!" to the client .

But is not receiving properly ..
I telneted the program to test
and when i send a message from telnet to this program .

It is showing some:-
"libc.so.6"

on the screen

why this is happening ..
Help me with this.

Here is the new source code:-

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>

#define portno "4040"

int main()
{
    	struct addrinfo hints,*serv,*p;
	int sockfd,newfd,status;
	struct sockaddr_storage their_addr;
	socklen_t addr_size;
	char *msg;
	int len;
	char buff[100];

	memset(&hints,0,sizeof(hints));
	hints.ai_socktype=SOCK_STREAM;
	hints.ai_family=AF_UNSPEC;
	hints.ai_flags=AI_PASSIVE;

	if((status=getaddrinfo(NULL,portno,&hints,&serv)) == -1)
	{
		fprintf(stderr,"Error in getaddrinfo :-\n %s\n",gai_strerror(status));
	}

	for(p=serv;p!=NULL;p = p->ai_next)
	{
		if( (sockfd = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1 )
		{
			printf("Error in Socket \n");
			continue;
		}
		
		if( bind(sockfd,p->ai_addr,p->ai_addrlen) == -1)
		{
			printf("Binding error!!!\n");
			close(sockfd);			
			continue;
		}
		
		break;
	}

	/* if( (connect(sockfd,serv->ai_addr,serv->ai_addrlen) ) == -1 )
	{
		printf("Connect err!!!\n\n");
		return(0);
	}  if on client we use this*/

	listen(sockfd,10);

	addr_size= sizeof(their_addr);
	newfd = accept(sockfd, (struct sockaddr *)&their_addr,&addr_size);
	
	
		printf("%s",msg);
		
		if( ( send(newfd,"Hiii!!!!",9,0) ) == -1)
		{
			printf("err in send\n\n");
			return(1);
		}
	
	recv(newfd,buff,sizeof(buff),0);
	printf("\n");
	freeaddrinfo(serv);
	close(sockfd);
	return(0);
}
0

Hi....
dears...
this program will to perform a echo using echoserver and echo client.

/*EchoClient.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s1,t,len;
struct sockaddr_un remote;
char str[100];
if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{
printf("socket\n");
exit(1);
}
printf("Trying to connect\n");
remote.sun_family=AF_UNIX;
strcpy(remote.sun_path,SOCK_PATH);
len=strlen(remote.sun_path)+sizeof(remote.sun_family);
if(connect(s1,(struct sockaddr*)&remote,len)==-1)
{
perror("connect");
exit(1);
}
printf("Connectd\n");
while(printf("->"),fgets(str,100,stdin),!feof(stdin))
{
if(send(s1,str,strlen(str),0)==-1)
{
perror("send");
exit(1);
}
if((t=recv(s1,str,100,0))>0)
{
str[t]='\0';
printf(" echo->%s",str);
}
else
{
if(t<0)
perror("recv");
else
printf("server closed connection\n");
exit(1);
}
}
close(s1);
return 0;
}

Output:
[cs201@localhost ~]$ cc echoclient.c
[cs201@localhost ~]$ ./a.out 172.16.1.215 2000
Trying to connect
Connectd
->jai ganesh
echo->jai ganesh
->dsec
echo->dsec
->

//*EchoServer.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s1,s2,len;
struct sockaddr_un local;
struct sockaddr_un remote;
char str[100];
if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{
printf("socket\n");
exit(1);
}
local.sun_family=AF_UNIX;
strcpy(local.sun_path,SOCK_PATH);
unlink(local.sun_path);
len=strlen(local.sun_path)+sizeof(local.sun_family);
if(bind(s1,(struct sockaddr*)&local,len)==-1)
{
printf("Bind");
exit(1);
}
if(listen(s1,5)==-1)
{
printf("listen");
exit(1);
}
for(;;)
{
int done,n,t;
printf("Waiting for connection\n");
t=sizeof(remote);
if((s2=accept(s1,(struct sockaddr*)&remote,&t))==-1)
{
perror("accept");
exit(1);
}
printf("Connected\n");
done=0;
do
{
n=recv(s2,str,100,0);
if(n<=0)
{
if(n<0)
perror("recv");
done=1;
}
if(!done)
if(send(s2,str,n,0)<0)
{
perror("send");
done=1;
}
}
while(!done);
close(s2);
}
return 0;
}

//output
"echoserver.c" 68L, 1023C written
[cs201@localhost ~]$ cc echoserver.c
[cs201@localhost ~]$ ./a.out 2000
Waiting for connection

0

Hi....
dears...
this program will help to perform an echoservice using an echoserver and an echo client.

the user has to run the echo server first using cc echoserver.c followed by
./a.out [2000] minimize the window
then he has to run echoclient.c using cc echoclient.c followed by
./a.out [ip address] [2000]

/*EchoClient.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s1,t,len;
struct sockaddr_un remote;
char str[100];
if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{
printf("socket\n");
exit(1);
}
printf("Trying to connect\n");
remote.sun_family=AF_UNIX;
strcpy(remote.sun_path,SOCK_PATH);
len=strlen(remote.sun_path)+sizeof(remote.sun_family);
if(connect(s1,(struct sockaddr*)&remote,len)==-1)
{
perror("connect");
exit(1);
}
printf("Connectd\n");
while(printf("->"),fgets(str,100,stdin),!feof(stdin))
{
if(send(s1,str,strlen(str),0)==-1)
{
perror("send");
exit(1);
}
if((t=recv(s1,str,100,0))>0)
{
str[t]='\0';
printf(" echo->%s",str);
}
else
{
if(t<0)
perror("recv");
else
printf("server closed connection\n");
exit(1);
}
}
close(s1);
return 0;
}

Output:
[cs201@localhost ~]$ cc echoclient.c
[cs201@localhost ~]$ ./a.out 172.16.1.215 2000
Trying to connect
Connectd
->jai ganesh
echo->jai ganesh
->dsec
echo->dsec
->

//*EchoServer.c

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s1,s2,len;
struct sockaddr_un local;
struct sockaddr_un remote;
char str[100];
if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{
printf("socket\n");
exit(1);
}
local.sun_family=AF_UNIX;
strcpy(local.sun_path,SOCK_PATH);
unlink(local.sun_path);
len=strlen(local.sun_path)+sizeof(local.sun_family);
if(bind(s1,(struct sockaddr*)&local,len)==-1)
{
printf("Bind");
exit(1);
}
if(listen(s1,5)==-1)
{
printf("listen");
exit(1);
}
for(;;)
{
int done,n,t;
printf("Waiting for connection\n");
t=sizeof(remote);
if((s2=accept(s1,(struct sockaddr*)&remote,&t))==-1)
{
perror("accept");
exit(1);
}
printf("Connected\n");
done=0;
do
{
n=recv(s2,str,100,0);
if(n<=0)
{
if(n<0)
perror("recv");
done=1;
}
if(!done)
if(send(s2,str,n,0)<0)
{
perror("send");
done=1;
}
}
while(!done);
close(s2);
}
return 0;
}

//output
"echoserver.c" 68L, 1023C written
[cs201@localhost ~]$ cc echoserver.c
[cs201@localhost ~]$ ./a.out 2000
Waiting for connection

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.