Skip to content

Instantly share code, notes, and snippets.

@beomkm
Last active March 29, 2016 02:45
Show Gist options
  • Save beomkm/67e40f6090babf1c425e to your computer and use it in GitHub Desktop.
Save beomkm/67e40f6090babf1c425e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 1024
void err_h(char *msg);
int main(int argc,char **argv)
{
int sock;
char msg[BUF_SIZE];
int str_len;
struct sockaddr_in s_addr;
if(argc != 3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
err_h("socket() error");
memset(&s_addr, 0, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = inet_addr(argv[1]);
s_addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1)
err_h("bind() error");
else
puts("Connected.............");
while(1) {
fputs("Input message(Q to quit): ", stdout);
fgets(msg, BUF_SIZE, stdin);
if(!strcmp(msg, "q\n") || !strcmp(msg, "Q\n"))
break;
write(sock, msg, strlen(msg));
str_len = read(sock, msg, BUF_SIZE-1);
msg[str_len] = 0;
printf("Msg from server : %s", msg);
}
close(sock);
return 0;
}
void err_h(char *msg)
{
fputs(msg, stderr);
fputc('\n', stderr);
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 1024
#define OPSZ 4
void err_h(char *msg);
int calculate(int opnum, int opnds[], char operator);
int main(int argc,char **argv)
{
int s_sock, c_sock;
char opinfo[BUF_SIZE];
int result, opnd_cnt, i;
int recv_cnt, recv_len;
struct sockaddr_in s_addr, c_addr;
socklen_t c_addr_size;
if(argc != 2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
s_sock = socket(PF_INET, SOCK_STREAM, 0);
if(s_sock == -1)
err_h("socket() error");
memset(&s_addr, 0, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
s_addr.sin_port = htons(atoi(argv[1]));
if(bind(s_sock, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1)
err_h("bind() error");
if(listen(s_sock, 5) == -1)
err_h("listen() error");
c_addr_size = sizeof(c_addr);
for(i=0; i<5; i++) {
opnd_cnt = 0;
c_sock = accept(s_sock, (struct sockaddr *)&c_addr, &c_addr_size);
read(c_sock, &opnd_cnt, 1);
recv_len = 0;
while((opnd_cnt*OPSZ+1) > recv_len) {
recv_cnt = read(c_sock, &opinfo[recv_len], BUF_SIZE-1);
recv_len += recv_cnt;
}
result = calculate(opnd_cnt, (int*)opinfo, opinfo[recv_len-1]);
write(c_sock, (char*)&result, sizeof(result));
close(c_sock);
}
close(s_sock);
return 0;
}
int calculate(int opnum, int opnds[], char op)
{
int result = opnds[0], i;
switch(op) {
case '+':
for(i=1; i<opnum; i++) result += opnds[i];
break;
case '-':
for(i=1; i<opnum; i++) result -= opnds[i];
break;
case '*':
for(i=1; i<opnum; i++) result *= opnds[i];
break;
}
return result;
}
void err_h(char *msg)
{
fputs(msg, stderr);
fputc('\n', stderr);
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment