//MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler.....
/*An implimentation of socket using two files..ONE IS client & 2nd IS SERVER...*/
//SO ENJOY SOCKETS ..FRIENDS.....
//THIS IS SERVER
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/socket.h>//all calls of socket are defined in it
#include<linux/in.h>//types of addresses are defined here ..also struct sockaddr_in is defined in it
/*struct sockaddr_in
{
unsigned char* sin_path;
};*/
int main()
{
struct sockaddr_in server_addr,client_addr;//after assigning values to this variable...it will be used into bind system call as 2nd argument....
int count,ret,fd,client_fd;
char buff[5];
socklen_t len;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(555);//address of server..i.e. ITS PUBLIC IP ADDRESS...WITH TYPE OF ADDRESS I.E.AF_INET////I M USING LINUX ...SO IT MAY BE AF_UNIX FOR LINUX OR UNIX SYSTEMS SPECIALLY...& ALSO PORT NO IS ASSIGNED INTO VARIALE OF "struct sockaddr_in{"
ret=bind(fd,(struct sockaddr*)&server_addr,len);//bind system call use...sin_path="name of socket"..provide this name to the socket//fd of server is binded to name
if(ret==0)
printf("name is provided to this server socket\n");//struct sockaddr_in has no member sin_path...?
ret=listen(fd,5);//listen call will make wait to server ..by polling procedure...untill client make request by its connect call....it has 2 parameters ...1st is fd..i.e. file descriptor of this server socket...& 2nd is no. of clients for which it may accept maximum..AT THE SAME TIME...bcz..it will estimate that queue for how much elements is to be created...in that way is create queue for those no. of clients
//make wait to those clients//server socket will wait & acquire for 5 clients max. at same time
if(ret==0)
printf("listen queue will be created\n");
client_fd=accept(fd,(struct sockaddr*)&client_addr,&len);//accept call require server socket's fd...&emply variable of "struct sockaddr_in{" which has acquired memory of elements of "struct sockaddr_in{"......in which during connect call...all client_addr parameters provided by client..came while accepting the request of client to create a new fd of socket..i.e. exact copy of that client's socket...which will have all the data written by that client,...whose request is accepted..//3rd arg is pointer to len..in which length of IP address oF will come...
//SERVER fd WILL BE USED TO GET ,...CLIENT'S FD...IN ACCEPT CALL
/* SOME NICE QUESTIONS CREATED WHILE ITS IMPLEMENTATION?*/ //THIS IS CLIENT..... #include<stdio.h> #include<stdlib.h> #include<string.h> #include<linux/in.h> #include<sys/socket.h> #include<fcntl.h> #include<unistd.h> int main() { int fd,count=0,ret; socklen_t len;//len is of type socklen_t ...len =strlen of address of "sruct sockaddr_in{"...or its variable client_addr len=sizeof(struct sockaddr_in); struct sockaddr_in client_addr; client_addr.sin_family=AF_INET; client_addr.sin_port=htons(555);// I will use these arguments while using connect call client_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); fd=socket(AF_INET,SOCK_STREAM,0); printf("fd=%d\n",fd); ret=connect(fd,(struct sockaddr*)&client_addr,len);//padding of structure is done in in.h file ...so client_addr variable is either of struct sockaddr_in structure ...it is typecasted by "struct sockaddr{" //TO CONNECT THERE SHOULD BE A SOCKET ON OTHER SYSTEM...WAITING FOR CLIENT TO COME..USING LISTEM SYSTEM CALL.............. if(ret==0)//connect will return 0 for success... { printf("connected\n"); } printf("anu\n"); count=write(fd,"hello",5);//it will not write into fd...untill created socket is connected to another socket in other system/....or using loopback address in same system.. //loopback address is to connet in ur own system ...to communicate implementation of socket ...it is provided such that it will connect the socket into its own system... printf("count=%d\n",count); return 0; }
//QUES. CREATED........??????? //May i change the port no. of server...to enter?
//if there are more than 2 sockets....in same system..means 2 servers are used to do two diffrent task in same system...which server will acess...or which socket will access my client...can at entry point of socket.....i recognise the data..& operation wt specific socket can do?
//IF I m using same system to implement the socket...currently..IT should not need of port no. m I RIGHT?