Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #define SA struct sockaddr
- struct packet {
- char oper[5];
- int int1;
- int int2;
- };
- struct resultpacket {
- int success;
- int result;
- };
- //addition procedure
- int addition(int num1, int num2) {
- return num1 + num2;
- }
- //subtraction procedure
- int subtract(int num1, int num2) {
- return num1 - num2;
- }
- //multiplication procedure
- int multiply(int num1, int num2) {
- return num1 * num2;
- }
- //division procedure
- int divide(int num1, int num2) {
- if (num2 == 0)
- {
- return -1;
- }
- return num1 / num2;
- }
- // Function for send/receive between client and server.
- void talk(int sockfd)
- {
- // infinite loop for send/receive
- for (;;) {
- int result, success;
- //create packet for receiving from client
- struct packet pkt;
- recv(sockfd, &pkt, sizeof(pkt), 0);
- printf("Packet from client: %s %d %d\n", pkt.oper, pkt.int1, pkt.int2);
- struct resultpacket respkt;
- //check operator from packet
- if (strcmp(pkt.oper,"add") == 0) {
- result = addition(pkt.int1, pkt.int2);
- success = 1;
- } else if (strcmp(pkt.oper,"sub") == 0) {
- result = subtract(pkt.int1, pkt.int2);
- success = 1;
- } else if (strcmp(pkt.oper,"mul") == 0) {
- result = multiply(pkt.int1, pkt.int2);
- success = 1;
- } else if (strcmp(pkt.oper,"div") == 0) {
- result = divide(pkt.int1, pkt.int2);
- if (result == -1) {
- success = 0;
- } else {
- success = 1;
- }
- }
- respkt.success = success;
- respkt.result = result;
- //sending the result packet to client
- printf("Sending to client - success: %d / result: %d\n",respkt.success, respkt.result);
- send(sockfd, &respkt, sizeof(respkt), 0);
- }
- }
- // Server setup function
- int main()
- {
- int sockfd, connfd, len;
- struct sockaddr_in servaddr, cli;
- // socket create and verification
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd == -1) {
- printf("socket creation failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully created..\n");
- bzero(&servaddr, sizeof(servaddr));
- // assign IP, PORT
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons( 8888 );
- // Binding newly created socket to given IP and verification
- if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
- printf("socket bind failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully binded..\n");
- // Now server is ready to listen and verification
- if ((listen(sockfd, 5)) != 0) {
- printf("Listen failed...\n");
- exit(0);
- }
- else
- printf("Server listening..\n");
- len = sizeof(cli);
- // Accept the data packet from client and verification
- connfd = accept(sockfd, (SA*)&cli, &len);
- if (connfd < 0) {
- printf("server acccept failed...\n");
- exit(0);
- }
- else
- printf("server acccept the client...\n");
- // Function for send/receive between client and server
- talk(connfd);
- // After chatting close the socket
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement