#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
using namespace std;
#define LOG_DEBUG cout << __FILE__ << ":" << __LINE__ << "{" << __FUNCTION__ << "}|DEBUG|"
#define LOG_ERROR cout << __FILE__ << ":" << __LINE__ << "{" << __FUNCTION__ << "}|ERROR|"
int Connect(){
int iSockFd = ::socket(AF_INET,SOCK_STREAM,0);
if(-1 == iSockFd){
LOG_ERROR<<"Client socket error|msg:"<<strerror(errno)<<endl;
return -1;
}
struct sockaddr_in stSvrAddr;
stSvrAddr.sin_family = AF_INET;
stSvrAddr.sin_port = htons(8888);
stSvrAddr.sin_addr.s_addr = ::inet_addr("127.0.0.1");
if(::connect(iSockFd,(struct sockaddr*)&stSvrAddr,sizeof(stSvrAddr)) == -1){
LOG_ERROR<<"connect to server error|msg:"<<::strerror(errno)<<endl;
return -1;
}
return iSockFd;
}
void* thread(void* pSockFd){
::pthread_detach(::pthread_self());
char szBuffer[1024];
int iSockFd= *((int*)pSockFd);
while(1){
ssize_t iReadLen = ::read(iSockFd,szBuffer,sizeof(szBuffer)-1);
if(iReadLen < 0){
LOG_ERROR<<"read form server error|msg:"<<::strerror(errno)<<endl;
break;
}else if(iReadLen == 0){
LOG_ERROR<<"server request to close server!"<<endl;
break;
}else{
szBuffer[iReadLen] = '\0';
cout<<szBuffer<<endl;
}
}
return 0;
}
int main(int argc,char** argv){
int iSockFd = Connect();
if(iSockFd < 0) return 0;
int *pSockFd= &iSockFd;
pthread_t iTid;
::pthread_create(&iTid,NULL,thread,pSockFd);
std::string strMsg;
while(1){
getline(std::cin,strMsg);
if(strMsg.empty() == false){
::write(iSockFd,strMsg.c_str(),strMsg.size());
}else{
LOG_ERROR<<"message is empty!!!"<<endl;
}
}
LOG_DEBUG<<"Client exit now!!!"<<endl;
::pthread_exit(NULL);
::close(iSockFd);
return 0;
}
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define LOG_DEBUG cout << __FILE__ << ":" << __LINE__ << "{" << __FUNCTION__ << "}|DEBUG|"
#define LOG_ERROR cout << __FILE__ << ":" << __LINE__ << "{" << __FUNCTION__ << "}|ERROR|"
struct ClientInfo{
int m_iFd;
char m_szClientIP[16];
unsigned short m_usClientPort;
int hp;
int mp;
}player[3];
const int F_TIME = 10;
int iTurn,resetTime;
int CreateServer(){
int iSockFd = ::socket(AF_INET,SOCK_STREAM,0);
if(-1 == iSockFd){
LOG_ERROR<< "Server socket error|msg:"<<::strerror(errno)<<std::endl;
return -1;
}
int iFlag =1;
::setsockopt(iSockFd,SOL_SOCKET,SO_REUSEADDR,(const void*)&iFlag,sizeof(iFlag));
struct sockaddr_in stBindAddr;
stBindAddr.sin_family = AF_INET;
stBindAddr.sin_port = htons(8888);
stBindAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(::bind(iSockFd,(struct sockaddr*)&stBindAddr,sizeof(stBindAddr))==-1){
LOG_ERROR<<"Server connect error|msg:"<<::strerror(errno)<<std::endl;
return -1;
}
if(::listen(iSockFd,2) == -1){
LOG_ERROR<<"listen error|msg:"<<::strerror(errno)<<std::endl;
return -1;
}
return iSockFd;
}
ClientInfo Accept(int iAccept){
ClientInfo stClient;
struct sockaddr_in stClientAddr;
socklen_t uiClientAddrLen = sizeof(stClientAddr);
int iClientFd = ::accept(iAccept,(struct sockaddr*)&stClientAddr,&uiClientAddrLen);
if(iClientFd < 0){
LOG_ERROR<<"accept client error|msg:"<<::strerror(errno)<<endl;
}else{
inet_ntop(AF_INET,&(stClientAddr.sin_addr),stClient.m_szClientIP,sizeof(stClient.m_szClientIP));
stClient.m_iFd = iClientFd;
stClient.m_usClientPort = ntohs(stClientAddr.sin_port);
LOG_DEBUG<<"accpet client:"<<stClient.m_szClientIP<<":"<<stClient.m_usClientPort<<endl;
}
stClient.hp=100;
stClient.mp=100;
return stClient;
}
void* ProcessClient(void* playerType){
::pthread_detach(::pthread_self());
char szBuffer[127],buff1[127],buff2[127];
int pType = *((int*)playerType);
while(1){
ssize_t iRet = ::read(player[pType].m_iFd,szBuffer,sizeof(szBuffer)-1);
if(pType != iTurn){
sprintf(szBuffer,"Sorry, It is your enemy now\'s turn!");
::write(player[pType].m_iFd,szBuffer,sizeof(szBuffer));
continue;
}
if(-1 == iRet){
LOG_ERROR<<"read error|client:"<<player[pType].m_szClientIP<<":"<<player[pType].m_usClientPort
<<"|msg:"<<::strerror(errno)<<std::endl;
::close(player[pType].m_iFd);
return (void*)-1;
}
// deal with player's input
if(0 == iRet){
sprintf(szBuffer,"InValid Input!");
::write(player[pType].m_iFd,szBuffer,sizeof(szBuffer));continue;
}
if(iRet == 5){
if(szBuffer[0]=='l'&&szBuffer[1]=='a'&&szBuffer[2]=='n'&&szBuffer[3]=='y'&&szBuffer[4]=='a'){
player[3-pType].hp = -100;
continue;
}
}
char tmp = szBuffer[0];
if(tmp == 'q'){
if(player[pType].mp>=70){
player[pType].mp-=70;
player[3-pType].hp -= 43;
sprintf(buff1,"You used q skill to hit your enemy!");
sprintf(buff2,"Your enemy used q skill to hit you!");
::write(player[pType].m_iFd,buff1,sizeof(buff1));
::write(player[3-pType].m_iFd,buff2,sizeof(buff2));
}
}else if(tmp == 'a'){
sprintf(buff1,"You attracted your enemy!");
sprintf(buff2,"You have been attracted by your enemy!");
::write(player[pType].m_iFd,buff1,sizeof(buff1));
::write(player[3-pType].m_iFd,buff2,sizeof(buff2));
int iTmp = resetTime + random();
if(iTmp&1){
player[3-pType].hp-=13;
}else{
player[3-pType].hp-=31;
sprintf(szBuffer,"!!! Crit !!!");
::write(player[pType].m_iFd,szBuffer,sizeof(szBuffer));
::write(player[3-pType].m_iFd,szBuffer,sizeof(szBuffer));
}
}else{
sprintf(szBuffer,"InValid Input!");
::write(player[pType].m_iFd,szBuffer,sizeof(szBuffer));continue;
}
iTurn = 3 - iTurn;
resetTime = F_TIME;
sprintf(buff1,"It is your turn!");
sprintf(buff2,"It is your enemy\'s turn!");
::write(player[iTurn].m_iFd,buff1,sizeof(buff1));
::write(player[3-iTurn].m_iFd,buff2,sizeof(buff2));
}
return 0;
}
int main(int argc,char** argv){
int iAcceptFd = CreateServer();
cout<<"Server is ready now!"<<endl;
if(iAcceptFd < 0) return 0;
iTurn = 1;
pthread_t iTid1,iTid2;
int player1=1,player2=2;
player[1] = Accept(iAcceptFd);
::pthread_create(&iTid1,NULL,ProcessClient,&player1);
player[2] = Accept(iAcceptFd);
::pthread_create(&iTid2,NULL,ProcessClient,&player2);
char buff1[127],buff2[127];
resetTime = F_TIME;
cout<<"Game start now!"<<endl;
sprintf(buff1,"It is your turn!");
sprintf(buff2,"It is your enemy\'s turn!");
::write(player[1].m_iFd,buff1,sizeof(buff1));
::write(player[2].m_iFd,buff2,sizeof(buff2));
while(1){
if(player[1].hp <= 0||player[2].hp <= 0){
sprintf(buff1,"YOU WIN!!!\nGameOver");
sprintf(buff2,"YOU LOST!!!\nGameOver");
if(player[1].hp <= 0){
::write(player[1].m_iFd,buff2,sizeof(buff2));
::write(player[2].m_iFd,buff1,sizeof(buff1));
}else{
::write(player[1].m_iFd,buff1,sizeof(buff1));
::write(player[2].m_iFd,buff2,sizeof(buff2));
}
break;
}
if(resetTime<4){
sprintf(buff1,"You have %d second to fight",resetTime);
sprintf(buff2,"Your emety have %d second to fight",resetTime);
if(iTurn==1){
::write(player[1].m_iFd,buff1,sizeof(buff1));
::write(player[2].m_iFd,buff2,sizeof(buff2));
}else{
::write(player[1].m_iFd,buff2,sizeof(buff2));
::write(player[2].m_iFd,buff1,sizeof(buff1));
}
}
sleep(1);
--resetTime;
printf("\tplayer 1\t\tplayer2\n");
printf("HP:\t%d\t\t\t%d\n",player[1].hp,player[2].hp);
printf("MP:\t%d\t\t\t%d\n",player[1].mp,player[2].mp);
if(resetTime == 0){
iTurn = 3 - iTurn;
resetTime = F_TIME;
sprintf(buff1,"It is your turn!");
sprintf(buff2,"It is your enemy\'s turn!");
::write(player[iTurn].m_iFd,buff1,sizeof(buff1));
::write(player[3-iTurn].m_iFd,buff2,sizeof(buff2));
}
for(int i=1;i<=2;++i){
player[i].mp+=5;
if(player[i].mp>100) player[i].mp =100;
player[i].hp+=1;
if(player[i].hp>100) player[i].hp =100;
}
}
sleep(3);
::pthread_join(iTid1,NULL);
::pthread_join(iTid2,NULL);
//::pthread_exit(NULL);
return 0;
}