C - Programming Guide |
Client - Server Program in C - Socket Programming Posted: 30 Nov 2012 03:31 AM PST Server File - #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> #include<pthread.h> #define MAX 30 int AddDigits(int number); int Factorial(int number); int StringLength(char str[]); int StringCompare(char str[],char str1[]); char* StrConcat(char str1[],char str2[]); char* SubsString(char str1[],int start,int end); int ssfd; int ccfd,res; int serverlen,clientlen; struct sockaddr_in serveraddr; struct sockaddr_in clientaddr; char result[MAX]; typedef struct message { char msg[MAX][MAX]; char msgoutput[MAX]; int input; int ch; int output; }message; void main() { int msgLen; char *ch; ssfd=socket(AF_INET,SOCK_STREAM,0); message *msg; message *reply; msg=(message*)malloc(sizeof(message)); reply=(message*)malloc(sizeof(message)); printf("\nSSFD Value:%d",ssfd); msgLen=sizeof(message); serveraddr.sin_family=AF_INET; serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1"); //serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); //serveraddr.sin_port = htons(9734); serveraddr.sin_port=9734; serverlen=sizeof(serveraddr); bind(ssfd,(struct sockaddr*)&serveraddr,serverlen); printf("\nConnected to socket"); listen(ssfd,5); ccfd=accept(ssfd,(struct sockaddr*)&clientaddr,&clientlen); printf("\nConnection accepted successfully"); while(1) { read(ccfd,reply,msgLen); switch(reply->ch) { case 1: reply->output=AddDigits((int)reply->input); write(ccfd,reply,msgLen); break; case 2: reply->output=Factorial((int)reply->input); write(ccfd,reply,msgLen); break; case 3: reply->output=StringLength(reply->msg[0]); write(ccfd,reply,msgLen); break; case 4: reply->output=StringCompare(reply->msg[0],reply->msg[1]); write(ccfd,reply,msgLen); break; case 5: strcpy(reply->msgoutput,StrConcat(reply->msg[0],reply->msg[1])); write(ccfd,reply,msgLen); break; case 6: reply->output=Palindrome(reply->msg[0]); write(ccfd,reply,msgLen); break; case 7: strcpy(reply->msgoutput,SubsString(reply->msg[0],reply->input,reply->output)); write(ccfd,reply,msgLen); break; case 8: system("xeyes"); break; } } } int AddDigits(int number) { int result=0; while(number/10!=0) { result+=number%10; number=number/10; } result+=number%10; return result; } int Factorial(int number){ int result=1,i; for(i=1;i<=number;i++){ result*=i; } return result; } int StringLength(char str[]){ int result=0; while(str[result]!='\0'){ result++; } return result; } //0 ===> Strings are equal.... //1 ===> Strings are not equal.... int StringCompare(char str[],char str1[]) { int result; int i,j; for(i=0;i<StringLength(str);i++){ if(str[i]!=str1[i])return 1; } return 0; } //0 ===> Palindrome... //1 ===> Not Palindrome... int Palindrome(char str[]) { int result; int i,j; for(i=0,j=strlen(str)-1;i<StringLength(str);j--,i++){ if(str[i]!=str[j])return 1; } return 0; } char* SubsString(char str1[],int start,int end){ int i,j=0; //strcpy(result,str1); for(i=start;i<=end;i++){ result[j++]=str1[i]; } //result[j]='\0'; return (char*)result; } char* StrConcat(char str1[],char str2[]) { int i=0,j=0; strcpy(result,str1); strcat(result,str2); //printf("Result %s",result); return (char*)result; } Client File - /* Class :BEIT Assignment No :1 */ #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> #define MAX 30 typedef struct message { char msg[MAX][MAX]; char msgoutput[MAX]; sint input; int ch; int output; }message; void main() { int ssfd; int len; int msgLen; struct sockaddr_in address; int result; FILE *fp; char ch='A'; message *msg; message *reply; msg=(message*)malloc(sizeof(message)); reply=(message*)malloc(sizeof(message)); msgLen=sizeof(message); //strcpy(msg->msg,"Aniket"); ssfd=socket(AF_INET,SOCK_STREAM,0); address.sin_family=AF_INET; address.sin_addr.s_addr=inet_addr("127.0.0.1"); address.sin_port=9734; len=sizeof(address); result=connect(ssfd,(struct sockaddr*)&address,len); //fp=fdopen(ssfd,"rw"); if(result==-1){ perror("Server not found"); printf("\nError Result:%d",result); //exit(1); } printf("\nConnection successful"); do{ printf("\n\nSELECT Appropriate Option:\n"); printf("\n1.\tAddition of Digits"); printf("\n2.\tFactorial of a Number"); printf("\n3.\tLength of string"); printf("\n4.\tString Comparison"); printf("\n5.\tString Concatenation"); printf("\n6.\tCheck For Palindrome"); printf("\n7.\tFind Substring."); printf("\n8.\tRun Command"); printf("\n9.\tDisconnect And Exit"); scanf("%d",&msg->ch); printf("Your Choice:%d",msg->ch); //write(ssfd,msg,msgLen); switch(msg->ch){ case 1: printf("\nEnter Number:"); scanf("%d",&msg->input); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%d",reply->output); break; case 2: printf("\nEnter Number:"); scanf("%d",&msg->input); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%d",reply->output); break; case 3: printf("\nEnter any string:"); scanf("%s",msg->msg[0]); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%d",reply->output); break; case 4: printf("\nEnter any First String:"); scanf("%s",msg->msg[0]); printf("\nEnter any Second String:"); scanf("%s",msg->msg[1]); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%d",reply->output); if(reply->output==0) printf("\nStrings are equal..."); else printf("\nStrings are not equal..."); break; case 5: printf("\nEnter any First String:"); scanf("%s",msg->msg[0]); printf("\nEnter any Second String:"); scanf("%s",msg->msg[1]); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%s",reply->msgoutput); break; case 6: printf("\nEnter any string:"); scanf("%s",msg->msg[0]); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%d",reply->output); if(reply->output==0) printf("\nString is a palindrome"); else printf("\nStrings is not palindrome"); break; case 7: printf("\nEnter any string:"); scanf("%s",msg->msg[0]); printf("\nEnter Start Index:"); scanf("%d",&msg->input); printf("\nEnter End Index:"); scanf("%d",&msg->output); write(ssfd,msg,msgLen); read(ssfd,reply,msgLen); printf("Output from server:%s",reply->msgoutput); break; case 8: printf("\nEnter String-\"xeyes\" or other valid command:"); scanf("%s",msg->msg[0]); write(ssfd,msg,msgLen); system(msg->msg[0]); read(ssfd,reply,msgLen); break; default: printf("Invalid Choice....Select Another"); break; } }while(msg->ch!=9); close(ssfd); //exit(0); } /* OUTPUT desktop ~ $ gcc server.c desktop ~ $ ./a.out SSFD Value:3 Connected to socket desktop ~ $ gcc -o client.out client.c desktop ~ $ ./client.out Connection successful SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit1 Your Choice:1 Enter Number:1234 Output from server:10 SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit2 Your Choice:2 Enter Number:5 Output from server:120 SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit3 Your Choice:3 Enter any string:abc Output from server:3 SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit4 Your Choice:4 Enter any First String:xyz Enter any Second String:xyx Output from server:1 Strings are not equal... SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit5 Your Choice:5 Enter any First String:Abc Enter any Second String:Xyz Output from server:AbcXyz SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit6 Your Choice:6 Enter any string:malyalam Output from server:1 Strings is not palindrome SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit7 Your Choice:7 Enter any string:intelcoreigen Enter Start Index:2 Enter End Index:5 Output from server:telc SELECT Appropriate Option: 1. Addition of Digits 2. Factorial of a Number 3. Length of string 4. String Comparison 5. String Concatenation 6. Check For Palindrome 7. Find Substring. 8. Run Command 9. Disconnect And Exit9 */ |
You are subscribed to email updates from C - Programming Guide To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Post a Comment