我写了个客户/服务起的程序可是,可是不知道怎么连接编译了!!!

wengzhong 2003-11-07 06:36:06
//主要的意图就是一个棋盘 ,两个人轮流下棋

//我刚转到LINUX写程序还有很多不习惯!!
//有个问题想问问!
//怎么把头文件和.cpp文件连起来??
//
//我的这个程序需要把
//game.h server_client.h player.h client.cpp -->client.exe
//game.h server_client.h player.h server.cpp -->server.exe

//我是这么写的makefile
//
//但是不 幸的是不能通过

//client.o: server_client.h game.h client.cpp
// g++ -c client.cpp
//server.o: server_client.h game.h server.cpp
// g++ -c server.cpp
`````````````````````````````````````````````````````````
[root@wengzhong text]# make ./makefile
makefile:3: *** missing separator. Stop.
[root@wengzhong text]# make makefile
makefile:3: *** missing separator. Stop.
```````````````````````````````````````````````````````````
谢谢指点一下!!!!




````````````````````````````````````````````````````````````````
game.h//主要是头文件和一些数据
````````````````````````````````````````````````````````````````

#ifndef _game_h_
#define _game_h_


#include<iostream>
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<cstring>
#include<stdlib.h>
#include<netinet/in.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
#define PORT 11111 //端口
#define MAXSIZEH 10 //棋盘大小
#define MAXSIZEV 10 //~~~~
#define BLOCK 1 //等待队列大小




#endif

``````````````````````````````````````````````````````````````````````````````
```````````````````````````
server_client.h //网络接口的一个类,可以产生服务起端,也可以产生客户端对象
``````````````````````````````````````````````````````````````````````````````
```````````````````````````
#ifndef _server_client_h_
#define _server_client_h_
#include "game.h"

using namespace std;
class server_client
{
private:
int sockfd;
struct sockaddr_in sock_h_c;
struct hostent *host;
unsigned short port;
int p;
public:
server_client(char *hostname,unsigned short portt=PORT)//初始话
{
host=gethostbyname(hostname);
if(host==NULL)
{
cerr<<"bad hostname\n";
exit(1);
}
port=portt;
sock_h_c.sin_family=AF_INET;
sock_h_c.sin_port=htons(port);
sock_h_c.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(sock_h_c.sin_zero),0);

}
void Socket() //socke
{
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
cerr<<"socekt allocation failed\n";
exit(1);
}

}
void Bind() //bind
{
if(bind(sockfd,(struct sockaddr *)&sock_h_c,sizeof(struct sockaddr_in))==-1)

{
cerr<<"can not bind.\n";
exit(1);
}
else
{
cout<<"successfully bound\n";

}
}
void Connect() //connect
{
if(connect(sockfd,(struct sockaddr*)&sock_h_c,sizeof(struct sockaddr))==-1)

{
cerr<<"connect error\n";
exit(1);
}
cout<<"successfully connect\n";
}
void Accept() //accept
{
p=accept(sockfd,NULL,NULL);
Close();
}
void Listen() //listen
{
listen(sockfd,BLOCK);
}
int get_sock_id() //得到ACCEPT产什的新的文件描述符
{
return p;
}
int get_sock_fd() //得到SOCKET产生的文件描述符
{
return sockfd;
}
void Close()
{
close(sockfd);
}
};


#endif


``````````````````````````````````````````````````````````````````````````````
```````````````````````````````
player.h // 游戏者
``````````````````````````````````````````````````````````````````````````````
```````````````````````````````
#ifndef _player_h_
#define _player_h_

