Skip to content

Instantly share code, notes, and snippets.

@jhowliu
Last active October 31, 2015 12:41
Show Gist options
  • Save jhowliu/5c22f9223f24bd5da250 to your computer and use it in GitHub Desktop.
Save jhowliu/5c22f9223f24bd5da250 to your computer and use it in GitHub Desktop.
Computer Networking Homework 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 512
#define SERVER_PORT 3000
int main(int argc, char **argv)
{
int socket_fd;
struct sockaddr_in *server_addr;
char *buf = (char *)malloc(sizeof(char) * BUFFER_SIZE);
printf("Please input the server ip address:");
scanf(" %s", buf);
printf("Connecting the server from %s\n", buf);
socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (socket_fd == -1) printf("Creating socket arises error.\n");
server_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
inet_aton(buf, &server_addr->sin_addr);
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(SERVER_PORT);
if (connect(socket_fd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) != -1)
{
printf("Connect successfully.\n");
do
{
printf("Please input the two floating number(a,b): ");
while(1)
{
float a, b;
if (scanf(" %f,%f", &a, &b) == 2)
{
sprintf(buf, "%f,%f", a, b);
send(socket_fd, buf, strlen(buf), 0);
break;
}
else
{
// Clean up the input stream.
scanf(" %s", buf);
}
printf("Please input a floating number again(a,b): ");
}
printf("Please input operator such as +, -, *, /: ");
while(1)
{
if (scanf(" %s", buf) && (strcmp(buf, "+") == 0
|| strcmp(buf, "-") == 0
|| strcmp(buf, "*") == 0
|| strcmp(buf, "/") == 0))
{
send(socket_fd, buf, strlen(buf), 0);
break;
}
printf("Please input operator such as +, -, *, / again: ");
}
printf("Wait for result from server......\n");
if (recv(socket_fd, buf, BUFFER_SIZE, 0)) printf("%s\n", buf);
printf("Do you want to continue(Y/N): ");
scanf(" %s", buf);
} while (strcmp("Y", buf) == 0 || strcmp("y", buf) == 0);
}
else
{
printf("Your ip address is wrong.\n");
return 1;
}
close(socket_fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 512
#define PORT 3000
#define BACKLOG 5 // How many pending connections queue will hold.
float calculate(float a, float b, char operator, int *flag)
{
float result = 0;
switch (operator)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b != 0.0) result = a / b;
else *flag = 1;
break;
}
return (float)((int)(result * 100 + 0.5) / 100.0);
}
int main(int argc, char **argv)
{
int socket_fd;
int bind_state;
struct sockaddr_in *my_addr;
my_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
// Create socket file descriptor.
// SOCK_STREAM: Create a TCP connection.
// AF_INET: ipv4
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1) printf("Creating socket arises error.\n");
// Some information about server.
my_addr->sin_family = AF_INET;
my_addr->sin_port = htons(PORT);
// INADDR_ANY: Automatically set your ip address of local machine.
my_addr->sin_addr.s_addr = INADDR_ANY;
// or use my_addr.sin_addr.s_addr = inet_aton(IP);
bind_state = bind(socket_fd, (struct sockaddr *)my_addr, sizeof(struct sockaddr));
if (bind_state == -1)
{
printf("Binding error.\n");
return -1;
}
// Server listening.
listen(socket_fd, BACKLOG);
// Some information about client.
int accepted_socket_fd;
struct sockaddr_in *client_addr;
int sin_size = sizeof(struct sockaddr_in);
client_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
accepted_socket_fd = accept(socket_fd, (struct sockaddr *)client_addr, &sin_size);
if (accepted_socket_fd != -1)
{
printf("New connection.\n");
char *buf = (char *)malloc(sizeof(char) * BUFFER_SIZE);
char operator;
float a, b, result;
int flag = 0; // The flag catch divide-by-zero.
while(1)
{
printf("Wait for data.\n");
if (accepted_socket_fd == -1) break;
if (recv(accepted_socket_fd, buf, BUFFER_SIZE, 0))
{
printf("Receive %s from client\n", buf);
sscanf(buf, "%f,%f", &a, &b);
a = (float)((int)(a * 100 + 0.5) / 100.0);
b = (float)((int)(b * 100 + 0.5) / 100.0);
if (recv(accepted_socket_fd, buf, BUFFER_SIZE, 0))
{
sscanf(buf, "%c\n", &operator);
result = calculate(a, b, operator, &flag);
}
if (flag)
{
flag = 0;
sprintf(buf, "You cannot divide by zero.\n");
send(accepted_socket_fd, buf, strlen(buf), 0);
}
else
{
printf("%.2f %c %.2f = %.2f\n", a, operator, b, result);
sprintf(buf, "%.2f %c %.2f = %.2f", a, operator, b, result);
send(accepted_socket_fd, buf, strlen(buf), 0);
}
}
else
{
// Close the client connection.
close(accepted_socket_fd);
break;
}
}
}
close(socket_fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment