-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
516 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
search_tree:search_tree.c | ||
.PHONY:all | ||
all:server client | ||
server: game.c server.c | ||
gcc -o $@ -g $^ | ||
.PHONY: | ||
clean | ||
client: game.c client.c | ||
.PHONY:clean | ||
clean: | ||
rm -f search_tree | ||
|
||
rm -f server client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,110 @@ | ||
#include <stdio.h> | ||
#include <arpa/inet.h> | ||
#include <stdlib.h> | ||
#include <sys/types.h> | ||
#include "game.h" | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <sys/types.h> | ||
#include <netinet/in.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
void SocketCommon(int sock, struct sockaddr_in* server) | ||
#include <stdlib.h> | ||
|
||
void Menu() | ||
{ | ||
char buf[1024]; | ||
struct sockaddr_in peer; | ||
while(1) | ||
{ | ||
socklen_t len = sizeof(peer); | ||
printf("Please Enter# \n"); | ||
fflush(stdout); | ||
ssize_t s = read(0,buf,sizeof(buf)-1); | ||
if(s > 0) | ||
{ | ||
buf[s-1] = 0; | ||
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)server,sizeof(*server)); | ||
ssize_t _s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&peer,&len); | ||
if(_s > 0) | ||
{ | ||
buf[_s] = 0; | ||
printf("server echo# %s\n",buf); | ||
} | ||
printf("==================================\n"); | ||
printf("|| 1.开始游戏 0.退出游戏 ||\n"); | ||
printf("==================================\n"); | ||
} | ||
void Game(int sock) | ||
{ | ||
Point point_server,point_client; | ||
char board[ROWS][COLS]; | ||
InitBoard(board,ROWS,COLS); | ||
PrintBoard(board,ROWS,COLS); | ||
ssize_t s; | ||
int x,y; | ||
while(1){ | ||
printf("请下子(输入坐标)> "); | ||
scanf("%d%d",&x,&y); | ||
point_client.row = x-1; | ||
point_client.col = y-1; | ||
ClientMove(board,&point_client); | ||
PrintBoard(board,ROWS,COLS); | ||
write(sock,(void*)&point_client,sizeof(point_client)); | ||
if(GameState(board,&point_client) == 'X') | ||
{ | ||
printf("You win!\n"); | ||
break; | ||
} | ||
else if(GameState(board,&point_client) == 'p') | ||
{ | ||
printf("平局!\n"); | ||
break; | ||
} | ||
s = read(sock,(void*)&point_server,sizeof(point_server)); | ||
if(s == 0) | ||
{ | ||
printf("Server quit\n"); | ||
return; | ||
} | ||
ServerMove(board,&point_server); | ||
PrintBoard(board,ROWS,COLS); | ||
if(GameState(board,&point_server) == 'O') | ||
{ | ||
printf("Server win!\n"); | ||
break; | ||
} | ||
else if(GameState(board,&point_server) == 'p') | ||
{ | ||
printf("平局!\n"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
int main(int argc,char *argv[]) | ||
int main(int argc, char* argv[]) | ||
{ | ||
if(argc < 3) | ||
if(argc != 3) | ||
{ | ||
perror("argc"); | ||
printf("Usage: ./server [ip] [port]\n"); | ||
exit(1); | ||
} | ||
//创建 socket 文件描述符 | ||
//AF_INET ipv4地址类型 | ||
//SOCK_DGRAM udp类型 | ||
//0 默认使用SOCK_DGRAM类型 | ||
int sock = socket(AF_INET,SOCK_DGRAM,0); | ||
|
||
int sock = socket(AF_INET,SOCK_STREAM,0); | ||
if(sock < 0) | ||
{ | ||
perror("socket"); | ||
exit(2); | ||
exit(1); | ||
} | ||
|
||
struct sockaddr_in server; | ||
server.sin_family = AF_INET;//ipv4地址类型 | ||
server.sin_port = htons(atoi(argv[2]));//端口号 | ||
server.sin_addr.s_addr = inet_addr(argv[1]);//ipv4地址转换 因为输入的是点分十进制 | ||
|
||
struct sockaddr_in client; | ||
client.sin_family = AF_INET; | ||
client.sin_addr.s_addr = inet_addr(argv[1]); | ||
client.sin_port = htons(atoi(argv[2])); | ||
int ret = connect(sock,(const struct sockaddr*)&client,sizeof(client)); | ||
if(ret < 0) | ||
{ | ||
perror("connect"); | ||
exit(2); | ||
} | ||
Menu(); | ||
int state; | ||
while(1) | ||
{ | ||
printf("请做出你的选择> "); | ||
scanf("%d",&state); | ||
if(state == 1) | ||
{ | ||
Game(sock); | ||
}else if(state == 0) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
printf("选择错误\n"); | ||
Menu(); | ||
sleep(2); | ||
} | ||
} | ||
|
||
SocketCommon(sock,&server); | ||
close(sock); | ||
return 0; | ||
} |
Oops, something went wrong.