*************************************SERVER*********************************
#include <sys/types.h>
#include <sys/socket.h>
#include<stdio.h>
#include <unistd.h>
#include<netinet/in.h>
#include<string.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv)
{
int sockfd,clientfd,len;
char tubelight[100];
struct sockaddr_in server1,client1;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
printf("Socket Creation Fail\n");
}
else
printf("Socet Created....\n");
server1.sin_family=AF_INET;
server1.sin_port=htons(SERV_TCP_PORT);
server1.sin_addr.s_addr=INADDR_ANY;
bind(sockfd,(struct sockaddr*)&server1,sizeof(server1));
printf("Binding......\n");
listen(sockfd,5);
printf("Listening.........\n");
len=sizeof(client1);
printf("Accepted......\n\n\n");
recvfrom(sockfd,tubelight,100,0,(struct sockaddr*)&client1,&len);
printf("TUBELIGHT=%s",tubelight);
sendto(sockfd,tubelight,100,0,(struct sockaddr*)&client1,len);
close(sockfd);
}
***************************************CLIENT*********************************
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#define SERV_TCP_PORT 5035
int main(int argc,char **argv)
{
int sockfd,len;
char tubelight[100];
struct sockaddr_in server1;
struct hostent *server;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
printf("Error in Creation.....\n");
server1.sin_family=AF_INET;
server1.sin_port=htons(SERV_TCP_PORT);
server1.sin_addr.s_addr=inet_addr("127.0.0.1");
connect(sockfd,(struct sockaddr*)&server1,sizeof(server1)) ;
len=sizeof(server1);
printf("TUBELIGHT:-");
fgets(tubelight,100,stdin);
sendto(sockfd,tubelight,100,0,(struct sockaddr*)&server1,len);
bzero(tubelight,sizeof(tubelight));
recvfrom(sockfd,tubelight,100,0,(struct sockaddr*)&server1,&len);
printf("server=%s",tubelight);
close(sockfd);
}
No comments:
Post a Comment