《TCP/IP网络编程》第5章
Linux计算器服务器端
op_server.c
#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 error_handling(char *message);
int calculate(int opnum, int opnds[], char oprator);
int main(int argc, char *argv[]) {
int serv_sock;
int clnt_sock;
char opinfo[BUF_SIZE];
int result;
int opnc_cnt;
int i;
int recv_len;
int recv_cnt;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t addr_size;
if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock==-1)
error_handling("socket() error");
addr_size = sizeof(struct sockaddr_in);
memset(&serv_addr, 0, addr_size);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*)&serv_addr, addr_size)==-1)
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
for(i=0; i<5; i++) {
opnd_cnt = 0;
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &addr_size);
if(clnt_sock==-1)
error_handling("accept() error");
else
printf("Connected client %d\n", i+1);
//接收一个字节,数值个数
read(clnt_sock, &opnd_cnt, 1);
recv_len = 0;
//接收opnd_cnt个数值+1个运算符
while(recv_len<OPSZ*opnd_cnt+1) {
recv_cnt = read(clnt_sock, &opinfo[recv_len], BUF_SIZE-1);
recv_len += recv_cnt;
}
result = calculate(opnd_cnt, (int*)opinfo, opinfo[recv_len-1);
write(clnt_sock, (char *)result, sizeof(result));
close(clnt_sock);
}
close(serv_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 error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc op_server.c -o opserver
./opserver 9190
Linux计算器客户端
op_client.c
#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 RLT_SIZE 4
#define OPSZ 4
void error_handling(char *message);
int main(int argc, char *argv[]) {
int sock;
char opmsg[BUF_SIZE];
int result;
int opnd_cnt;
int i;
struct sockaddr_in addr;
socklen_t addr_size;
if(argc!=3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock==-1)
error_handling("socket() error");
addr_size = sizeof(addr);
memset(&addr, 0, addr_size);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr*)&addr, addr_size)==-1)
error_handling("connect() error");
else
puts("Connected......");
fputs("Operand count: ", stdout);
scanf("%d", &opnd_cnt);
mpmsg[0] = (char)opnd_cnt;
for(i=0; i<opnd_cnt; i++) {
printf("Operand %d: ", i+1);
scanf("%d", (int*)&opmsg[1+OPSZ*i]);
}
fgetc(stdin); //'\n'
fputs("Operator: ", stdout);
scanf("%c", &opmsg[1+OPSZ*opnd_cnt]);
write(sock, opmsg, 1+OPSZ*opnd_cnt+1);
read(sock, &result, RLT_SIZE);
printf("Operation result: %d\n", result);
close(sock);
return 0;
}
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc op_client.c -o opclient
./eclient 127.0.0.1 9190
Windows计算器服务器端
op_server_win.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF_SIZE 1024
#define OPSZ 4
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET hServSock;
SOCKET hClntSock;
char opinfo[BUF_SIZE];
int result;
int opnc_cnt;
int i;
int recv_len;
int recv_cnt;
SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
int szAddr;
if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
hServSock = socket(PF_INET, SOCK_STREAM, 0);
if(hServSock==INVALID_SOCKET)
ErrorHanding("socket() error");
szAddr = sizeof(SOCKADDR_IN);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(atoi(argv[1]));
if(bind(hServSock, (SOCKADDR *)&servAddr, szAddr)==SOCKET_ERROR)
ErrorHanding("bind() error");
if(listen(hServSock, 5)==SOCKET_ERROR)
ErrorHanding("listen() error");
for(i=0; i<5; i++) {
opnd_cnt = 0;
hClntSock = accept(hServSock, (SOCKADDR *)&clntAddr, &szAddr);
//if(hClntSock==INVALID_SOCKET)
if(hClntSock==-1)
ErrorHanding("accept() error");
else
printf("Connected client %d\n", i+1);
//接收一个字节,数值个数
recv(hClntSock, &opnd_cnt, 1, 0);
recv_len = 0;
//接收opnd_cnt个数值+1个运算符
while(recv_len<OPSZ*opnd_cnt+1) {
recv_cnt = recv(hClntSock, &opinfo[recv_len], BUF_SIZE-1, 0);
recv_len += recv_cnt;
}
result = calculate(opnd_cnt, (int*)opinfo, opinfo[recv_len-1);
send(hClntSock, (char *)&result, sizeof(result), 0);
closesocket(hClntSock);
}
closesocket(hServSock);
WSACleanup();
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 ErrorHanding(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc op_server_win.c /Fe:opServerWin.exe /link ws2_32.lib
opServerWin 9190
Windows计算器客户端
op_client_win.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF_SIZE 1024
#define RLT_SIZE 4
#define OPSZ 4
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET hSock;
char opmsg[BUF_SIZE];
int result;
int opnd_cnt;
int i;
SOCKADDR_IN servAddr;
int szAddr;
if(argc!=3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
hSock = socket(PF_INET, SOCK_STREAM, 0);
if(hSock==INVALID_SOCKET)
ErrorHanding("socket() error");
szAddr = sizeof(SOCKADDR_IN);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(argv[1]);
servAddr.sin_port = htons(atoi(argv[2]));
if(connect(hSock, (SOCKADDR *)&servAddr, szAddr )==SOCKET_ERROR)
ErrorHanding("connect() error");
else
puts("Connected.......");
fputs("Operand count: ", stdout);
scanf("%d", &opnd_cnt);
mpmsg[0] = (char)opnd_cnt;
for(i=0; i<opnd_cnt; i++) {
printf("Operand %d: ", i+1);
scanf("%d", (int*)&opmsg[1+OPSZ*i]);
}
fgetc(stdin); //'\n'
fputs("Operator: ", stdout);
scanf("%c", &opmsg[1+OPSZ*opnd_cnt]);
send(hSock, opmsg, 1+OPSZ*opnd_cnt+1, 0);
recv(hSock, &result, RLT_SIZE, 0);
printf("Operation result: %d\n", result);
closesocket(hSock);
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc op_client_win.c /Fe:opClientWin.exe /link ws2_32.lib
opClientWin 127.0.0.1 9190