#include"game.h"
using namespace std;
class player
{
private:
char plane[MAXSIZEH][MAXSIZEV]; //游戏者得到的棋盘
int data[2];
char my_stone; //自己下的起用什么标记
char peer_stone; //对方
int p; //文件描述符
void Read()
{
read(p,data,sizeof(data)); //READ 读出来诉数据
if(data[0]==88) //以88作为结束
{
cout<<"the player quit the game\n";
exit(1);
}
}
void Write() //写数据
{
write(p,data,sizeof(data));
}
void change(int x,int y,char t) //写棋盘
{
plane[x][y]=t;
data[0]=x;
data[1]=y;

}
bool can(int x,int y) //判断输入是否合法
{

if (x>=0&&x<MAXSIZEH&&y>=0&&y<MAXSIZEV&&plane[x][y]== * )
{
return 1;
}
else
{
return 0;
}
}
public:
player(char a,char b,int soc) //初始话
{
my_stone=a;
peer_stone=b;
p=soc;
int i,j;
for(i=0;i<MAXSIZEH;++i)
{
for(j=0;j<MAXSIZEV;++j)
{
plane[i][j]= * ;
}
}
}
void showplane() //显示棋盘
{
int i,j;
for(i=0;i<MAXSIZEH;++i)
{
for(j=0;j<MAXSIZEV;++j)
{
cout<<plane[i][j];
}
cout<<endl;
}
}

void get_my_stone() //得到自己的输入
{
int x,y;
do
{
cin>>x;
if(x==88)
{
data[0]=88;
break;
}
cin>>y;
}
while(!can(x,y));
if(x!=88)
{
change(x,y,my_stone);
}
Write();
if(x==88)
{
exit(1);
}
}
void get_peer_stone() //得到对方的输入
{
Read();
change(data[0],data[1],peer_stone);
}

};

#endif

``````````````````````````````````````````````````````````````````````````````
`````````````````````````````````
client.cpp //客户端的CPP
``````````````````````````````````````````````````````````````````````````````
`````````````````````````````````
#include "server_client.h"
#include "player.h"

int main()
{
char name[256];
cout<<"put client s name\n";
cin>>name;
server_client client(name); 主机名子
client.Socket(); //
client.Connect(); //socket通信
player client_player( O , X ,client.get_sock_fd());//创建PLAYER
while(1)
{
client_player.showplane();
cout<<"Go\n";
client_player.get_my_stone();
client_player.showplane();
cout<<"puts\n";
client_player.get_peer_stone();
}
close(client.get_sock_fd());
return 0;
}

``````````````````````````````````````````````````````````````````````````````
```````````````````````````

server.cpp //服务起的
``````````````````````````````````````````````````````````````````````````````
```````````````````````````
#include "server_client.h"
#include "player.h"

int main()
{
char hostname[256];
cout<<"put server s name\n";
cin>>hostname;
server_client server(hostname);
server.Socket(); //SOCKET通信
server.Bind(); //
server.Listen(); //
server.Accept(); //
if(server.get_sock_id()==-1)
{
cerr<<"error creat server\n";
exit(1);
}
player server_player( X , O ,server.get_sock_id());
while(1)
{
server_player.showplane();
cout<<"wait~~~~\n";
server_player.get_peer_stone();
server_player.showplane();
cout<<"Go~~\n";
server_player.get_my_stone();
}
close(server.get_sock_id());
return 0;
}
...全文
24 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
blh 2003-11-10
  • 打赏
  • 举报
回复
试试下面的Makefile
你的头文件和cpp文件在同一目录下

client: server_client.h game.h client.cpp
g++ -o clinet -g client.cpp -I. #必须以tab字符作为起始行

server: server_client.h game.h server.cpp
g++ -o server -g server.cpp -I.

clean:
rm *.o client server

你执行时使用
make clinet 创建client
make server 创建server
liuty2006 2003-11-10
  • 打赏
  • 举报
回复
mk
newalbert 2003-11-10
  • 打赏
  • 举报
回复
在makefile中,规则或命令前面一定得是tab,不能只是空格。
wengzhong 2003-11-07
  • 打赏
  • 举报
回复
我用的是
G++
没有用GCC
:)
gomi 2003-11-07
  • 打赏
  • 举报
回复
.cpp的扩展名gcc是不认的
.c表示c的程序
.C或.cc表示C++的程序

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