服务端
客户端
头文件
c++实现调用C函数
第一步采用gcc编译服务端:
hqlong@ubuntu:~$ gcc server.c -o server
第二步启动服务等待链接:
hqlong@ubuntu:~$ ./server
第三步采用gcc编译客户端:
(有main函数的,编译后可执行)
hqlong@ubuntu:~$ gcc client.c -o client
hqlong@ubuntu:~$ ./client
(没有main函数的,编译后不可执行,但可以供c++调用的库)
hqlong@ubuntu:~$ gcc -c client.c -o client.o
第四步编译C++源代码:
hqlong@ubuntu:~$ g++ -c cppClient.cpp -o cppClient.o (只编译不连接)
第五步通过头文件连接编译好的c库和c++库:
hqlong@ubuntu:~$ g++ -o cppClient cppClient.o client.o
能实现这样连接的关键点是c和c++都实现了头文件client.h的定义:
[b]#ifdef __cplusplus
extern "C" {
#endif[/b]
。。。。。。
void client();
。。。。。。
[b]#ifdef __cplusplus
}
#endif[/b]
参考:
http://hqlong.com/2009/06/800.html
http://man.chinaunix.net/develop/c&c++/linux_c/default.htm
/*
* server.c
*
* Created on: 2010-6-30
* Author: ankerdiao
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MAXSIZE 1024
void server() {
int sockfd, client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
//创建套接字
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket create failed!");
exit(1);
}
//绑定端口地址
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
if (bind(sockfd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind error!");
exit(1);
}
//监听端口
if (listen(sockfd, BACKLOG) == -1) {
perror("listen error");
exit(1);
}
while (1) {
int sin_size = sizeof(struct sockaddr_in);
if ((client_fd = accept(sockfd, (struct sockaddr*) &remote_addr,
&sin_size)) == -1) {
perror("accept error!");
continue;
}
printf("Received a connection from %s\n", (char*) inet_ntoa(
remote_addr.sin_addr));
//子进程段
if (!fork()) {
//接受client发送的请示信息
int rval;
char buf[MAXSIZE];
if ((rval = read(client_fd, buf, MAXSIZE)) < 0) {
perror("reading stream error!");
continue;
}
printf("%s\n", buf);
//向client发送信息
char* msg = "Hello,Mr hqlong, you are connected!\n";
if (send(client_fd, msg, strlen(msg), 0) == -1)
perror("send error!");
close(client_fd);
exit(0);
}
close(client_fd);
}
}
int main() {
server();
return 0;
}
客户端
/*
* client.c
*
* Created on: 2010-6-30
* Author: ankerdiao
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "client.h"
#define SERVPORT 3333
#define MAXDATASIZE 100
#define SERVER_IP "127.0.0.1"
#define DATA "this is a client message 客户端的信息"
void client(){
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if (( sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error!");
exit(1);
}
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr= inet_addr(SERVER_IP);
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1) {
perror("connect error!");
exit(1);
}
write(sockfd,DATA, sizeof(DATA));
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE,0)) == -1) {
perror("recv error!");
exit(1);
}
buf[recvbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
}
int main(int argc, char* argv[]) {
if(argc>=1){
printf("%s\n", argv[1]);
}
client();
return 0;
}
头文件
/*
* client.h
*
* Created on: 2010-6-30
* Author: ankerdiao
*/
#ifndef CLIENT_H_
#define CLIENT_H_
#ifdef __cplusplus
extern "C" {
#endif
void client();
#ifdef __cplusplus
}
#endif
#endif /* CLIENT_H_ */
c++实现调用C函数
/*
* cppClient.cpp
*
* Created on: 2010-6-30
* Author: ankerdiao
*/
#include "client.h"
int main(){
client();
return 0;
}
第一步采用gcc编译服务端:
hqlong@ubuntu:~$ gcc server.c -o server
第二步启动服务等待链接:
hqlong@ubuntu:~$ ./server
第三步采用gcc编译客户端:
(有main函数的,编译后可执行)
hqlong@ubuntu:~$ gcc client.c -o client
hqlong@ubuntu:~$ ./client
(没有main函数的,编译后不可执行,但可以供c++调用的库)
hqlong@ubuntu:~$ gcc -c client.c -o client.o
第四步编译C++源代码:
hqlong@ubuntu:~$ g++ -c cppClient.cpp -o cppClient.o (只编译不连接)
第五步通过头文件连接编译好的c库和c++库:
hqlong@ubuntu:~$ g++ -o cppClient cppClient.o client.o
能实现这样连接的关键点是c和c++都实现了头文件client.h的定义:
[b]#ifdef __cplusplus
extern "C" {
#endif[/b]
。。。。。。
void client();
。。。。。。
[b]#ifdef __cplusplus
}
#endif[/b]
参考:
http://hqlong.com/2009/06/800.html
http://man.chinaunix.net/develop/c&c++/linux_c/default.htm