sever
// op_server.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#define BUF_SIZE 1024
#define OPSZ 4
void ErrorHandling(char *message);
int calculate(int opnum,int opnds[],char oprator);
int main(int argc, char* argv[])
{
WSADATA wsaData;
SOCKET hServSock,hClntSock;
char opinfo[BUF_SIZE];
int result , i;
char opndCnt;
int recvCnt,recvLen;
SOCKADDR_IN servAdr,clntAdr;
int clntAdrSize;
if (argc!=2)
{
printf("Usage:%s<IP><port>\n",argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
ErrorHandling("WSAStartup() error!");
hServSock=socket(PF_INET,SOCK_STREAM,0);
if(hServSock==INVALID_SOCKET)
ErrorHandling("socket()error");
memset(&servAdr,0,sizeof(servAdr));
servAdr.sin_family=AF_INET;
servAdr.sin_addr.s_addr=htonl(INADDR_ANY);
servAdr.sin_port=htons(atoi(argv[1]));
if(bind(hServSock,(SOCKADDR*)&servAdr,sizeof(servAdr))==SOCKET_ERROR)
ErrorHandling("bind() error!");
if(listen(hServSock,5)==SOCKET_ERROR)
ErrorHandling("listen() error!");
clntAdrSize=sizeof(clntAdr);
for (i=0;i<5;i++)
{
opndCnt=0;
hClntSock=accept(hServSock,(SOCKADDR*)&clntAdr,&clntAdrSize);
recv(hClntSock,&opndCnt,1,0);
recvLen=0;
while ((opndCnt*OPSZ+1)>recvLen)
{
recvCnt=recv(hClntSock,&opinfo[recvLen],BUF_SIZE-1,0);
recvLen+=recvCnt;
}
result=calculate(opndCnt,(int*)opinfo,opinfo[recvLen-1]);
send(hClntSock,(char*)&result,sizeof(result),0);
closesocket(hClntSock);
}
closesocket(hServSock);
WSACleanup();
system("pause");
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 ErrorHandling(char *message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}
op_client
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#define BUF_SIZE 1024
void ErrorHandling(char *message);
int main(int argc, CHAR* argv[])
{
WSADATA wsaData;
SOCKET hSocket;
char message[BUF_SIZE];
int str_len,recv_len,recv_cnt;
SOCKADDR_IN serv_adr;
if(argc!=3)
{
printf("Usage:%S<IP> <port>\n",argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
ErrorHandling("WSAStart() error!");
hSocket=socket(PF_INET,SOCK_STREAM,0);
if(hSocket==INVALID_SOCKET)
ErrorHandling("socket() error");
memset(&serv_adr,0,sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=inet_addr(argv[1]);
serv_adr.sin_port=htons(atoi(argv[2]));
if(connect(hSocket,(SOCKADDR*)&serv_adr,sizeof(serv_adr))==SOCKET_ERROR)
ErrorHandling("connect() error!");
else
puts("Connected...");
while (1)
{
fputs("Input message(Q to quit):",stdout);
fgets(message,BUF_SIZE,stdin);
if(!strcmp(message,"q\n")||!strcmp(message,"Q\n"))
break;
str_len=send(hSocket,message,strlen(message),0);
recv_len=0;
while(recv_len<str_len)
{
recv_cnt=recv(hSocket,&message[recv_len],BUF_SIZE-1,0);
if(recv_cnt==-1)
ErrorHandling("read() error!");
recv_len+=recv_cnt;
}
message[recv_len]=0;
printf("Message from server:%s",message);
}
closesocket(hSocket);
WSACleanup();
return 0;
}
void ErrorHandling(char* message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}