Thursday, May 28, 2009

Basic socket programming (TCP)

Sockets are end points on a machine for communication.

Below code demonstatres connection oriented program (TCP).

Client:
#include following libraries stdio.h, sys/socket.h, sys/types.h, netinet/in.h, netdb.h
int main(int argc,char **argv)
{
  int sockfd,newsockfd;  // socket descriptors 
  struct sockaddr_in serv_addr;
  struct hostent *he;
  char a[50],a1[50];
  sockfd=socket(AF_INET,SOCK_STREAM,0); // creates a socket
  if(sockfd<0)
  {
   printf("Socket failed");
   exit(0);
  }
// Use either green or yellow code, if you want to give server name as command line argument use the green one. 
  if((he=gethostbyname(argv[2]))==NULL)  // gets the ip address
  {
  printf("gethost error");
  exit(1);
  }
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_addr.s_addr=inet_addr("127.127.127.1");
  memcpy(&(serv_addr.sin_addr),he->h_addr,he->h_length);
  serv_addr.sin_port=htons(atoi(argv[1])); 
  if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
  {
   printf("\nConnection failed");
   exit(0);
  }
  printf("\nEnter a Message");
  scanf("%s",&a);
  write(sockfd,a,50);
  read(sockfd,a1,50);
  printf("\nClient received Message %s",a1);
  close(sockfd); // closing the socket descriptor
}


Server: 
#include following libraries stdio.h, sys/socket.h, sys/types.h, netinet/in.h
int main(int argc,char **argv)
{
  int sockfd,newsockfd,clilen;
  struct sockaddr_in serv_addr,cli_addr;
  char a[50];
  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd<0)
  {
   printf("\nSocket failed!");
   exit(0);
  }
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  serv_addr.sin_port=htons(atoi(argv[1]));
  if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
  {
   printf("\nBind failed");
   exit(1);
  }
  if(listen(sockfd,5)<0)
  {
   printf("\nListen fails");
   exit(0);
  }
  clilen=sizeof(cli_addr);
  newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
  read(newsockfd,a,80);
  printf("Server received %s",a);
  write(newsockfd,"Server received message",26);
  close(newsockfd);
  close(sockfd);
}


compilation:
client :   gcc -o client client.c
server :   gcc -o server server.c

Execution:
server : ./server (portno)
client : ./client (portno) (servername)

Communication between client and server.


Client side:
client creates a socket and sends a request to server

Server side:
  1. Server creates a socket and waiting for a request.
  2. when request arrives to server socket then it creates a new socket and forward the request to that new socket. 
  3. Further communication is goes on in between client's socket and server's new socket.

Monday, May 11, 2009

Different ways of reading a file in C

In C there are different methods to read a file, but which is more suitable to your code is important. Here we look into different functions and see how it reads from a file.

file.txt (Input)
I am in your blogspot.

fscanf:

FILE *stream,*fopen();
stream=fopen("file.txt","r");
while(!feof(stream))
{
fscanf(stream,"%s",x);
printf("%s\n",x);
}

output:
I
am
in
your
blogspot.
blogspot.

It is clear that is taking string by string and printing on the screen. Have you notice blogspot. is printing twice. we can avoid that using memcpy command.

#include
main()
{
FILE *stream,*fopen();
char x[100];
stream=fopen("file.txt","r");
while(!feof(stream))
{
fscanf(stream,"%s",x);
printf("%s\n",x);
memcpy(x,"\0",100); // every time it clears x
}
}

Output:
I
am
in
your
blogspot.

fgets:

while(fgets(x,5, stream) != NULL)
{
printf("%s\n",x);
}

Output:
I am
 in
your
 blo
gspo
t.

Every time, fgets method takes 5 character from the stream and puts into user buffer.

fgetc:

x=fgetc(stream);
while(x!=EOF)
{
printf("%c\n",x);
x=fgetc(stream);
}

Output:
I

a
m

i
n

y
o
u
r

b
l
o
g
s
p
o
t
.

it reads character by character from file.

Friday, May 8, 2009

Shell script commands

It is similar to batch programming.

PRINT ON THE SCREEN

echo "Hello"

WRITE TO A FILE

echo "Hello" > file.txt

APPEND TO A FILE

echo "Hello" >> file.txt

TAKE INPUT FROM KEYBOARD

read d (some variable d)

EXAMPLE ON FOR LOOP

for d in 1 2 4 f g g d s
do
echo $d
done

EXAMPLE OF WHILE LOOP

n=1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done

Singleton

Singleton design patters are used where there is only one instance of an object is required.

Here is an example of singleton class.

public class singleton
{
private static singleton instance=null;
static public singleton getInstance()
{
if(instance==null)
instance=new singleton();
return instance;
}
private singleton()
{}
.
.
.
.
}

Saturday, May 2, 2009